rack-timeout 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +31 -15
- data/lib/rack/timeout.rb +2 -2
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c350e40f59d01af35e44aa9caebf66752f30e15
|
4
|
+
data.tar.gz: f5f0133eb613f110cff035afee6dd512eb0a0d10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 805fe810b977eb29867df02daac8f10eeb28344c57fa8bb1d91ee3a45dc3fd6a59d525db64986f0a3f51d27923dc48c95ef3bcd0ecf838bf03106aa8ef95ab4d
|
7
|
+
data.tar.gz: 704345ef0762eb668eb3a6a8d0ef08c3e4cfad85a6aad0ab332986afc1cd6b52d72e6b785b57cb64f502fc1dd0c652747b34ab557eb30eeabf50e0b022a82253
|
data/README.markdown
CHANGED
@@ -17,20 +17,26 @@ The following covers currently supported versions of Rails, Rack, Ruby, and Bund
|
|
17
17
|
|
18
18
|
### Rails apps
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
```ruby
|
21
|
+
# Gemfile
|
22
|
+
gem "rack-timeout"
|
23
|
+
```
|
22
24
|
|
23
25
|
That's all that's required if you want to use the default timeout of 15s. To use a custom timeout, create an initializer file:
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
+
```ruby
|
28
|
+
# config/initializers/timeout.rb
|
29
|
+
Rack::Timeout.timeout = 5 # seconds
|
30
|
+
```
|
27
31
|
|
28
32
|
### Sinatra and other Rack apps
|
29
33
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
```ruby
|
35
|
+
# config.ru
|
36
|
+
require "rack-timeout"
|
37
|
+
use Rack::Timeout # Call as early as possible so rack-timeout runs before all other middleware.
|
38
|
+
Rack::Timeout.timeout = 5 # Recommended. If omitted, defaults to 15 seconds.
|
39
|
+
```
|
34
40
|
|
35
41
|
|
36
42
|
The Rabbit Hole
|
@@ -168,13 +174,17 @@ Observers are blocks that are notified about state changes during a request's li
|
|
168
174
|
|
169
175
|
You can register an observer with:
|
170
176
|
|
171
|
-
|
177
|
+
```ruby
|
178
|
+
Rack::Timeout.register_state_change_observer(:a_unique_name) { |env| do_things env }
|
179
|
+
```
|
172
180
|
|
173
181
|
There's currently no way to subscribe to changes into or out of a particular state. To check the actual state we're moving into, read `env['rack-timeout.info'].state`. Handling going out of a state would require some additional logic in the observer.
|
174
182
|
|
175
183
|
You can remove an observer with `unregister_state_change_observer`:
|
176
184
|
|
177
|
-
|
185
|
+
```ruby
|
186
|
+
Rack::Timeout.unregister_state_change_observer(:a_unique_name)
|
187
|
+
```
|
178
188
|
|
179
189
|
|
180
190
|
rack-timeout's logging is implemented using an observer; see `Rack::Timeout::StageChangeLoggingObserver` in logger.rb for the implementation.
|
@@ -193,21 +203,27 @@ Rack::Timeout will try to use `Rails.logger` if present, otherwise it'll look fo
|
|
193
203
|
|
194
204
|
A custom logger can be set via `Rack::Timeout::StageChangeLoggingObserver.logger`. This takes priority over the automatic logger detection:
|
195
205
|
|
196
|
-
|
206
|
+
```ruby
|
207
|
+
Rack::Timeout::StageChangeLoggingObserver.logger = Logger.new
|
208
|
+
```
|
197
209
|
|
198
210
|
When creating its own logger, rack-timeout will use a log level of `INFO`. Otherwise whatever log level is already set on the logger being used continues in effect.
|
199
211
|
|
200
212
|
Logging is enabled by default if Rack::Timeout is loaded via the `rack-timeout` file (recommended), but can be removed by unregistering its observer:
|
201
213
|
|
202
|
-
|
214
|
+
```ruby
|
215
|
+
Rack::Timeout.unregister_state_change_observer(:logger)
|
216
|
+
```
|
203
217
|
|
204
218
|
Each log line is a set of `key=value` pairs, containing the entries from the `env["rack-timeout.info"]` struct that are not `nil`. See the Request Lifetime section above for a description of each field. Note that while the values for `wait`, `timeout`, and `service` are stored internally as seconds, they are logged as milliseconds for readability.
|
205
219
|
|
206
220
|
A sample log excerpt might look like:
|
207
221
|
|
208
|
-
|
209
|
-
|
210
|
-
|
222
|
+
```
|
223
|
+
source=rack-timeout id=13793c wait=369ms timeout=10000ms state=ready at=info
|
224
|
+
source=rack-timeout id=13793c wait=369ms timeout=10000ms service=15ms state=completed at=info
|
225
|
+
source=rack-timeout id=ea7bd3 wait=371ms timeout=10000ms state=timed_out at=error
|
226
|
+
```
|
211
227
|
|
212
228
|
|
213
229
|
Compatibility
|
data/lib/rack/timeout.rb
CHANGED
@@ -80,7 +80,7 @@ module Rack
|
|
80
80
|
info.wait, info.timeout = seconds_waited, final_wait_timeout # updating the info properties; info.timeout will be the wait timeout at this point
|
81
81
|
if seconds_service_left <= 0 # expire requests that have waited for too long in the queue (as they are assumed to have been dropped by the web server / routing layer at this point)
|
82
82
|
RT._set_state! env, :expired
|
83
|
-
raise RequestExpiryError, "Request older than #{
|
83
|
+
raise RequestExpiryError, "Request older than #{info.ms(:timeout)}."
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
@@ -103,7 +103,7 @@ module Rack
|
|
103
103
|
sleep(sleep_seconds)
|
104
104
|
end
|
105
105
|
RT._set_state! env, :timed_out
|
106
|
-
app_thread.raise(RequestTimeoutError, "Request #{"waited #{info.ms(:wait)}
|
106
|
+
app_thread.raise(RequestTimeoutError, "Request #{"waited #{info.ms(:wait)}, then " if info.wait}ran for longer than #{info.ms(:timeout)}")
|
107
107
|
end
|
108
108
|
response = @app.call(env)
|
109
109
|
ensure
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-timeout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Caio Chassot
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Rack middleware which aborts requests that have been running for longer
|
14
14
|
than a specified timeout.
|
@@ -42,8 +42,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
42
|
version: '0'
|
43
43
|
requirements: []
|
44
44
|
rubyforge_project:
|
45
|
-
rubygems_version: 2.
|
45
|
+
rubygems_version: 2.4.5
|
46
46
|
signing_key:
|
47
47
|
specification_version: 4
|
48
48
|
summary: Abort requests that are taking too long
|
49
49
|
test_files: []
|
50
|
+
has_rdoc:
|