Class: OMQ::Routing::Peer
- Inherits:
-
Object
- Object
- OMQ::Routing::Peer
- Includes:
- FairRecv
- 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
- #recv_queue ⇒ FairQueue readonly
Instance Method Summary collapse
- #connection_added(connection) ⇒ Object
- #connection_removed(connection) ⇒ Object
- #enqueue(parts) ⇒ Object
-
#initialize(engine) ⇒ Peer
constructor
A new instance of Peer.
-
#send_queues_drained? ⇒ Boolean
True when all per-connection send queues are empty.
-
#stop ⇒ Object
Stops all background tasks (send pumps).
Constructor Details
#initialize(engine) ⇒ Peer
Returns a new instance of Peer.
18 19 20 21 22 23 24 25 26 |
# File 'lib/omq/routing/peer.rb', line 18 def initialize(engine) @engine = engine @recv_queue = FairQueue.new @connections_by_routing_id = {} @routing_id_by_connection = {} @conn_queues = {} @conn_send_tasks = {} @tasks = [] end |
Instance Attribute Details
#recv_queue ⇒ FairQueue (readonly)
31 32 33 |
# File 'lib/omq/routing/peer.rb', line 31 def recv_queue @recv_queue end |
Instance Method Details
#connection_added(connection) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/omq/routing/peer.rb', line 35 def connection_added(connection) routing_id = SecureRandom.bytes(4) @connections_by_routing_id[routing_id] = connection @routing_id_by_connection[connection] = routing_id add_fair_recv_connection(connection) { |msg| [routing_id, *msg] } q = Routing.build_queue(@engine..send_hwm, :block) @conn_queues[connection] = q @conn_send_tasks[connection] = ConnSendPump.start(@engine, connection, q, @tasks) end |
#connection_removed(connection) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/omq/routing/peer.rb', line 50 def connection_removed(connection) routing_id = @routing_id_by_connection.delete(connection) @connections_by_routing_id.delete(routing_id) if routing_id @recv_queue.remove_queue(connection) @conn_queues.delete(connection) @conn_send_tasks.delete(connection)&.stop end |
#enqueue(parts) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/omq/routing/peer.rb', line 61 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.
78 79 80 |
# File 'lib/omq/routing/peer.rb', line 78 def send_queues_drained? @conn_queues.values.all?(&:empty?) end |
#stop ⇒ Object
Stops all background tasks (send pumps).
70 71 72 73 |
# File 'lib/omq/routing/peer.rb', line 70 def stop @tasks.each(&:stop) @tasks.clear end |