Module: OMQ::Engine::Heartbeat

Defined in:
lib/omq/engine/heartbeat.rb

Overview

Spawns a heartbeat task for a connection.

Sends PING frames at interval seconds and closes the connection if no traffic is seen within timeout seconds.

Class Method Summary collapse

Class Method Details

.start(parent, conn, options, tasks) ⇒ Object

Parameters:

  • parent (Async::Task, Async::Barrier)

    parent to spawn under

  • conn (Connection)
  • options (Options)
  • tasks (Array)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/omq/engine/heartbeat.rb', line 16

def self.start(parent, conn, options, tasks)
  interval = options.heartbeat_interval
  return unless interval

  ttl     = options.heartbeat_ttl     || interval
  timeout = options.heartbeat_timeout || interval
  conn.touch_heartbeat

  tasks << parent.async(transient: true, annotation: "heartbeat") do
    loop do
      sleep interval
      conn.send_command(Protocol::ZMTP::Codec::Command.ping(ttl: ttl, context: "".b))
      if conn.heartbeat_expired?(timeout)
        conn.close
        break
      end
    end
  rescue Async::Stop, Async::Cancel
  rescue *CONNECTION_LOST
    # connection closed
  end
end