Class: Sidekiq::Throttled::Strategy::Threshold
- Inherits:
-
Object
- Object
- Sidekiq::Throttled::Strategy::Threshold
- 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
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#period ⇒ Object
readonly
Returns the value of attribute period.
Instance Method Summary collapse
-
#count(*job_args) ⇒ Integer
Current count of jobs.
- #dynamic_keys? ⇒ Boolean
-
#initialize(strategy_key, opts) ⇒ Threshold
constructor
A new instance of Threshold.
-
#reset!(*job_args) ⇒ void
Resets count of jobs.
-
#throttled?(*job_args) ⇒ Boolean
Whenever job is throttled or not.
Constructor Details
#initialize(strategy_key, opts) ⇒ Threshold
Returns a new instance of Threshold.
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
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
34 35 36 |
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 34 def limit @limit end |
#period ⇒ Object (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.
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
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.
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 |