Class: WorkerKiller::CountLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/worker_killer/count_limiter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min: 3072, max: 4096, verbose: false) ⇒ CountLimiter

Returns a new instance of CountLimiter.



6
7
8
9
10
11
12
# File 'lib/worker_killer/count_limiter.rb', line 6

def initialize(min: 3072, max: 4096, verbose: false)
  @min = min
  @max = max
  @limit = nil
  @left = nil
  @verbose = verbose
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



4
5
6
# File 'lib/worker_killer/count_limiter.rb', line 4

def left
  @left
end

#limitObject (readonly)

Returns the value of attribute limit.



4
5
6
# File 'lib/worker_killer/count_limiter.rb', line 4

def limit
  @limit
end

#maxObject (readonly)

Returns the value of attribute max.



4
5
6
# File 'lib/worker_killer/count_limiter.rb', line 4

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



4
5
6
# File 'lib/worker_killer/count_limiter.rb', line 4

def min
  @min
end

#started_atObject (readonly)

Returns the value of attribute started_at.



4
5
6
# File 'lib/worker_killer/count_limiter.rb', line 4

def started_at
  @started_at
end

Instance Method Details

#checkObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/worker_killer/count_limiter.rb', line 14

def check
  # initialize limits on first check
  if @limit.nil?
    @limit = min + WorkerKiller.randomize(max - min + 1)
    @left = @limit
  end

  return nil if @limit < 1

  @started_at ||= Time.now

  if @verbose
    logger.info "#{self.class}: worker (pid: #{Process.pid}) has #{@left} left before being limited"
  end

  return false if (@left -= 1) > 0

  logger.warn "#{self.class}: worker (pid: #{Process.pid}) exceeds max number of requests (limit: #{@limit})"

  true
end

#loggerObject



36
37
38
# File 'lib/worker_killer/count_limiter.rb', line 36

def logger
  WorkerKiller.configuration.logger
end