slowpoke 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5001012d6c99301ae24d91ab6d400e68d3bba3194829645bcd9334e05c630793
4
- data.tar.gz: 750108765f01af869c4ed46796ce015dc9b8b82dd0f3f3f669387850ccc561d4
3
+ metadata.gz: 79e082a34ab895956d7086764a63dc6f4dbedeee15f7557f727a5b23fa51ad08
4
+ data.tar.gz: e7b7aa0452587608fbf2a03f4dad9289562b9ece95a07fc98cf526e9ac3cfb5f
5
5
  SHA512:
6
- metadata.gz: be064a8afb4a84a37dc7282300a510a769dda19283b7a2d1f71867e10f4361ea333d8ff9aa414c3346be8da2abde724aa5376b685a2e137850189b3c33ba4842
7
- data.tar.gz: e468660cfb1c056c10fa41cea26fc116802fb7aefde498c831bbf56b4bc76460925bb1c6f3be7f9a7f9f5a1f6344df5f42dd9808c9fda806caea2565cdce4420
6
+ metadata.gz: 4e4d9eee0e505d0163b1d34e7a8a34b35f399261cacb216d422478b0a81d7637b54a967749d4feb8de04ff40e03ef88f6cb066dbed6fbdf168851e454aaed343
7
+ data.tar.gz: c2fb32575fc524033c39dc351022421da93cb928747251f463c53b6352adeca75ce635d02dbd35587af8c8b5e5a9d5e5092de798afab6171364ceaf9af7c7bee
@@ -1,6 +1,10 @@
1
+ ## 0.3.2 (2019-12-23)
2
+
3
+ - Added `on_timeout` method
4
+
1
5
  ## 0.3.1 (2019-12-10)
2
6
 
3
- - Added support for conditional timeouts
7
+ - Added support for dynamic timeouts
4
8
 
5
9
  ## 0.3.0 (2019-05-31)
6
10
 
data/README.md CHANGED
@@ -2,9 +2,9 @@
2
2
 
