Sha256: 36410d26f86d7ff25bddc571b16600261cae04afa28085be9a6190ace9cce834
Contents?: true
Size: 1.11 KB
Versions: 1
Compression:
Stored size: 1.11 KB
Contents
--- title: Timeout layout: gem-single name: dry-effects --- `Timeout` consists of two methods: - `timeout` returns an ever-decreasing number of seconds until this number reaches 0. - `timed_out?` checks if no time left. The handler provides the initial timeout and uses the monotonic time for counting down. A practical example is limiting the length of all external HTTP calls during request processing. Sample class for making HTTP requests in an application: ```ruby class MakeRequest include Dry::Effects.Timeout(:http) def call(url) HTTParty.get(url, timeout: timeout) end end ``` Handling timeouts: ```ruby class WithTimeout include Dry::Effects::Handler.Timeout(:http) def initialize(app) @app = app end def call(env) with_timeout(10.0) { @app.(env) } rescue Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout [504, {}, ["Gateway Timeout"]] end end ``` The code above guarantees all requests made with `MakeRequest` during `@app.(env)` will finish within 10 seconds. If `@app` doesn't spend much time somewhere else, it gives a reasonably reliable hard limit on request processing.
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-effects-0.1.4 | docsite/source/effects/timeout.html.md |