Class: OMQ::Routing::Req
- Inherits:
-
Object
- Object
- OMQ::Routing::Req
- Includes:
- RoundRobin
- Defined in:
- lib/omq/routing/req.rb
Overview
REQ socket routing: round-robin send with strict send/recv alternation.
REQ prepends an empty delimiter frame on send and strips it on receive.
Constant Summary collapse
- EMPTY_BINARY =
Shared frozen empty binary string to avoid repeated allocations.
::Protocol::ZMTP::Codec::EMPTY_BINARY
Constants included from RoundRobin
OMQ::Routing::RoundRobin::BATCH_BYTE_CAP, OMQ::Routing::RoundRobin::BATCH_MSG_CAP
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) ⇒ Req
constructor
A new instance of Req.
-
#stop ⇒ void
Stops all background tasks.
-
#unblock_recv ⇒ void
Wakes a blocked #dequeue_recv with a nil sentinel.
Methods included from RoundRobin
Constructor Details
#initialize(engine) ⇒ Req
Returns a new instance of Req.
23 24 25 26 27 28 29 |
# File 'lib/omq/routing/req.rb', line 23 def initialize(engine) @engine = engine @recv_queue = Routing.build_queue(engine..recv_hwm, :block) @tasks = [] @state = :ready # :ready or :waiting_reply init_round_robin(engine) end |
Instance Attribute Details
#recv_queue ⇒ Async::LimitedQueue (readonly)
18 19 20 |
# File 'lib/omq/routing/req.rb', line 18 def recv_queue @recv_queue end |
Instance Method Details
#connection_added(connection) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/omq/routing/req.rb', line 52 def connection_added(connection) task = @engine.start_recv_pump(connection, @recv_queue) do |msg| @state = :ready msg.first&.empty? ? msg[1..] : msg end @tasks << task if task add_round_robin_send_connection(connection) end |
#connection_removed(connection) ⇒ Object
65 66 67 68 |
# File 'lib/omq/routing/req.rb', line 65 def connection_removed(connection) @connections.delete(connection) remove_round_robin_send_connection(connection) end |
#dequeue_recv ⇒ Array<String>?
Dequeues the next received message. Blocks until one is available.
36 37 38 |
# File 'lib/omq/routing/req.rb', line 36 def dequeue_recv @recv_queue.dequeue end |
#enqueue(parts) ⇒ Object
73 74 75 76 77 |
# File 'lib/omq/routing/req.rb', line 73 def enqueue(parts) raise SocketError, "REQ socket expects send/recv/send/recv order" unless @state == :ready @state = :waiting_reply enqueue_round_robin(parts) end |
#stop ⇒ void
This method returns an undefined value.
Stops all background tasks.
84 85 86 87 |
# File 'lib/omq/routing/req.rb', line 84 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.
45 46 47 |
# File 'lib/omq/routing/req.rb', line 45 def unblock_recv @recv_queue.enqueue(nil) end |