开发者

max-requests-per-worker in unicorn

开发者 https://www.devze.com 2023-04-13 06:35 出处:网络
I sought, but did not find, a max-requests-per-worker option in unicorn similar to gunicorn\'s max_requests or apache\'s MaxRequestsPerChild.开发者_运维百科

I sought, but did not find, a max-requests-per-worker option in unicorn similar to gunicorn's max_requests or apache's MaxRequestsPerChild.开发者_运维百科

Does it exist?

If not, has anyone implemented it?

I'm thinking of putting it in the file where I have oobgc, since that gets control after every requests anyway. Does that sound about right?

The problem is that my unicorn workers are getting big and fat, and garbage collection is taking more and more of my CPU.


i've just released 'unicorn-worker-killer' gem. This enables you to kill Unicorn worker based on 1) Max number of requests and 2) Process memory size (RSS), without affecting the request. It's really easy to use. At first, please add this line to your Gemfile.

gem 'unicorn-worker-killer'

Then, please add the following lines to your config.ru.

# Unicorn self-process killer
require 'unicorn/worker_killer'

# Max requests per worker
use Unicorn::WorkerKiller::MaxRequests, 3072, 4096

# Max memory size (RSS) per worker
use Unicorn::WorkerKiller::Oom, (256*(1024**2)), (384*(1024**2))

It's highly recommended to randomize the threshold to avoid killing all workers at once.


Unicorn doesn't offer a max-requests.

The unicorn master will re-spawn any worker which exits and a worker will gracefully exit at the end of the current request when it receives a QUIT signal, so you could easily roll your own max request logic into your worker request life-cycle.

With Rails, something like the following in your application controller (alternatively, similar logic in a rack middleware)

after_filter do
  @@request_count ||= 0
  Process.kill('QUIT',$$) if (@@request_count += 1) > MAX_REQUESTS
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号