Sha256: 812514e9f9cbcfb1c431d6e8b5c664cb5f9691e5be9d726bcc826ec485315a0d

Contents?: true

Size: 1.15 KB

Versions: 4

Compression:

Stored size: 1.15 KB

Contents

module Sidekiq
  class Throttler
    module Storage
      ##
      # Stores job executions in a Hash of Arrays.
      class Memory
        include Singleton

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

        ##
        # Number of executions for +key+.
        #
        # @param [String]
        #   Key to fetch count for
        #
        # @return [Fixnum]
        #   Execution count
        def count(key)
          @hash[key].length
        end

        ##
        # Remove entries older than +cutoff+.
        #
        # @param [String] key
        #   The key to prune
        #
        # @param [Time] cutoff
        #   Oldest allowable time
        def prune(key, cutoff)
          @hash[key].reject! { |time| time <= cutoff }
        end

        ##
        # Add a new entry to the hash.
        #
        # @param [String] key
        #   The key to append to
        #
        # @param [Time]
        #   The time to insert
        def append(key, time)
          @hash[key] << time
        end

        def reset
          @hash.clear
        end
      end
    end # Storage
  end # Throttler
end # Sidekiq

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sidekiq-throttler-0.4.1 lib/sidekiq/throttler/storage/memory.rb
sidekiq-throttler-0.4.0 lib/sidekiq/throttler/storage/memory.rb
sidekiq-throttler-0.3.1 lib/sidekiq/throttler/storage/memory.rb
sidekiq-throttler-0.3.0 lib/sidekiq/throttler/storage/memory.rb