{"id":18319742,"url":"https://github.com/redding/much-timeout","last_synced_at":"2025-04-05T22:31:35.346Z","repository":{"id":55102536,"uuid":"60543762","full_name":"redding/much-timeout","owner":"redding","description":"IO.select based timeouts; an alternative to Ruby's stdlib Timeout module.","archived":false,"fork":false,"pushed_at":"2021-01-10T14:17:58.000Z","size":17,"stargazers_count":5,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-21T12:57:40.998Z","etag":null,"topics":["ruby","timeout"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-06T16:36:11.000Z","updated_at":"2021-07-01T08:52:34.000Z","dependencies_parsed_at":"2022-08-14T12:01:00.248Z","dependency_job_id":null,"html_url":"https://github.com/redding/much-timeout","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-timeout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-timeout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-timeout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-timeout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redding","download_url":"https://codeload.github.com/redding/much-timeout/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411236,"owners_count":20934650,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ruby","timeout"],"created_at":"2024-11-05T18:14:08.398Z","updated_at":"2025-04-05T22:31:34.888Z","avatar_url":"https://github.com/redding.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MuchTimeout\n\nIO.select based timeouts.  This is an alternative to the stdlib's Timeout module that doesn't rely on `sleep`.  This should produce more accurate timeouts with an expanded API for different handling options.\n\n## Usage\n\n### `timeout`\n\n```ruby\nrequire 'much-timeout'\n\nMuchTimeout.timeout(5) do\n  # ... something that should be interrupted ...\n\n  # raises a `MuchTimeout::TimeoutError` exception if it takes more than 5 seconds\n  # returns the result of the block otherwise\nend\n```\n\nMuchTimeout, in its basic form, is a replacement for Timeout.  The main difference is that `IO.select` on an internal pipe is the mechanism for detecting the timeout.\n\n**Note**: like Timeout, **`Thread#raise` is used to interrupt the block**.  This technique is [widely](http://blog.headius.com/2008/02/ruby-threadraise-threadkill-timeoutrb.html) [considered](http://www.mikeperham.com/2015/05/08/timeout-rubys-most-dangerous-api/) to be [dangerous](http://jvns.ca/blog/2015/11/27/why-rubys-timeout-is-dangerous-and-thread-dot-raise-is-terrifying/).  Be aware and use responsibly.\n\n```ruby\nMuchTimeout.timeout(5, MyCustomTimeoutError) do\n  # ... something that should be interrupted ...\n\n  # raises a `MyCustomTimeoutError` exception if it takes more than 5 seconds\nend\n```\n\nLike Timeout, you can optionally specify a custom exception class to raise.\n\n### `optional_timeout`\n\n```ruby\nseconds = [5, nil].sample\nMuchTimeout.optional_timeout(seconds) do\n  # ... something that should be interrupted ...\n\n  # raises an exception if seconds is not nil and it takes more than 5 seconds\n  # otherwise the block is called directly and will not be interrupted\nend\n```\n\nIn addtion to the basic `timeout` API, MuchTimeout provides `optional_timeout` which conditionally applies timeout handling based on the given seconds value.  Passing `nil` seconds will just call the block and will not apply any timeout handling (where passing `nil` seconds to `timeout` raises an argument error).\n\n### `just_{optional_}timeout`\n\n```ruby\nMuchTimeout.just_timeout(5, :do =\u003e proc{\n  # ... something that should be interrupted ...\n\n  # interrupt if it takes more than 5 seconds\n  # no exceptions are raised (they are all rescued internally)\n})\n\nseconds = [5, nil].sample\nMuchTimeout.just_optional_timeout(seconds, :do =\u003e proc{\n  # ... something that should be interrupted ...\n\n  # interrupt if seconds is not nil and it takes more than 5 seconds\n  # no exceptions are raised (they are all rescued internally)\n})\n```\n\nThese alternative timeout methods execute and interrupt the given `:do` block if it times out.  However, no exceptions are raised and no exception handling is required (the `Thread#raise` interrupt is rescued internally).  Use this option to avoid any custom exception handling logic when you don't care about the timeout exception information.\n\nIn the case you want to run some custom logic when a timeout occurs, pass an optional `:on_timeout` proc:\n\n```ruby\nMuchTimeout.just_timeout(5, {\n  :do =\u003e proc{\n    # ... something that should be interrupted ...\n  },\n  :on_timeout =\u003e proc{\n    # ... something that should run when a timeout occurs ...\n  }\n})\n```\n\nThis works as you'd expect for both `just_timeout` and `just_optional_timeout`.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'much-timeout'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install much-timeout\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fmuch-timeout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredding%2Fmuch-timeout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fmuch-timeout/lists"}