slowpoke 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 196837a764b30dcd76bd206904b4f019a3dc77149a9ab472c9aaa062f5e00584
4
- data.tar.gz: 410bd79283dc9921762ca886d54978f2adac2496e2ad0c6e8a381157e5318dc2
3
+ metadata.gz: 5001012d6c99301ae24d91ab6d400e68d3bba3194829645bcd9334e05c630793
4
+ data.tar.gz: 750108765f01af869c4ed46796ce015dc9b8b82dd0f3f3f669387850ccc561d4
5
5
  SHA512:
6
- metadata.gz: 46f0c622df519a853f40660bcdaa644e1e3586eeb8607ffd09420456a4b10d4a7b536154eb8b911d5131b9d8dca6afe0b6521f48191438176a7924cf6dec3b3d
7
- data.tar.gz: 8ab0b75f01bd8f0662ba21ca4f130ff6926e2f5f5f11dedb296aec35ad2f06f957067d68c4defdf52774da5cdb756a2f590261b28193498ecb1128067ac34843
6
+ metadata.gz: be064a8afb4a84a37dc7282300a510a769dda19283b7a2d1f71867e10f4361ea333d8ff9aa414c3346be8da2abde724aa5376b685a2e137850189b3c33ba4842
7
+ data.tar.gz: e468660cfb1c056c10fa41cea26fc116802fb7aefde498c831bbf56b4bc76460925bb1c6f3be7f9a7f9f5a1f6344df5f42dd9808c9fda806caea2565cdce4420
data/CHANGELOG.md CHANGED
@@ -1,31 +1,35 @@
1
- ## 0.3.0
1
+ ## 0.3.1 (2019-12-10)
2
+
3
+ - Added support for conditional timeouts
4
+
5
+ ## 0.3.0 (2019-05-31)
2
6
 
3
7
  - Use proper signal for Puma
4
8
  - Dropped support for rack-timeout < 0.4
5
9
  - Dropped support for migration timeouts
6
10
  - Dropped support for Rails < 5
7
11
 
8
- ## 0.2.1
12
+ ## 0.2.1 (2018-05-21)
9
13
 
10
14
  - Don’t kill server in test environment
11
15
  - Require rack-timeout < 0.5
12
16
 
13
- ## 0.2.0
17
+ ## 0.2.0 (2017-11-05)
14
18
 
15
19
  - Fixed custom error pages for Rails 5.1
16
20
  - Fixed migration statement timeout
17
21
  - Don’t kill server in development
18
22
 
19
- ## 0.1.3
23
+ ## 0.1.3 (2016-08-03)
20
24
 
21
25
  - Fixed deprecation warning in Rails 5
22
26
  - No longer requires ActiveRecord
23
27
 
24
- ## 0.1.2
28
+ ## 0.1.2 (2016-02-10)
25
29
 
26
30
  - Updated to latest version of rack-timeout, removing the need to bubble timeouts
27
31
 
28
- ## 0.1.1
32
+ ## 0.1.1 (2015-08-02)
29
33
 
30
34
  - Fixed safer service timeouts
