Class: DistributedRateQueue
- Inherits:
-
Object
- Object
- DistributedRateQueue
- Defined in:
- lib/limiter/distributed_rate_queue.rb
Overview
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#lock_duration ⇒ Object
readonly
Returns the value of attribute lock_duration.
-
#lock_manager ⇒ Object
readonly
Returns the value of attribute lock_manager.
Instance Method Summary collapse
-
#initialize(redis_urls:, key:, rate: 60, interval: 60) ⇒ DistributedRateQueue
constructor
A new instance of DistributedRateQueue.
- #shift(&block) ⇒ Object
Constructor Details
#initialize(redis_urls:, key:, rate: 60, interval: 60) ⇒ DistributedRateQueue
Returns a new instance of DistributedRateQueue.
7 8 9 10 11 |
# File 'lib/limiter/distributed_rate_queue.rb', line 7 def initialize(redis_urls:, key:, rate: 60, interval: 60) @lock_duration = ((interval / rate.to_f) * 1000).to_i # Redlock deals with miliseconds @lock_manager = Redlock::Client.new(redis_urls) @key = key end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/limiter/distributed_rate_queue.rb', line 5 def key @key end |
#lock_duration ⇒ Object (readonly)
Returns the value of attribute lock_duration.
5 6 7 |
# File 'lib/limiter/distributed_rate_queue.rb', line 5 def lock_duration @lock_duration end |
#lock_manager ⇒ Object (readonly)
Returns the value of attribute lock_manager.
5 6 7 |
# File 'lib/limiter/distributed_rate_queue.rb', line 5 def lock_manager @lock_manager end |
Instance Method Details
#shift(&block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/limiter/distributed_rate_queue.rb', line 13 def shift(&block) if lock_manager.lock(key, lock_duration) # puts "timestamp: #{Time.now.to_i}" yield else # Logger.log("Lock not acquired, waiting for next turn...", Process.pid) wait_for_next_turn shift(&block) end end |