1 # -*- encoding: binary -*-
3 # Strongly consider https://github.com/tmm1/gctools if using Ruby 2.1+
4 # It is built on new APIs in Ruby 2.1, so it is more intelligent than
5 # this historical implementation.
7 # Users on Ruby 2.0 (not 2.1+) may also want to check out
8 # lib/middleware/unicorn_oobgc.rb from the Discourse project
9 # (https://github.com/discourse/discourse)
11 # The following information is only for historical versions of Ruby.
13 # Runs GC after requests, after closing the client socket and
14 # before attempting to accept more connections.
16 # This shouldn't hurt overall performance as long as the server cluster
17 # is at <50% CPU capacity, and improves the performance of most memory
18 # intensive requests. This serves to improve _client-visible_
19 # performance (possibly at the cost of overall performance).
21 # Increasing the number of +worker_processes+ may be necessary to
22 # improve average client response times because some of your workers
23 # will be busy doing GC and unable to service clients. Think of
24 # using more workers with this module as a poor man's concurrent GC.
26 # We'll call GC after each request is been written out to the socket, so
27 # the client never sees the extra GC hit it.
29 # This middleware is _only_ effective for applications that use a lot
30 # of memory, and will hurt simpler apps/endpoints that can process
31 # multiple requests before incurring GC.
33 # This middleware is only designed to work with unicorn, as it harms
34 # performance with keepalive-enabled servers.
36 # Example (in config.ru):
38 # require 'unicorn/oob_gc'
40 # # GC ever two requests that hit /expensive/foo or /more_expensive/foo
41 # # in your app. By default, this will GC once every 5 requests
42 # # for all endpoints in your app
43 # use Unicorn::OobGC, 2, %r{\A/(?:expensive/foo|more_expensive/foo)}
45 # Feedback from users of early implementations of this module:
46 # * http://comments.gmane.org/gmane.comp.lang.ruby.unicorn.general/486
47 # * http://article.gmane.org/gmane.comp.lang.ruby.unicorn.general/596
50 # this pretends to be Rack middleware because it used to be
51 # But we need to hook into unicorn internals so we need to close
52 # the socket before clearing the request env.
54 # +interval+ is the number of requests matching the +path+ regular
55 # expression before invoking GC.
56 def self.new(app, interval = 5, path = %r{\A/})
58 self.const_set :OOBGC_PATH, path
59 self.const_set :OOBGC_INTERVAL, interval
60 ObjectSpace.each_object(Unicorn::HttpServer) do |s|
62 self.const_set :OOBGC_ENV, s.instance_variable_get(:@request).env
64 app # pretend to be Rack middleware since it was in the past
68 PATH_INFO = "PATH_INFO"
69 def process_client(client)
70 super(client) # Unicorn::HttpServer#process_client
71 if OOBGC_PATH =~ OOBGC_ENV[PATH_INFO] && ((@@nr -= 1) <= 0)
76 GC.disable if disabled