有没有一种简单的方法可以在 Passenger 的请求周期之外运行垃圾收集?

发布于 2024-11-13 22:43:41 字数 198 浏览 2 评论 0原文

Unicorn 有 OobGC 可用于运行 GC.start 在一定数量的请求之后。

Phusion Passenger 中有类似的东西吗?

Unicorn has OobGC rack middleware that can be used to run GC.start after a certain number of requests.

Is there a similar sort of thing in Phusion Passenger?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮凉 2024-11-20 22:43:41

Phusion Passenger 4 正式引入了带外垃圾收集机制。它比 Unicorn 更灵活,允许任意工作,而不仅仅是垃圾收集。 http:// blog.phusion.nl/2013/01/22/phusion-passenger-4-technology-preview-out-of-band-work/

Phusion Passenger 4 officially introduces an out of band garbage collection mechanism. It's more flexible than Unicorn's by allowing any arbitrary work, not just garbage collection. http://blog.phusion.nl/2013/01/22/phusion-passenger-4-technology-preview-out-of-band-work/

北渚 2024-11-20 22:43:41

挂钩 PhusionPassenger::Rack::RequestHandler#process_request() 是我发现的唯一机制。

要以与 Unicorn OobGC 类似的方式执行此操作,您可以使用以下模块:

module PassengerOobGC
  def self.install!(path, interval = 5)
    self.const_set :OOBGC_PATH,     path
    self.const_set :OOBGC_INTERVAL, interval
    @@oob_nr = interval
    PhusionPassenger::Rack::RequestHandler.send :include, self
  end

  def self.included(base)
    base.send :alias_method_chain, :process_request, :gc
  end

  def process_request_with_gc(env, *args)
    process_request_without_gc(env, *args)

    if OOBGC_PATH =~ env["PATH_INFO"] && ((@@oob_nr -= 1) <= 0)
      @@oob_nr = OOBGC_INTERVAL
      GC.start
    end
  end
end

并在初始化程序中调用它:

if defined?(PhusionPassenger::Rack::RequestHandler)
  require 'passenger_oob_gc'
  PassengerOobGC.install!(%r{^/admin/}, 3)
end

Hooking into PhusionPassenger::Rack::RequestHandler#process_request() is the only mechanism I have found.

To do this in a similar way to the Unicorn OobGC, you can use the following module:

module PassengerOobGC
  def self.install!(path, interval = 5)
    self.const_set :OOBGC_PATH,     path
    self.const_set :OOBGC_INTERVAL, interval
    @@oob_nr = interval
    PhusionPassenger::Rack::RequestHandler.send :include, self
  end

  def self.included(base)
    base.send :alias_method_chain, :process_request, :gc
  end

  def process_request_with_gc(env, *args)
    process_request_without_gc(env, *args)

    if OOBGC_PATH =~ env["PATH_INFO"] && ((@@oob_nr -= 1) <= 0)
      @@oob_nr = OOBGC_INTERVAL
      GC.start
    end
  end
end

and invoke it in an initializer with:

if defined?(PhusionPassenger::Rack::RequestHandler)
  require 'passenger_oob_gc'
  PassengerOobGC.install!(%r{^/admin/}, 3)
end
蓬勃野心 2024-11-20 22:43:41

你必须修补乘客。在每个请求被移交后执行 GC.start 可确保在保留客户端请求时不会发生垃圾收集。如果您想减少平均请求时间,您可能会考虑这是一项单行更改。

在 lib/phusion_passenger/abstract_request_handler.rb 中,修补 accept_and_process_next_request 并在末尾添加 GC.start 调用,并设置适当的间隔。

有关示例,请参阅此提交(感谢@raphaelcm)。

You have to patch Passenger. Doing a GC.start after each request has been handed off ensures that garbage collection never occurs while holding a client request. This is a one-line change that you might consider if you're trying to reduce your average request time.

In lib/phusion_passenger/abstract_request_handler.rb, patch accept_and_process_next_request and add the GC.start call at the end, with an appropriate interval.

See this commit for an example (thanks, @raphaelcm).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文