Class: Unicorn::OobGC
- Inherits:
-
Struct
- Object
- Struct
- Unicorn::OobGC
- Defined in:
- lib/unicorn/oob_gc.rb
Overview
Run GC after every request, after closing the client socket and before attempting to accept more connections.
This shouldn’t hurt overall performance as long as the server cluster is at <50% CPU capacity, and improves the performance of most memory intensive requests. This serves to improve client-visible performance (possibly at the cost of overall performance).
We’ll call GC after each request is been written out to the socket, so the client never sees the extra GC hit it.
This middleware is only effective for applications that use a lot of memory, and will hurt simpler apps/endpoints that can process multiple requests before incurring GC.
This middleware is only designed to work with Unicorn, as it harms keepalive performance.
Example (in config.ru):
require 'unicorn/oob_gc'
# GC ever two requests that hit /expensive/foo or /more_expensive/foo
# in your app. By default, this will GC once every 5 requests
# for all endpoints in your app
use Unicorn::OobGC, 2, %r{\A/(?:expensive/foo|more_expensive/foo)}
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#body ⇒ Object
Returns the value of attribute body.
-
#env ⇒ Object
Returns the value of attribute env.
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#nr ⇒ Object
Returns the value of attribute nr.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#close ⇒ Object
in Unicorn, this is closed after the client socket.
- #each(&block) ⇒ Object
-
#initialize(app, interval = 5, path = %r{\A/}) ⇒ OobGC
constructor
A new instance of OobGC.
Constructor Details
#initialize(app, interval = 5, path = %r{\A/}) ⇒ OobGC
Returns a new instance of OobGC.
32 33 34 |
# File 'lib/unicorn/oob_gc.rb', line 32 def initialize(app, interval = 5, path = %r{\A/}) super(app, interval, path, interval) end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app
30 31 32 |
# File 'lib/unicorn/oob_gc.rb', line 30 def app @app end |
#body ⇒ Object
Returns the value of attribute body
30 31 32 |
# File 'lib/unicorn/oob_gc.rb', line 30 def body @body end |
#env ⇒ Object
Returns the value of attribute env
30 31 32 |
# File 'lib/unicorn/oob_gc.rb', line 30 def env @env end |
#interval ⇒ Object
Returns the value of attribute interval
30 31 32 |
# File 'lib/unicorn/oob_gc.rb', line 30 def interval @interval end |
#nr ⇒ Object
Returns the value of attribute nr
30 31 32 |
# File 'lib/unicorn/oob_gc.rb', line 30 def nr @nr end |
#path ⇒ Object
Returns the value of attribute path
30 31 32 |
# File 'lib/unicorn/oob_gc.rb', line 30 def path @path end |
Instance Method Details
#call(env) ⇒ Object
36 37 38 39 |
# File 'lib/unicorn/oob_gc.rb', line 36 def call(env) status, headers, self.body = app.call(self.env = env) [ status, headers, self ] end |
#close ⇒ Object
in Unicorn, this is closed after the client socket
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/unicorn/oob_gc.rb', line 46 def close body.close if body.respond_to?(:close) if path =~ env['PATH_INFO'] && ((self.nr -= 1) <= 0) self.nr = interval self.body = nil env.clear GC.start end end |
#each(&block) ⇒ Object
41 42 43 |
# File 'lib/unicorn/oob_gc.rb', line 41 def each(&block) body.each(&block) end |