Previously (up to rails 5.2.3, rack-timeout 0.5.1) to change the timeout specification for each path using rack-timeout Was specified as follows (slightly simplified).
Gemfile
gem 'rack-timeout', require: 'rack/timeout/base'
config/initializers/rack_timeout.rb
module Rack
class DynamicTimeout < Rack::Timeout
def call(env)
#URL is/30 seconds if starting with admin, 10 seconds otherwise
@service_timeout = env['REQUEST_URI'].start_with?('/admin') ? 30 : 10
super(env)
end
end
end
timeout_params = {
service_timeout: 10,
wait_timeout: 5.minutes.to_i,
wait_overtime: 5.minutes.to_i,
service_past_wait: false,
}
Rails.application.config.middleware.insert_before Rack::Runtime, Rack::DynamicTimeout, timeout_params
However, starting with Rails 6 or rack-timeout 0.6.0, the timeout was shorter than the dynamically specified value of service_timeout
.
It was actually shorter (10 seconds) than the value output to the log (30 seconds) as shown below.
severity:ERROR message:source=rack-timeout id=0284947e-c24c-4024-a9ba-c542b3eb58f9 timeout=30000ms service=10000ms state=timed_out
After investigating the cause by preparing various logs, it seems that the call
of Rack :: Timeout
itself is called in addition to the call
of Rack :: DynamicTimeout
.
For convenience, the call
method was executed twice, first with Rack :: DynamicTimeout
, then with Rack :: Timeout
, and finally the timeout value was specified with Rack :: Timeout
. The value is used.
It seemed like it would take some time to find out where it was called and if it could be changed, so this time I decided to use a patch that overwrites Rack :: Timeout
.
Gemfile
gem 'rack-timeout'
config/initializers/rack_timeout.rb
module Rack
module DynamicTimeout
def call(env)
#URL is/30 seconds if starting with admin, 10 seconds otherwise
@service_timeout = env['REQUEST_URI'].start_with?('/admin') ? 30 : 10
super(env)
end
end
end
Rack::Timeout.prepend Rack::DynamicTimeout
Enabled to handle even if call
of Rack :: Timeout
is called by inserting the module with Module # prepare
.
With this for the time being.
Recommended Posts