Class: Vines::Services::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/vines/services/throttle.rb

Overview

Send many outgoing stanzas to the server at a sustained rate that won’t cause the server to shutdown the component’s stream with a policy_violation error.

Instance Method Summary collapse

Constructor Details

#initialize(stream, delay = 0.1) ⇒ Throttle

Returns a new instance of Throttle.



9
10
11
# File 'lib/vines/services/throttle.rb', line 9

def initialize(stream, delay=0.1)
  @stream, @delay = stream, delay
end

Instance Method Details

#async_send(nodes) ⇒ Object

Send the nodes to the server at a constant rate. The nodes are sent asynchronously, so this method returns immediately.



15
16
17
18
19
20
21
22
23
# File 'lib/vines/services/throttle.rb', line 15

def async_send(nodes)
  timer = EM::PeriodicTimer.new(@delay) do
    if node = nodes.shift
      @stream.write(node)
    else
      timer.cancel
    end
  end
end