Sha256: 0bf0aa7e05020ec2e42d8915a9cf0fb2d8b416e76b2b7cb122f6219a611afb10

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 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
  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

3 entries across 3 versions & 1 rubygems

Version Path
dry-effects-0.1.3 docsite/source/effects/timeout.html.md
dry-effects-0.1.2 docsite/source/effects/timeout.html.md
dry-effects-0.1.1 docsite/source/effects/timeout.html.md