Class: Sidekiq::Processor::Counter
- Inherits:
-
Object
- Object
- Sidekiq::Processor::Counter
- Defined in:
- lib/sidekiq/processor.rb
Overview
Ruby doesn’t provide atomic counters out of the box so we’ll implement something simple ourselves. bugs.ruby-lang.org/issues/14706
Instance Method Summary collapse
- #incr(amount = 1) ⇒ Object
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
- #reset ⇒ Object
Constructor Details
#initialize ⇒ Counter
Returns a new instance of Counter.
192 193 194 195 |
# File 'lib/sidekiq/processor.rb', line 192 def initialize @value = 0 @lock = Mutex.new end |
Instance Method Details
#incr(amount = 1) ⇒ Object
197 198 199 |
# File 'lib/sidekiq/processor.rb', line 197 def incr(amount=1) @lock.synchronize { @value = @value + amount } end |
#reset ⇒ Object
201 202 203 |
# File 'lib/sidekiq/processor.rb', line 201 def reset @lock.synchronize { val = @value; @value = 0; val } end |