Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

max-requests-per-worker in unicorn

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.

like image 596
Alex L Avatar asked Feb 22 '23 16:02

Alex L


1 Answers

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.

like image 170
Kazuki Ohta Avatar answered Mar 07 '23 16:03

Kazuki Ohta