port test/unit/test_ccc.rb to Perl 5
[unicorn.git] / lib / unicorn / oob_gc.rb
blobefd9177f1ec78970eb8e34eede025a37a6258795
1 # -*- encoding: binary -*-
2 # frozen_string_literal: false
4 # Strongly consider https://github.com/tmm1/gctools if using Ruby 2.1+
5 # It is built on new APIs in Ruby 2.1, so it is more intelligent than
6 # this historical implementation.
8 # Users on Ruby 2.0 (not 2.1+) may also want to check out
9 # lib/middleware/unicorn_oobgc.rb from the Discourse project
10 # (https://github.com/discourse/discourse)
12 # The following information is only for historical versions of Ruby.
14 # Runs GC after requests, after closing the client socket and
15 # before attempting to accept more connections.
17 # This shouldn't hurt overall performance as long as the server cluster
18 # is at <50% CPU capacity, and improves the performance of most memory
19 # intensive requests.  This serves to improve _client-visible_
20 # performance (possibly at the cost of overall performance).
22 # Increasing the number of +worker_processes+ may be necessary to
23 # improve average client response times because some of your workers
24 # will be busy doing GC and unable to service clients.  Think of
25 # using more workers with this module as a poor man's concurrent GC.
27 # We'll call GC after each request is been written out to the socket, so
28 # the client never sees the extra GC hit it.
30 # This middleware is _only_ effective for applications that use a lot
31 # of memory, and will hurt simpler apps/endpoints that can process
32 # multiple requests before incurring GC.
34 # This middleware is only designed to work with unicorn, as it harms
35 # performance with keepalive-enabled servers.
37 # Example (in config.ru):
39 #     require 'unicorn/oob_gc'
41 #     # GC ever two requests that hit /expensive/foo or /more_expensive/foo
42 #     # in your app.  By default, this will GC once every 5 requests
43 #     # for all endpoints in your app
44 #     use Unicorn::OobGC, 2, %r{\A/(?:expensive/foo|more_expensive/foo)}
46 # Feedback from users of early implementations of this module:
47 # * https://yhbt.net/unicorn-public/0BFC98E9-072B-47EE-9A70-05478C20141B@lukemelia.com/
48 # * https://yhbt.net/unicorn-public/AANLkTilUbgdyDv9W1bi-s_W6kq9sOhWfmuYkKLoKGOLj@mail.gmail.com/
50 module Unicorn::OobGC
52   # this pretends to be Rack middleware because it used to be
53   # But we need to hook into unicorn internals so we need to close
54   # the socket before clearing the request env.
55   #
56   # +interval+ is the number of requests matching the +path+ regular
57   # expression before invoking GC.
58   def self.new(app, interval = 5, path = %r{\A/})
59     @@nr = interval
60     self.const_set :OOBGC_PATH, path
61     self.const_set :OOBGC_INTERVAL, interval
62     ObjectSpace.each_object(Unicorn::HttpServer) do |s|
63       s.extend(self)
64     end
65     app # pretend to be Rack middleware since it was in the past
66   end
68   #:stopdoc:
69   def process_client(*args)
70     super(*args) # Unicorn::HttpServer#process_client
71     env = instance_variable_get(:@request).env
72     if OOBGC_PATH =~ env['PATH_INFO'] && ((@@nr -= 1) <= 0)
73       @@nr = OOBGC_INTERVAL
74       env.clear
75       disabled = GC.enable
76       GC.start
77       GC.disable if disabled
78     end
79   end
81   # :startdoc:
82 end