Class: OMQ::Routing::Peer

Inherits:
Object
  • Object
show all
Defined in:
lib/omq/routing/peer.rb

Overview

PEER socket routing: bidirectional multi-peer with auto-generated 4-byte routing IDs.

Prepends routing ID on receive. Strips routing ID on send and routes to the identified connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ Peer

Returns a new instance of Peer.

Parameters:

  • engine (Engine)


16
17
18
19
20
21
22
23
24
# File 'lib/omq/routing/peer.rb', line 16

def initialize(engine)
  @engine                     = engine
  @recv_queue                 = Routing.build_queue(engine.options.recv_hwm, :block)
  @connections_by_routing_id  = {}
  @routing_id_by_connection   = {}
  @conn_queues                = {}
  @conn_send_tasks            = {}
  @tasks                      = []
end

Instance Attribute Details

#recv_queueAsync::LimitedQueue (readonly)

Returns:

  • (Async::LimitedQueue)


29
30
31
# File 'lib/omq/routing/peer.rb', line 29

def recv_queue
  @recv_queue
end

Instance Method Details

#connection_added(connection) ⇒ Object

Parameters:

  • connection (Connection)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/omq/routing/peer.rb', line 52

def connection_added(connection)
  routing_id = SecureRandom.bytes(4)
  @connections_by_routing_id[routing_id] = connection
  @routing_id_by_connection[connection]  = routing_id

  task = @engine.start_recv_pump(connection, @recv_queue) { |msg| [routing_id, *msg] }
  @tasks << task if task

  q = Routing.build_queue(@engine.options.send_hwm, :block)
  @conn_queues[connection] = q
  @conn_send_tasks[connection] = ConnSendPump.start(@engine, connection, q, @tasks)
end

#connection_removed(connection) ⇒ Object

Parameters:

  • connection (Connection)


68
69
70
71
72
73
# File 'lib/omq/routing/peer.rb', line 68

def connection_removed(connection)
  routing_id = @routing_id_by_connection.delete(connection)
  @connections_by_routing_id.delete(routing_id) if routing_id
  @conn_queues.delete(connection)
  @conn_send_tasks.delete(connection)&.stop
end

#dequeue_recvArray<String>?

Dequeues the next received message. Blocks until one is available.

Returns:

  • (Array<String>, nil)


36
37
38
# File 'lib/omq/routing/peer.rb', line 36

def dequeue_recv
  @recv_queue.dequeue
end

#enqueue(parts) ⇒ Object

Parameters:

  • parts (Array<String>)


78
79
80
81
82
83
# File 'lib/omq/routing/peer.rb', line 78

def enqueue(parts)
  routing_id = parts.first
  conn = @connections_by_routing_id[routing_id]
  return unless conn
  @conn_queues[conn]&.enqueue(parts[1..])
end

#send_queues_drained?Boolean

True when all per-connection send queues are empty.

Returns:

  • (Boolean)


95
96
97
# File 'lib/omq/routing/peer.rb', line 95

def send_queues_drained?
  @conn_queues.values.all?(&:empty?)
end

#stopObject

Stops all background tasks (send pumps).



87
88
89
90
# File 'lib/omq/routing/peer.rb', line 87

def stop
  @tasks.each(&:stop)
  @tasks.clear
end

#unblock_recvvoid

This method returns an undefined value.

Wakes a blocked #dequeue_recv with a nil sentinel.



45
46
47
# File 'lib/omq/routing/peer.rb', line 45

def unblock_recv
  @recv_queue.enqueue(nil)
end