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(strategy_key, opts) ⇒ Threshold

Returns a new instance of Threshold.

Parameters:

  • strategy_key (#to_s)
  • opts (Hash)

Options Hash (opts):

  • :limit (#to_i)

    Amount of jobs allowed per period

  • :period (#to_f)

    Period in seconds



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

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

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

#periodObject (readonly)

Returns the value of attribute period.



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

def period
  @period
end

Instance Method Details

#count(*job_args) ⇒ Integer

Returns Current count of jobs.

Returns:

  • (Integer)

    Current count of jobs



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

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

#dynamic_keys?Boolean

Returns:

  • (Boolean)


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

def dynamic_keys?
  @key_suffix
end

#reset!(*job_args) ⇒ void

This method returns an undefined value.

Resets count of jobs



67
68
69
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 67

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

#throttled?(*job_args) ⇒ Boolean

Returns whenever job is throttled or not.

Returns:

  • (Boolean)

    whenever job is throttled or not



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

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