Class: Sidekiq::Processor::Counter

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeCounter

Returns a new instance of Counter.


200
201
202
203
# File 'lib/sidekiq/processor.rb', line 200

def initialize
  @value = 0
  @lock = Mutex.new
end

Instance Method Details

#incr(amount = 1) ⇒ Object


205
206
207
# File 'lib/sidekiq/processor.rb', line 205

def incr(amount = 1)
  @lock.synchronize { @value += amount }
end

#resetObject


209
210
211
212
213
214
215
# File 'lib/sidekiq/processor.rb', line 209

def reset
  @lock.synchronize {
    val = @value
    @value = 0
    val
  }
end