oob_gc: reimplement to fix breakage and add tests
[unicorn.git] / lib / unicorn / oob_gc.rb
blob312b44c375b30639c5b3ae18c55f5a3e621c42fc
1 # -*- encoding: binary -*-
3 # Runs GC after requests, after closing the client socket and
4 # before attempting to accept more connections.
6 # This shouldn't hurt overall performance as long as the server cluster
7 # is at <50% CPU capacity, and improves the performance of most memory
8 # intensive requests.  This serves to improve _client-visible_
9 # performance (possibly at the cost of overall performance).
11 # Increasing the number of +worker_processes+ may be necessary to
12 # improve average client response times because some of your workers
13 # will be busy doing GC and unable to service clients.  Think of
14 # using more workers with this module as a poor man's concurrent GC.
16 # We'll call GC after each request is been written out to the socket, so
17 # the client never sees the extra GC hit it.
19 # This middleware is _only_ effective for applications that use a lot
20 # of memory, and will hurt simpler apps/endpoints that can process
21 # multiple requests before incurring GC.
23 # This middleware is only designed to work with unicorn, as it harms
24 # performance with keepalive-enabled servers.
26 # Example (in config.ru):
28 #     require 'unicorn/oob_gc'
30 #     # GC ever two requests that hit /expensive/foo or /more_expensive/foo
31 #     # in your app.  By default, this will GC once every 5 requests
32 #     # for all endpoints in your app
33 #     use Unicorn::OobGC, 2, %r{\A/(?:expensive/foo|more_expensive/foo)}
35 # Feedback from users of early implementations of this module:
36 # * http://comments.gmane.org/gmane.comp.lang.ruby.unicorn.general/486
37 # * http://article.gmane.org/gmane.comp.lang.ruby.unicorn.general/596
38 module Unicorn::OobGC
40   # this pretends to be Rack middleware because it used to be
41   # But we need to hook into unicorn internals so we need to close
42   # the socket before clearing the request env.
43   #
44   # +interval+ is the number of requests matching the +path+ regular
45   # expression before invoking GC.
46   def self.new(app, interval = 5, path = %r{\A/})
47     @@nr = interval
48     self.const_set :OOBGC_PATH, path
49     self.const_set :OOBGC_INTERVAL, interval
50     ObjectSpace.each_object(Unicorn::HttpServer) do |s|
51       s.extend(self)
52       self.const_set :OOBGC_ENV, s.instance_variable_get(:@request).env
53     end
54     app # pretend to be Rack middleware since it was in the past
55   end
57   #:stopdoc:
58   PATH_INFO = "PATH_INFO"
59   def process_client(client)
60     super(client) # Unicorn::HttpServer#process_client
61     if OOBGC_PATH =~ OOBGC_ENV[PATH_INFO] && ((@@nr -= 1) <= 0)
62       @@nr = OOBGC_INTERVAL
63       OOBGC_ENV.clear
64       GC.start
65     end
66   end
68   # :startdoc:
69 end