how does a middleware get deleted?

余生颓废 提交于 2019-12-22 09:06:54

问题


rack-timeout is included in the Gemfile, but we only want it as middleware on production. Thus in an initializer, we have:

config.middleware.delete Rack::Timeout

Inspecting before and after this line shows rack-timeout removed from the array. Regardless, requests are still timing out, and a quick 'puts' in the gem shows that it is indeed the culprit.

Is this because the middleware stack has already been built before delete is called? Or is the stack read in every request? If that's the case, what could be the issue?


回答1:


Why not just have something like the following?

group :production do
  gem "rack-timeout"
end

In theory, the middleware deletion in the initializer should take care of the problem after a server restart, assuming you're talking about putting something in config/initializers/.


Did a little more experimentation and dropped this into config/initializers/rack-timeout.rb:

if Rails.env.production?
  Rack::Timeout.timeout = 0.5
else
  Rails.configuration.middleware.delete Rack::Timeout
end

And this in a scaffolded controller:

sleep 1

Everything seemed cool after I restarted the dev server (no timeouts in sight :D). So, maybe just a bad variable.

I still think using a production-only group is the better solution.

Ran with Rails 3.2.2 with ruby 1.9.2-p290 on OSX.



来源:https://stackoverflow.com/questions/7856851/how-does-a-middleware-get-deleted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!