Class: EventMachine::ThrottledQueue

Inherits:
Queue
  • Object
show all
Defined in:
lib/em/throttled_queue.rb,
lib/em/throttled_queue/version.rb

Overview

TODO:

Add a maximum size-limit on amount of items in queue.

An EM::Queue with a rate-limit applied to it. This is useful if you wish to consume items at a limited pace.

Constant Summary collapse

VERSION =

Gem version, following Semantic Versioning since v1.0.2

See Also:

[1, 1, 1].join('.')

Instance Method Summary collapse

Constructor Details

#initialize(limit, refresh_interval = 1) ⇒ ThrottledQueue

Create a new rate-limited queue.

Examples:


EM::ThrottledQueue.new(10)
# => allows maximum 10 deqs every second

EM::ThrottledQueue.new(5, 10)
# => allows maximum 5 deqs every 10 seconds

24
25
26
27
28
29
30
31
32
# File 'lib/em/throttled_queue.rb', line 24

def initialize(limit, refresh_interval = 1)
  @credits = @limit = limit.to_i
  @ticker  = EM::add_periodic_timer(refresh_interval) do
    @credits = @limit
    scheduled_dequeue
  end
  
  super()
end

Instance Method Details

#pop {|item| ... } ⇒ ThrottledQueue

Note:

The given block is executed within the reactor thread.

Pop an item off the queue as soon as conditions allow, passing it to the given block.

Yields:

  • (item)

    callback to execute when item is popped off

Yield Parameters:

  • item

    an item from the queue

See Also:


42
43
44
# File 'lib/em/throttled_queue.rb', line 42

def pop(&block)
  interact { @popq.push(block) }
end

#push(*items) ⇒ ThrottledQueue

Note:

This call is thread-safe.

Push items onto the queue (from within the reactor thread) asap.


51
52
53
# File 'lib/em/throttled_queue.rb', line 51

def push(*items)
  interact { @items.push(*items) }
end