unicorn-worker-killer 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +4 -4
  2. data/VERSION +1 -1
  3. data/lib/unicorn/worker_killer.rb +11 -7
  4. metadata +2 -2
data/README.md CHANGED
@@ -25,17 +25,17 @@ Add these lines to your `config.ru`.
25
25
 
26
26
  This gem provides two modules.
27
27
 
28
- ### Unicorn::WorkerKiller::MaxRequests(max_requests = 1024)
28
+ ### Unicorn::WorkerKiller::MaxRequests(max_requests_min=3072, max_requests_max=4096)
29
29
 
30
30
  This module automatically restarts the Unicorn workers, based on the number of requests which worker processed.
31
31
 
32
- `max_requests` specifies the maximum number of requests which this worker should process. Once the number exceeds `max_requests`, that worker is automatically restarted. It's highly recommended to randomize this number to avoid restarting all workers at once.
32
+ `max_requests_min` and `max_requests_max` specify the min and max of maximum requests per worker. The actual limit is decided by rand() between `max_requests_min` and `max_requests_max` per worker, to prevent all workers to be dead at the same time. Once the number exceeds the limit, that worker is automatically restarted.
33
33
 
34
- ### Unicorn::WorkerKiller::Oom(memory_size = (1024**3), check_cycle = 16)
34
+ ### Unicorn::WorkerKiller::Oom(memory_limit_min=(1024**3), memory_limit_max=(2*(1024**3)), check_cycle = 16)
35
35
 
36
36
  This module automatically restarts the Unicorn workers, based on its memory size.
37
37
 
38
- `memory_size` specifies the maximum memory which this worker can have. Once the memory size exceeds `memory_size`, that worker is automatically restarted. It's highly recommended to randomize this number to avoid restarting all workers at once.
38
+ `memory_limit_min` and `memory_limit_max` specify the min and max of maximum memory per worker. The actual limit is decided by rand() between `memory_limit_min` and `memory_limit_max` per worker, to prevent all workers to be dead at the same time. Once the memory size exceeds `memory_size`, that worker is automatically restarted.
39
39
 
40
40
  The memory size check is done in every `check_cycle` requests.
41
41
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -28,10 +28,11 @@ module Unicorn::WorkerKiller
28
28
  # affect the request.
29
29
  #
30
30
  # @see https://github.com/defunkt/unicorn/blob/master/lib/unicorn/oob_gc.rb#L40
31
- def self.new(app, memory_size = (1024**3), check_cycle = 16)
31
+ def self.new(app, memory_limit_min = (1024**3), memory_limit_max = (2*(1024**3)), check_cycle = 16)
32
32
  ObjectSpace.each_object(Unicorn::HttpServer) do |s|
33
33
  s.extend(self)
34
- s.instance_variable_set(:@_worker_memory_size, memory_size)
34
+ s.instance_variable_set(:@_worker_memory_limit_min, memory_limit_min)
35
+ s.instance_variable_set(:@_worker_memory_limit_max, memory_limit_max)
35
36
  s.instance_variable_set(:@_worker_check_cycle, check_cycle)
36
37
  s.instance_variable_set(:@_worker_check_count, 0)
37
38
  end
@@ -40,13 +41,14 @@ module Unicorn::WorkerKiller
40
41
 
41
42
  def process_client(client)
42
43
  @_worker_process_start ||= Time.now
44
+ @_worker_memory_limit ||= Random.rand(@_worker_memory_limit_min..@_worker_memory_limit_max)
43
45
  super(client) # Unicorn::HttpServer#process_client
44
46
 
45
47
  @_worker_check_count += 1
46
48
  if @_worker_check_count % @_worker_check_cycle == 0
47
49
  rss = _worker_rss()
48
- if rss > @_worker_memory_size
49
- logger.warn "#{self}: worker (pid: #{Process.pid}) exceeds memory limit (#{rss} bytes > #{@_worker_memory_size} bytes)"
50
+ if rss > @_worker_memory_limit
51
+ logger.warn "#{self}: worker (pid: #{Process.pid}) exceeds memory limit (#{rss} bytes > #{@_worker_memory_limit} bytes)"
50
52
  Unicorn::WorkerKiller.kill_self(logger, @_worker_process_start)
51
53
  end
52
54
  @_worker_check_count = 0
@@ -91,17 +93,19 @@ module Unicorn::WorkerKiller
91
93
  # affect the request.
92
94
  #
93
95
  # @see https://github.com/defunkt/unicorn/blob/master/lib/unicorn/oob_gc.rb#L40
94
- def self.new(app, max_requests = 1024)
96
+ def self.new(app, max_requests_min = 3072, max_requests_max = 4096)
95
97
  ObjectSpace.each_object(Unicorn::HttpServer) do |s|
96
98
  s.extend(self)
97
- s.instance_variable_set(:@_worker_max_requests, max_requests)
98
- s.instance_variable_set(:@_worker_cur_requests, max_requests)
99
+ s.instance_variable_set(:@_worker_max_requests_min, max_requests_min)
100
+ s.instance_variable_set(:@_worker_max_requests_max, max_requests_max)
99
101
  end
100
102
  app # pretend to be Rack middleware since it was in the past
101
103
  end
102
104
 
103
105
  def process_client(client)
104
106
  @_worker_process_start ||= Time.now
107
+ @_worker_cur_requests ||= Random.rand(@_worker_max_requests_min..@_worker_max_requests_max)
108
+ @_worker_max_requests ||= @_worker_cur_requests
105
109
  super(client) # Unicorn::HttpServer#process_client
106
110
 
107
111
  if (@_worker_cur_requests -= 1) <= 0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unicorn-worker-killer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-12-30 00:00:00.000000000 Z
13
+ date: 2012-12-31 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: unicorn