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 ⇒ Integer
Current count of jobs.
-
#initialize(base_key, opts) ⇒ Threshold
constructor
A new instance of Threshold.
-
#reset! ⇒ void
Resets count of jobs.
-
#throttled? ⇒ Boolean
Whenever job is throttled or not.
Constructor Details
#initialize(base_key, opts) ⇒ Threshold
Returns a new instance of Threshold.
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
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
33 34 35 |
# File 'lib/sidekiq/throttled/strategy/threshold.rb', line 33 def limit @limit end |
#period ⇒ Object (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
#count ⇒ Integer
Returns 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.
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 |