dry-effects

v0.5
  1. Introduction
  2. Effects
    1. Cache
    2. Current Time
    3. Deferred execution
    4. Environment
    5. Interrupt
    6. Parallel execution
    7. Reader
    8. Resolve (Dependency Injection)
    9. State
    10. Timeout

TOC


Timeout

Timeout consists of two methods:

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:

class MakeRequest
  include Dry::Effects.Timeout(:http)

  def call(url)
    HTTParty.get(url, timeout: timeout)
  end
end

Handling timeouts:

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.