Module: Unicorn::WorkerKiller::MaxRequests
- Defined in:
- lib/unicorn/worker_killer.rb
Class Method Summary collapse
-
.new(app, max_requests_min = 3072, max_requests_max = 4096, verbose = false) ⇒ Object
Killing the process must be occurred at the outside of the request.
Instance Method Summary collapse
Class Method Details
.new(app, max_requests_min = 3072, max_requests_max = 4096, verbose = false) ⇒ Object
Killing the process must be occurred at the outside of the request. We’re using similar techniques used by OobGC, to ensure actual killing doesn’t affect the request.
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/unicorn/worker_killer.rb', line 77 def self.new(app, max_requests_min = 3072, max_requests_max = 4096, verbose = false) ObjectSpace.each_object(Unicorn::HttpServer) do |s| s.extend(self) s.instance_variable_set(:@_worker_max_requests_min, max_requests_min) s.instance_variable_set(:@_worker_max_requests_max, max_requests_max) s.instance_variable_set(:@_verbose, verbose) end app # pretend to be Rack middleware since it was in the past end |
Instance Method Details
#process_client(client) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/unicorn/worker_killer.rb', line 92 def process_client(client) super(client) # Unicorn::HttpServer#process_client return if @_worker_max_requests_min == 0 && @_worker_max_requests_max == 0 @_worker_process_start ||= Time.now @_worker_cur_requests ||= @_worker_max_requests_min + randomize(@_worker_max_requests_max - @_worker_max_requests_min + 1) @_worker_max_requests ||= @_worker_cur_requests logger.info "#{self}: worker (pid: #{Process.pid}) has #{@_worker_cur_requests} left before being killed" if @_verbose if (@_worker_cur_requests -= 1) <= 0 logger.warn "#{self}: worker (pid: #{Process.pid}) exceeds max number of requests (limit: #{@_worker_max_requests})" Unicorn::WorkerKiller.kill_self(logger, @_worker_process_start) end end |
#randomize(integer) ⇒ Object
88 89 90 |
# File 'lib/unicorn/worker_killer.rb', line 88 def randomize(integer) RUBY_VERSION > "1.9" ? Random.rand(integer.abs) : rand(integer) end |