Class: SeigenWatchdog::Limiters::Counter

Inherits:
Base
  • Object
show all
Defined in:
lib/seigen_watchdog/limiters/counter.rb

Overview

Limiter based on iteration count

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#started, #stopped

Constructor Details

#initialize(max_count:, initial: 0) ⇒ Counter

Returns a new instance of Counter.



16
17
18
19
20
21
# File 'lib/seigen_watchdog/limiters/counter.rb', line 16

def initialize(max_count:, initial: 0)
  super()
  @max_count = max_count
  @count = initial
  @mutex = Mutex.new
end

Instance Attribute Details

#max_countObject (readonly)

Returns the value of attribute max_count.



11
12
13
# File 'lib/seigen_watchdog/limiters/counter.rb', line 11

def max_count
  @max_count
end

Instance Method Details

#decrement(count = 1) ⇒ Object

Decrements the counter by the specified amount



33
34
35
# File 'lib/seigen_watchdog/limiters/counter.rb', line 33

def decrement(count = 1)
  @mutex.synchronize { @count -= count }
end

#exceeded?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/seigen_watchdog/limiters/counter.rb', line 45

def exceeded?
  @mutex.synchronize { @count >= @max_count }
end

#increment(count = 1) ⇒ Object

Increments the counter by the specified amount



26
27
28
# File 'lib/seigen_watchdog/limiters/counter.rb', line 26

def increment(count = 1)
  @mutex.synchronize { @count += count }
end

#reset(initial = 0) ⇒ Object

Resets the counter to the specified initial value



40
41
42
# File 'lib/seigen_watchdog/limiters/counter.rb', line 40

def reset(initial = 0)
  @mutex.synchronize { @count = initial }
end