Class: Sidekiq::Throttled::Strategy::Threshold

Inherits:
Object
  • Object
show all
Defined in:
lib/sidekiq/throttled/strategy/threshold.rb

Overview

TODO:

Use redis TIME command instead of sending current timestamp from sidekiq manager. See: redis.io/commands/time

Threshold throttling strategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_key, opts) ⇒ Threshold

Returns a new instance of Threshold.

Parameters:

  • base_key (#to_s)
  • opts (Hash)

Options Hash (opts):

  • :limit (#to_i)

    Amount of jobs allowed per period

  • :period (#to_f)

    Period in seconds



43
44
45
46
47
48
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 43

def initialize(base_key, opts)
  @key    = "#{base_key}:threshold".freeze
  @keys   = [@key]
  @limit  = opts.fetch(:limit).to_i
  @period = opts.fetch(:period).to_f
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



33
34
35
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 33

def limit
  @limit
end

#periodObject (readonly)

Returns the value of attribute period.



37
38
39
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 37

def period
  @period
end

Instance Method Details

#countInteger

Returns Current count of jobs.

Returns:

  • (Integer)

    Current count of jobs



56
57
58
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 56

def count
  Sidekiq.redis { |conn| conn.llen(@key) }.to_i
end

#reset!void

This method returns an undefined value.

Resets count of jobs



62
63
64
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 62

def reset!
  Sidekiq.redis { |conn| conn.del(@key) }
end

#throttled?Boolean

Returns whenever job is throttled or not.

Returns:

  • (Boolean)

    whenever job is throttled or not



51
52
53
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 51

def throttled?
  1 == SCRIPT.eval(@keys, [@limit, @period, Time.now.to_f])
end