3
3
  [Rack::Timeout](https://github.com/heroku/rack-timeout) enhancements for Rails
4
4
 
5
- - conditional timeouts
5
+ - safer service timeouts
6
+ - dynamic timeouts
6
7
  - custom error pages
7
- - [safer service timeouts](https://github.com/heroku/rack-timeout/issues/39)
8
8
 
9
9
  ## Installation
10
10
 
@@ -47,7 +47,7 @@ The default timeout is 15 seconds. You can change this in `config/environments/p
47
47
  config.slowpoke.timeout = 5
48
48
  ```
49
49
 
50
- For conditional timeouts, use:
50
+ For dynamic timeouts, use:
51
51
 
52
52
  ```ruby
53
53
  config.slowpoke.timeout = lambda do |env|
@@ -66,9 +66,30 @@ end
66
66
 
67
67
  To learn more, see the [Rack::Timeout documentation](https://github.com/heroku/rack-timeout).
68
68
 
69
- ## Threaded Servers
69
+ ## Safer Service Timeouts
70
70
 
71
- The only safe way to recover from a request timeout is to spawn a new process. For threaded servers like Puma, this means killing all threads when any one of them times out. This can have a significant impact on performance.
71
+ Rack::Timeout can raise an exception at any point in the code, which can leave your app in an [unclean state](https://www.schneems.com/2017/02/21/the-oldest-bug-in-ruby-why-racktimeout-might-hose-your-server/). The safest way to recover from a request timeout is to spawn a new process. This is the default behavior for Slowpoke.
72
+
73
+ For threaded servers like Puma, this means killing all threads when any one of them times out. This can have a significant impact on performance.
74
+
75
+ You can customize this behavior with:
76
+
77
+ ```ruby
78
+ Slowpoke.on_timeout do |env|
79
+ next if Rails.env.development? || Rails.env.test?
80
+
81
+ exception = env["action_dispatch.exception"]
82
+ if exception && exception.backtrace.first.include?("/active_record/")
83
+ Slowpoke.kill
84
+ end
85
+ end
86
+ ```
87
+
88
+ Note: To access `env["action_dispatch.exception"]` in development, temporarily add to `config/environments/development.rb`:
89
+
90
+ ```ruby
91
+ config.consider_all_requests_local = false
92
+ ```
72
93
 
73
94
  ## Database Timeouts
74
95
 
@@ -4,7 +4,7 @@
4
4
  <title>This page took too long to load (503)</title>
5
5
  <meta name="viewport" content="width=device-width,initial-scale=1">
6
6
  <style>
7
- body {
7
+ .rails-default-error-page {
8
8
  background-color: #EFEFEF;
9
9
  color: #2E2F30;
10
10
  text-align: center;
@@ -12,13 +12,13 @@
12
12
  margin: 0;
13
13
  }
14
14
 
15
- div.dialog {
15
+ .rails-default-error-page div.dialog {
16
16
  width: 95%;
17
17
  max-width: 33em;
18
18
  margin: 4em auto 0;
19
19
  }
20
20
 
21
- div.dialog > div {
21
+ .rails-default-error-page div.dialog > div {
22
22
  border: 1px solid #CCC;
23
23
  border-right-color: #999;
24
24
  border-left-color: #999;
@@ -31,13 +31,13 @@
31
31
  box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
32
  }
33
33
 
34
- h1 {
34
+ .rails-default-error-page h1 {
35
35
  font-size: 100%;
36
36
  color: #730E15;
37
37
  line-height: 1.5em;
38
38
  }
39
39
 
40
- div.dialog > p {
40
+ .rails-default-error-page div.dialog > p {
41
41
  margin: 0 0 1em;
42
42
  padding: 1em;
43
43
  background-color: #F7F7F7;
@@ -54,7 +54,7 @@
54
54
  </style>
55
55
  </head>
56
56
 
57
- <body>
57
+ <body class="rails-default-error-page">
58
58
  <!-- This file lives in public/503.html -->
59
59
  <div class="dialog">
60
60
  <div>
@@ -9,6 +9,30 @@ require "slowpoke/version"
9
9
 
10
10
  module Slowpoke
11
11
  ENV_KEY = "slowpoke.timed_out".freeze
12
+
13
+ def self.kill
14
+ if defined?(::PhusionPassenger)
15
+ `passenger-config detach-process #{Process.pid}`
16
+ elsif defined?(::Puma)
17
+ Process.kill("TERM", Process.pid)
18
+ else
19
+ Process.kill("QUIT", Process.pid)
20
+ end
21
+ end
22
+
23
+ def self.on_timeout(&block)
24
+ if block_given?
25
+ @on_timeout = block
26
+ else
27
+ @on_timeout
28
+ end
29
+ end
30
+
31
+ on_timeout do |env|
32
+ next if Rails.env.development? || Rails.env.test?
33
+
34
+ Slowpoke.kill
35
+ end
12
36
  end
13
37
 
14
38
  # remove noisy logger
@@ -7,19 +7,11 @@ module Slowpoke
7
7
  def call(env)
8
8
  @app.call(env)
9
9
  ensure
10
- if env[Slowpoke::ENV_KEY]
11
- # extremely important
12
- # protect the process with a restart
13
- # https://github.com/heroku/rack-timeout/issues/39
14
- # can't do in timed_out state consistently
15
- if defined?(::PhusionPassenger)
16
- `passenger-config detach-process #{Process.pid}`
17
- elsif defined?(::Puma)
18
- Process.kill("TERM", Process.pid)
19
- else
20
- Process.kill("QUIT", Process.pid)
21
- end
22
- end
10
+ # extremely important
11
+ # protect the process with a restart
12
+ # https://github.com/heroku/rack-timeout/issues/39
13
+ # can't do in timed_out state consistently
14
+ Slowpoke.on_timeout.call(env) if env[Slowpoke::ENV_KEY]
23
15
  end
24
16
  end
25
17
  end
@@ -20,7 +20,7 @@ module Slowpoke
20
20
  service_timeout: service_timeout.to_i
21
21
  end
22
22
 
23
- app.config.middleware.insert(0, Slowpoke::Middleware) unless Rails.env.development? || Rails.env.test?
23
+ app.config.middleware.insert(0, Slowpoke::Middleware)
24
24
  end
25
25
  end
26
26
  end
@@ -1,3 +1,3 @@
1
1
  module Slowpoke
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
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.1
4
+ version: 0.3.2
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-12-11 00:00:00.000000000 Z
11
+ date: 2019-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties