Rack-timeout を使用していますが、正常に動作します。しかし、特定の URL に時間を設定する方法がわかりませんでした。
私が好きでも:
map '/foo/bar' do Rack::Timeout.timeout = 10 終わり
/foo/bar アクションだけでなく、すべてのアクションが 10 秒後に終了します。
特定の URL にタイムアウトを設定することはできますか? または、rack-timeout 以外の別のソリューションを使用する必要がありますか?
Rack-timeout を使用していますが、正常に動作します。しかし、特定の URL に時間を設定する方法がわかりませんでした。
私が好きでも:
map '/foo/bar' do Rack::Timeout.timeout = 10 終わり
/foo/bar アクションだけでなく、すべてのアクションが 10 秒後に終了します。
特定の URL にタイムアウトを設定することはできますか? または、rack-timeout 以外の別のソリューションを使用する必要がありますか?
Jiten Kothariの回答の更新版:
module Rack
class Timeout
@excludes = [
'/statistics',
]
class << self
attr_accessor :excludes
end
def call_with_excludes(env)
#puts 'BEGIN CALL'
#puts env['REQUEST_URI']
#puts 'END CALL'
if self.class.excludes.any? {|exclude_uri| /\A#{exclude_uri}/ =~ env['REQUEST_URI']}
@app.call(env)
else
call_without_excludes(env)
end
end
alias_method_chain :call, :excludes
end
end
このコードをconfig/initializersフォルダーの下にtimeout.rbとして配置し、特定のURLをexcludes配列に配置します
require RUBY_VERSION < '1.9' && RUBY_PLATFORM != "java" ? 'system_timer' : 'timeout'
SystemTimer ||= Timeout
module Rack
class Timeout
@timeout = 30
@excludes = ['your url here',
'your url here'
]
class << self
attr_accessor :timeout, :excludes
end
def initialize(app)
@app = app
end
def call(env)
#puts 'BEGIN CALL'
#puts env['REQUEST_URI']
#puts 'END CALL'
if self.class.excludes.any? {|exclude_uri| /#{exclude_uri}/ =~ env['REQUEST_URI']}
@app.call(env)
else
SystemTimer.timeout(self.class.timeout, ::Timeout::Error) { @app.call(env) }
end
end
end
end