unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 779c3eeafd00a75f8ea25d0d4fdb7a2946bbb5db 1345 bytes (raw)
$ git show v2.0.0pre1:examples/big_app_gc.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
# Run GC after every request, before attempting to accept more connections.
#
# You could customize this patch to read REQ["PATH_INFO"] and only
# call GC.start after expensive requests.
#
# We could have this wrap the response body.close as middleware, but the
# scannable stack is would still be bigger than it would be here.
#
# 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. It's ideal to call the GC
# inside the HTTP server (vs middleware or hooks) since the stack is
# smaller at this point, so the GC will both be faster and more
# effective at releasing unused memory.
#
# This monkey patch 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.

class Unicorn::HttpServer
  REQ = Unicorn::HttpRequest::REQ
  alias _process_client process_client
  undef_method :process_client
  def process_client(client)
    _process_client(client)
    REQ.clear
    GC.start
  end
end if defined?(Unicorn)

git clone https://yhbt.net/unicorn.git