31
35
  - Added migration statement timeout
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [Rack::Timeout](https://github.com/heroku/rack-timeout) enhancements for Rails
4
4
 
5
+ - conditional timeouts
5
6
  - custom error pages
6
7
  - [safer service timeouts](https://github.com/heroku/rack-timeout/issues/39)
7
8
 
@@ -21,9 +22,9 @@ rails generate slowpoke:install
21
22
 
22
23
  This creates a `public/503.html` you can customize.
23
24
 
24
- ## Trying It in Development
25
+ ## Development
25
26
 
26
- Temporarily add to `config/environments/development.rb`:
27
+ To try out custom error pages in development, temporarily add to `config/environments/development.rb`:
27
28
 
28
29
  ```ruby
29
30
  config.slowpoke.timeout = 1
@@ -38,7 +39,7 @@ sleep(2)
38
39
 
39
40
  The custom error page should appear.
40
41
 
41
- ## Configuring It for Production
42
+ ## Production
42
43
 
43
44
  The default timeout is 15 seconds. You can change this in `config/environments/production.rb` with:
44
45
 
@@ -46,6 +47,15 @@ The default timeout is 15 seconds. You can change this in `config/environments/p
46
47
  config.slowpoke.timeout = 5
47
48
  ```
48
49
 
50
+ For conditional timeouts, use:
51
+
52
+ ```ruby
53
+ config.slowpoke.timeout = lambda do |env|
54
+ request = Rack::Request.new(env)
55
+ request.path.start_with?("/admin") ? 15 : 5
56
+ end
57
+ ```
58
+
49
59
  Subscribe to timeouts with:
50
60
 
51
61
  ```ruby
@@ -62,7 +72,14 @@ The only safe way to recover from a request timeout is to spawn a new process. F
62
72
 
63
73
  ## Database Timeouts
64
74
 
65
- It’s a good idea to set a [statement timeout](https://github.com/ankane/the-ultimate-guide-to-ruby-timeouts/#statement-timeouts-1) and a [connect timeout](https://github.com/ankane/the-ultimate-guide-to-ruby-timeouts/#activerecord).
75
+ It’s a good idea to set a [statement timeout](https://github.com/ankane/the-ultimate-guide-to-ruby-timeouts/#statement-timeouts-1) and a [connect timeout](https://github.com/ankane/the-ultimate-guide-to-ruby-timeouts/#activerecord). For Postgres, your `config/database.yml` should include something like:
76
+
77
+ ```yml
78
+ production:
79
+ connect_timeout: 3 # sec
80
+ variables:
81
+ statement_timeout: 5s
82
+ ```
66
83
 
67
84
  ## Upgrading
68
85
 
@@ -104,3 +121,11 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
104
121
  - Fix bugs and [submit pull requests](https://github.com/ankane/slowpoke/pulls)
105
122
  - Write, clarify, or fix documentation
106
123
  - Suggest or add new features
124
+
125
+ To get started with development:
126
+
127
+ ```sh
128
+ git clone https://github.com/ankane/slowpoke.git
129
+ cd slowpoke
130
+ bundle install
131
+ ```
data/lib/slowpoke.rb CHANGED
@@ -1,18 +1,16 @@
1
- require "slowpoke/version"
1
+ # dependencies
2
2
  require "rack/timeout/base"
3
+
4
+ # modules
3
5
  require "slowpoke/middleware"
4
6
  require "slowpoke/railtie"
5
- require "action_dispatch/middleware/exception_wrapper"
6
- require "action_controller/base"
7
+ require "slowpoke/timeout"
8
+ require "slowpoke/version"
7
9
 
8
10
  module Slowpoke
9
11
  ENV_KEY = "slowpoke.timed_out".freeze
10
12
  end
11
13
 
12
- # custom error page
13
- ActionDispatch::ExceptionWrapper.rescue_responses["Rack::Timeout::RequestTimeoutError"] = :service_unavailable
14
- ActionDispatch::ExceptionWrapper.rescue_responses["Rack::Timeout::RequestExpiryError"] = :service_unavailable
15
-
16
14
  # remove noisy logger
17
15
  Rack::Timeout.unregister_state_change_observer(:logger)
18
16
 
@@ -2,13 +2,23 @@ module Slowpoke
2
2
  class Railtie < Rails::Railtie
3
3
  config.slowpoke = ActiveSupport::OrderedOptions.new
4
4
 
5
+ # must happen outside initializer (so it runs earlier)
6
+ config.action_dispatch.rescue_responses.merge!(
7
+ "Rack::Timeout::RequestTimeoutError" => :service_unavailable,
8
+ "Rack::Timeout::RequestExpiryError" => :service_unavailable
9
+ )
10
+
5
11
  initializer "slowpoke" do |app|
6
12
  service_timeout = app.config.slowpoke.timeout
7
13
  service_timeout ||= ENV["RACK_TIMEOUT_SERVICE_TIMEOUT"] || ENV["REQUEST_TIMEOUT"] || ENV["TIMEOUT"] || 15
8
- service_timeout = service_timeout.to_i
9
14
 
10
- app.config.middleware.insert_after ActionDispatch::DebugExceptions, Rack::Timeout,
11
- service_timeout: service_timeout
15
+ if service_timeout.respond_to?(:call)
16
+ app.config.middleware.insert_after ActionDispatch::DebugExceptions, Slowpoke::Timeout,
17
+ service_timeout: service_timeout
18
+ else
19
+ app.config.middleware.insert_after ActionDispatch::DebugExceptions, Rack::Timeout,
20
+ service_timeout: service_timeout.to_i
21
+ end
12
22
 
13
23
  app.config.middleware.insert(0, Slowpoke::Middleware) unless Rails.env.development? || Rails.env.test?
14
24
  end
@@ -0,0 +1,18 @@
1
+ module Slowpoke
2
+ class Timeout
3
+ def initialize(app, service_timeout:)
4
+ @app = app
5
+ @service_timeout = service_timeout
6
+ @middleware = {}
7
+ end
8
+
9
+ def call(env)
10
+ service_timeout = @service_timeout.call(env)
11
+ if service_timeout
12
+ (@middleware[service_timeout] ||= Rack::Timeout.new(@app, service_timeout: service_timeout)).call(env)
13
+ else
14
+ @app.call(env)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Slowpoke
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slowpoke
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-01 00:00:00.000000000 Z
11
+ date: 2019-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -94,6 +94,7 @@ files:
94
94
  - lib/slowpoke.rb
95
95
  - lib/slowpoke/middleware.rb
96
96
  - lib/slowpoke/railtie.rb
97
+ - lib/slowpoke/timeout.rb
97
98
  - lib/slowpoke/version.rb
98
99
  homepage: https://github.com/ankane/slowpoke
99
100
  licenses: