Class: Sidekiq::Throttler::Storage::Memory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/sidekiq/throttler/storage/memory.rb

Overview

Stores job executions in a Hash of Arrays.

Instance Method Summary collapse

Constructor Details

#initializeMemory

Returns a new instance of Memory.



9
10
11
# File 'lib/sidekiq/throttler/storage/memory.rb', line 9

def initialize
  @hash = Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#append(key, time) ⇒ Object

Add a new entry to the hash.

Parameters:

  • key (String)

    The key to append to

  • time (Time)

    The time to insert



45
46
47
# File 'lib/sidekiq/throttler/storage/memory.rb', line 45

def append(key, time)
  @hash[key] << time
end

#count(key) ⇒ Fixnum

Number of executions for +key+.

Parameters:

  • key (String)

    Key to fetch count for

Returns:

  • (Fixnum)

    Execution count



21
22
23
# File 'lib/sidekiq/throttler/storage/memory.rb', line 21

def count(key)
  @hash[key].length
end

#prune(key, cutoff) ⇒ Object

Remove entries older than +cutoff+.

Parameters:

  • key (String)

    The key to prune

  • cutoff (Time)

    Oldest allowable time



33
34
35
# File 'lib/sidekiq/throttler/storage/memory.rb', line 33

def prune(key, cutoff)
  @hash[key].reject! { |time| time <= cutoff }
end

#resetObject



49
50
51
# File 'lib/sidekiq/throttler/storage/memory.rb', line 49

def reset
  @hash.clear
end