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. https://bugs.ruby-lang.org/issues/14706

Instance Method Summary collapse

Constructor Details

#initializeCounter

Returns a new instance of Counter.



235
236
237
238
# File 'lib/sidekiq/processor.rb', line 235

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

Instance Method Details

#incr(amount = 1) ⇒ Object



240
241
242
# File 'lib/sidekiq/processor.rb', line 240

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

#resetObject



244
245
246
247
248
249
250
# File 'lib/sidekiq/processor.rb', line 244

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