Class: OMQ::Routing::Pair
- Inherits:
-
Object
- Object
- OMQ::Routing::Pair
- Defined in:
- lib/omq/routing/pair.rb
Overview
PAIR socket routing: exclusive 1-to-1 bidirectional.
Only one peer connection is allowed at a time. The send queue is socket-level (one shared bounded queue), and a single send pump fiber drains it into the connected peer. On disconnect, the in-flight batch is dropped (matching libzmq).
Instance Attribute Summary collapse
- #recv_queue ⇒ Async::LimitedQueue readonly
Instance Method Summary collapse
- #connection_added(connection) ⇒ Object
- #connection_removed(connection) ⇒ Object
-
#dequeue_recv ⇒ Array<String>?
Dequeues the next received message.
- #enqueue(parts) ⇒ Object
-
#initialize(engine) ⇒ Pair
constructor
A new instance of Pair.
-
#send_queues_drained? ⇒ Boolean
True when the shared send queue is empty.
-
#stop ⇒ void
Stops all background tasks.
-
#unblock_recv ⇒ void
Wakes a blocked #dequeue_recv with a nil sentinel.
Constructor Details
#initialize(engine) ⇒ Pair
Returns a new instance of Pair.
20 21 22 23 24 25 26 27 |
# File 'lib/omq/routing/pair.rb', line 20 def initialize(engine) @engine = engine @connection = nil @recv_queue = Routing.build_queue(engine..recv_hwm, :block) @send_queue = Routing.build_queue(engine..send_hwm, :block) @send_pump = nil @tasks = [] end |
Instance Attribute Details
#recv_queue ⇒ Async::LimitedQueue (readonly)
15 16 17 |
# File 'lib/omq/routing/pair.rb', line 15 def recv_queue @recv_queue end |
Instance Method Details
#connection_added(connection) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/omq/routing/pair.rb', line 51 def connection_added(connection) raise "PAIR allows only one peer" if @connection @connection = connection task = @engine.start_recv_pump(connection, @recv_queue) @tasks << task if task unless connection.is_a?(Transport::Inproc::DirectPipe) start_send_pump(connection) end end |
#connection_removed(connection) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/omq/routing/pair.rb', line 66 def connection_removed(connection) if @connection == connection @connection = nil @send_pump&.stop @send_pump = nil end end |
#dequeue_recv ⇒ Array<String>?
Dequeues the next received message. Blocks until one is available.
34 35 36 |
# File 'lib/omq/routing/pair.rb', line 34 def dequeue_recv @recv_queue.dequeue end |
#enqueue(parts) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/omq/routing/pair.rb', line 77 def enqueue(parts) conn = @connection if conn.is_a?(Transport::Inproc::DirectPipe) && conn.direct_recv_queue conn.(parts) else @send_queue.enqueue(parts) end end |
#send_queues_drained? ⇒ Boolean
Returns true when the shared send queue is empty.
99 100 101 |
# File 'lib/omq/routing/pair.rb', line 99 def send_queues_drained? @send_queue.empty? end |
#stop ⇒ void
This method returns an undefined value.
Stops all background tasks.
91 92 93 94 |
# File 'lib/omq/routing/pair.rb', line 91 def stop @tasks.each(&:stop) @tasks.clear end |
#unblock_recv ⇒ void
This method returns an undefined value.
Wakes a blocked #dequeue_recv with a nil sentinel.
43 44 45 |
# File 'lib/omq/routing/pair.rb', line 43 def unblock_recv @recv_queue.enqueue(nil) end |