omq 0.29.1 → 0.30.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +181 -0
- data/README.md +3 -3
- data/lib/omq/constants.rb +9 -1
- data/lib/omq/drop_queue.rb +17 -0
- data/lib/omq/engine/connection_lifecycle.rb +34 -6
- data/lib/omq/engine/endpoints.rb +107 -0
- data/lib/omq/engine/heartbeat.rb +157 -15
- data/lib/omq/engine/monitoring.rb +95 -0
- data/lib/omq/engine/pump_tasks.rb +102 -0
- data/lib/omq/engine/reconnect.rb +10 -9
- data/lib/omq/engine/recv_pump.rb +18 -4
- data/lib/omq/engine/socket_lifecycle.rb +18 -4
- data/lib/omq/engine.rb +255 -304
- data/lib/omq/monitor_queue.rb +74 -0
- data/lib/omq/options.rb +78 -6
- data/lib/omq/routing/channel.rb +11 -5
- data/lib/omq/routing/client.rb +3 -1
- data/lib/omq/routing/closable_queues.rb +28 -0
- data/lib/omq/routing/conn_send_pump.rb +11 -9
- data/lib/omq/routing/dealer.rb +1 -0
- data/lib/omq/routing/dish.rb +5 -3
- data/lib/omq/routing/fan_out.rb +29 -21
- data/lib/omq/routing/gather.rb +4 -1
- data/lib/omq/routing/identity_routing.rb +136 -0
- data/lib/omq/routing/pair.rb +21 -12
- data/lib/omq/routing/peer.rb +30 -30
- data/lib/omq/routing/prefix_set.rb +239 -0
- data/lib/omq/routing/pub.rb +2 -1
- data/lib/omq/routing/pull.rb +2 -0
- data/lib/omq/routing/push.rb +1 -0
- data/lib/omq/routing/radio.rb +65 -54
- data/lib/omq/routing/rep.rb +4 -2
- data/lib/omq/routing/req.rb +8 -0
- data/lib/omq/routing/round_robin.rb +22 -17
- data/lib/omq/routing/router.rb +46 -28
- data/lib/omq/routing/scatter.rb +1 -0
- data/lib/omq/routing/server.rb +21 -25
- data/lib/omq/routing/sub.rb +26 -7
- data/lib/omq/routing/xpub.rb +25 -2
- data/lib/omq/routing/xsub.rb +78 -29
- data/lib/omq/routing.rb +52 -2
- data/lib/omq/socket.rb +4 -1
- data/lib/omq/transport/accept_loop.rb +60 -0
- data/lib/omq/transport/compressed_connection.rb +6 -0
- data/lib/omq/transport/inproc/pipe.rb +7 -0
- data/lib/omq/transport/inproc.rb +27 -33
- data/lib/omq/transport/ipc.rb +105 -7
- data/lib/omq/transport/tcp.rb +8 -5
- data/lib/omq/transport/udp.rb +92 -17
- data/lib/omq/version.rb +1 -1
- data/lib/omq.rb +2 -0
- metadata +11 -9
data/lib/omq/engine.rb
CHANGED
|
@@ -8,6 +8,9 @@ require_relative "engine/reconnect"
|
|
|
8
8
|
require_relative "engine/connection_lifecycle"
|
|
9
9
|
require_relative "engine/socket_lifecycle"
|
|
10
10
|
require_relative "engine/maintenance"
|
|
11
|
+
require_relative "engine/endpoints"
|
|
12
|
+
require_relative "engine/monitoring"
|
|
13
|
+
require_relative "engine/pump_tasks"
|
|
11
14
|
|
|
12
15
|
module OMQ
|
|
13
16
|
# Per-socket orchestrator.
|
|
@@ -16,6 +19,11 @@ module OMQ
|
|
|
16
19
|
# OMQ::Socket instance. Each socket type creates one Engine.
|
|
17
20
|
#
|
|
18
21
|
class Engine
|
|
22
|
+
include Endpoints
|
|
23
|
+
include Monitoring
|
|
24
|
+
include PumpTasks
|
|
25
|
+
|
|
26
|
+
|
|
19
27
|
# Scheme → transport module registry.
|
|
20
28
|
# Plugins add entries via +Engine.transports["scheme"] = MyTransport+.
|
|
21
29
|
#
|
|
@@ -25,6 +33,21 @@ module OMQ
|
|
|
25
33
|
class << self
|
|
26
34
|
# @return [Hash{String => Module}] registered transports
|
|
27
35
|
attr_reader :transports
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# The URI {#bind} and {#connect} return for +endpoint+. A transport
|
|
39
|
+
# with +.endpoint_uri+ builds it: IPC keeps the @ of abstract names.
|
|
40
|
+
# The native backends use it for the endpoints they resolve.
|
|
41
|
+
#
|
|
42
|
+
# @param endpoint [String]
|
|
43
|
+
# @return [URI::Generic]
|
|
44
|
+
#
|
|
45
|
+
def endpoint_uri(endpoint)
|
|
46
|
+
transport = @transports[endpoint[/\A[^:]+/]]
|
|
47
|
+
return transport.endpoint_uri(endpoint) if transport.respond_to?(:endpoint_uri)
|
|
48
|
+
|
|
49
|
+
URI.parse(endpoint)
|
|
50
|
+
end
|
|
28
51
|
end
|
|
29
52
|
|
|
30
53
|
|
|
@@ -60,7 +83,7 @@ module OMQ
|
|
|
60
83
|
|
|
61
84
|
|
|
62
85
|
# @!attribute [w] monitor_queue
|
|
63
|
-
# @param value [
|
|
86
|
+
# @param value [MonitorQueue, nil] queue for monitor events
|
|
64
87
|
#
|
|
65
88
|
attr_writer :monitor_queue
|
|
66
89
|
|
|
@@ -86,21 +109,58 @@ module OMQ
|
|
|
86
109
|
@routing = nil
|
|
87
110
|
@connections = {} # connection => ConnectionLifecycle
|
|
88
111
|
@dialers = {} # endpoint => Dialer (reconnect intent + connect logic)
|
|
112
|
+
@dialer_barriers = {} # endpoint => Async::Barrier (its reconnect + connection tasks)
|
|
89
113
|
@listeners = {} # endpoint => Listener
|
|
114
|
+
@bind_aliases = {} # endpoint as passed to #bind => listener endpoint
|
|
90
115
|
@lifecycle = SocketLifecycle.new
|
|
91
116
|
@fatal_error = nil
|
|
92
117
|
@monitor_queue = nil
|
|
93
118
|
@verbose_monitor = false
|
|
119
|
+
@drain_signal = nil # set while #close waits for send queues
|
|
120
|
+
@batches_sending = 0 # batches pumps took off a queue and haven't flushed
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
# @return [Async::Promise] resolves with the first peer that connects
|
|
125
|
+
#
|
|
126
|
+
def peer_connected
|
|
127
|
+
@lifecycle.peer_connected
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# @return [Async::Promise] resolves once every peer is gone
|
|
132
|
+
#
|
|
133
|
+
def all_peers_gone
|
|
134
|
+
@lifecycle.all_peers_gone
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
# @return [Async::Task, nil] the task the socket's tasks run under
|
|
139
|
+
#
|
|
140
|
+
def parent_task
|
|
141
|
+
@lifecycle.parent_task
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
# @return [Async::Barrier, nil] the socket-level barrier
|
|
146
|
+
#
|
|
147
|
+
def barrier
|
|
148
|
+
@lifecycle.barrier
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
# @return [Boolean] true once the socket is closed
|
|
153
|
+
#
|
|
154
|
+
def closed?
|
|
155
|
+
@lifecycle.closed?
|
|
94
156
|
end
|
|
95
157
|
|
|
96
158
|
|
|
97
|
-
#
|
|
98
|
-
|
|
99
|
-
def
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
def closed? = @lifecycle.closed?
|
|
103
|
-
def on_io_thread? = @lifecycle.on_io_thread
|
|
159
|
+
# @return [Boolean] true if the socket runs on the shared IO thread
|
|
160
|
+
#
|
|
161
|
+
def on_io_thread?
|
|
162
|
+
@lifecycle.on_io_thread
|
|
163
|
+
end
|
|
104
164
|
|
|
105
165
|
|
|
106
166
|
# Enables or disables auto-reconnect for dropped connections.
|
|
@@ -155,34 +215,6 @@ module OMQ
|
|
|
155
215
|
end
|
|
156
216
|
|
|
157
217
|
|
|
158
|
-
# Returns the transport object (Dialer or Listener) for an endpoint.
|
|
159
|
-
# Used by {ConnectionLifecycle#ready!} to call +#wrap_connection+.
|
|
160
|
-
#
|
|
161
|
-
# @param endpoint [String]
|
|
162
|
-
# @return [Dialer, Listener, nil]
|
|
163
|
-
#
|
|
164
|
-
def transport_object_for(endpoint)
|
|
165
|
-
@dialers[endpoint] || @listeners[endpoint]
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
# Spawns a ruby:// reconnect retry task under the socket's parent task.
|
|
170
|
-
#
|
|
171
|
-
# @param endpoint [String]
|
|
172
|
-
# @yield [interval] the retry loop body
|
|
173
|
-
#
|
|
174
|
-
def spawn_inproc_retry(endpoint)
|
|
175
|
-
ri = @options.reconnect_interval
|
|
176
|
-
ivl = ri.is_a?(Range) ? ri.begin : ri
|
|
177
|
-
ann = "inproc reconnect #{endpoint}"
|
|
178
|
-
|
|
179
|
-
@lifecycle.barrier.async(transient: true, annotation: ann) do
|
|
180
|
-
yield ivl
|
|
181
|
-
rescue Async::Stop, Async::Cancel
|
|
182
|
-
end
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
|
|
186
218
|
# Binds to an endpoint.
|
|
187
219
|
#
|
|
188
220
|
# @param endpoint [String] e.g. "tcp://127.0.0.1:5555", "ruby://foo"
|
|
@@ -198,8 +230,9 @@ module OMQ
|
|
|
198
230
|
start_accept_loops(listener)
|
|
199
231
|
|
|
200
232
|
@listeners[listener.endpoint] = listener
|
|
233
|
+
remember_bind_alias(endpoint, listener.endpoint)
|
|
201
234
|
emit_monitor_event(:listening, endpoint: listener.endpoint)
|
|
202
|
-
|
|
235
|
+
Engine.endpoint_uri(listener.endpoint)
|
|
203
236
|
rescue => error
|
|
204
237
|
emit_monitor_event(:bind_failed, endpoint: endpoint, detail: { error: error })
|
|
205
238
|
raise
|
|
@@ -217,24 +250,22 @@ module OMQ
|
|
|
217
250
|
endpoint, transport = resolve_endpoint(endpoint)
|
|
218
251
|
validate_endpoint!(endpoint, transport)
|
|
219
252
|
|
|
220
|
-
dialer
|
|
253
|
+
dialer = transport.dialer(endpoint, self, **opts)
|
|
254
|
+
@dialers[endpoint] = dialer
|
|
221
255
|
|
|
222
256
|
if endpoint.start_with?("ruby://")
|
|
223
|
-
|
|
224
|
-
dialer.connect
|
|
225
|
-
@dialers[endpoint] = dialer
|
|
257
|
+
connect_inproc(endpoint, dialer)
|
|
226
258
|
else
|
|
227
|
-
@dialers[endpoint] = dialer
|
|
228
259
|
emit_monitor_event(:connect_delayed, endpoint: endpoint)
|
|
229
260
|
schedule_reconnect(endpoint, delay: 0)
|
|
230
261
|
end
|
|
231
262
|
|
|
232
|
-
|
|
263
|
+
Engine.endpoint_uri(endpoint)
|
|
233
264
|
end
|
|
234
265
|
|
|
235
266
|
|
|
236
|
-
# Disconnects from an endpoint.
|
|
237
|
-
#
|
|
267
|
+
# Disconnects from an endpoint. Stops its reconnect loop and any
|
|
268
|
+
# handshake in progress, then closes its connections.
|
|
238
269
|
#
|
|
239
270
|
# @param endpoint [String]
|
|
240
271
|
# @return [void]
|
|
@@ -242,20 +273,26 @@ module OMQ
|
|
|
242
273
|
def disconnect(endpoint)
|
|
243
274
|
endpoint = canonical_endpoint(endpoint)
|
|
244
275
|
@dialers.delete(endpoint)
|
|
276
|
+
@dialer_barriers.delete(endpoint)&.stop
|
|
245
277
|
close_connections_at(endpoint)
|
|
278
|
+
drop_route(endpoint)
|
|
246
279
|
end
|
|
247
280
|
|
|
248
281
|
|
|
249
282
|
# Unbinds from an endpoint. Stops the listener and closes all
|
|
250
|
-
# connections that were accepted on it.
|
|
283
|
+
# connections that were accepted on it. Accepts the resolved
|
|
284
|
+
# endpoint (as returned by {#bind}) or the string passed to
|
|
285
|
+
# {#bind}, e.g. +tcp://localhost:5555+.
|
|
251
286
|
#
|
|
252
287
|
# @param endpoint [String]
|
|
253
288
|
# @return [void]
|
|
254
289
|
#
|
|
255
290
|
def unbind(endpoint)
|
|
256
291
|
endpoint = canonical_endpoint(endpoint)
|
|
292
|
+
endpoint = @bind_aliases.fetch(endpoint, endpoint)
|
|
257
293
|
listener = @listeners.delete(endpoint) or return
|
|
258
294
|
|
|
295
|
+
@bind_aliases.delete_if { |_, bound| bound == endpoint }
|
|
259
296
|
listener.stop
|
|
260
297
|
close_connections_at(endpoint)
|
|
261
298
|
end
|
|
@@ -279,8 +316,16 @@ module OMQ
|
|
|
279
316
|
# @return [void]
|
|
280
317
|
#
|
|
281
318
|
def handle_connected(io, endpoint: nil)
|
|
319
|
+
barrier = @dialer_barriers[endpoint]
|
|
320
|
+
|
|
321
|
+
# Disconnected (or closed) while the dial was in flight.
|
|
322
|
+
unless barrier && @dialers.key?(endpoint)
|
|
323
|
+
io.close rescue nil
|
|
324
|
+
return
|
|
325
|
+
end
|
|
326
|
+
|
|
282
327
|
emit_monitor_event(:connected, endpoint: endpoint)
|
|
283
|
-
spawn_connection(io, as_server: false, endpoint: endpoint)
|
|
328
|
+
spawn_connection(io, as_server: false, endpoint: endpoint, parent: barrier)
|
|
284
329
|
end
|
|
285
330
|
|
|
286
331
|
|
|
@@ -288,20 +333,82 @@ module OMQ
|
|
|
288
333
|
# Skips ZMTP handshake — just registers with routing strategy.
|
|
289
334
|
#
|
|
290
335
|
# @param pipe [Transport::Inproc::Pipe]
|
|
336
|
+
# @return [Boolean] false if the routing strategy refused the peer
|
|
337
|
+
#
|
|
338
|
+
def connection_ready(pipe, endpoint: nil, dialed: false)
|
|
339
|
+
ConnectionLifecycle.new(self, endpoint: endpoint, dialed: dialed).ready_direct!(pipe)
|
|
340
|
+
end
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
# @param conn [Protocol::ZMTP::Connection, Transport::Inproc::Pipe]
|
|
344
|
+
# @return [String, nil] the endpoint this socket dialed to get +conn+,
|
|
345
|
+
# nil for an accepted connection
|
|
346
|
+
#
|
|
347
|
+
def dialed_endpoint(conn)
|
|
348
|
+
lifecycle = @connections[conn]
|
|
349
|
+
lifecycle.endpoint if lifecycle&.dialed?
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
# @param endpoint [String, nil]
|
|
354
|
+
# @return [Boolean] true if a lost connection to +endpoint+ gets
|
|
355
|
+
# redialed
|
|
356
|
+
#
|
|
357
|
+
def redials?(endpoint)
|
|
358
|
+
return false unless endpoint && @dialers.key?(endpoint)
|
|
359
|
+
|
|
360
|
+
@lifecycle.open? && @lifecycle.reconnect_enabled
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
# Drops the send queue a routing strategy keeps for a dialed
|
|
365
|
+
# endpoint between connections (ROUTER, SERVER, PEER).
|
|
366
|
+
#
|
|
367
|
+
# @param endpoint [String, nil]
|
|
368
|
+
# @return [void]
|
|
369
|
+
#
|
|
370
|
+
def drop_route(endpoint)
|
|
371
|
+
@routing.drop_route(endpoint) if endpoint && @routing.respond_to?(:drop_route)
|
|
372
|
+
wake_drain
|
|
373
|
+
end
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
# Wakes {#drain_send_queues} to check the send queues again. Send
|
|
377
|
+
# pumps call it after each batch, teardown when a queue goes away.
|
|
378
|
+
# A no-op unless {#close} is waiting.
|
|
379
|
+
#
|
|
291
380
|
# @return [void]
|
|
292
381
|
#
|
|
293
|
-
def
|
|
294
|
-
|
|
382
|
+
def wake_drain
|
|
383
|
+
@drain_signal&.signal
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
# Runs a send pump's write of a batch it took off a queue. Until the
|
|
388
|
+
# block returns, {#drain_send_queues} counts the batch as unsent, so
|
|
389
|
+
# #close doesn't tear the connection down under the write.
|
|
390
|
+
#
|
|
391
|
+
# @yield the write and flush
|
|
392
|
+
# @return [Object] the block's value
|
|
393
|
+
#
|
|
394
|
+
def sending_batch
|
|
395
|
+
@batches_sending += 1
|
|
396
|
+
yield
|
|
397
|
+
ensure
|
|
398
|
+
@batches_sending -= 1
|
|
399
|
+
wake_drain
|
|
295
400
|
end
|
|
296
401
|
|
|
297
402
|
|
|
298
403
|
# Dequeues the next received message. Blocks until available.
|
|
299
404
|
#
|
|
300
|
-
# @return [Array<String
|
|
405
|
+
# @return [Array<String>, nil] message parts, or nil once the socket
|
|
406
|
+
# is closed (like +Thread::Queue#pop+ on a closed queue)
|
|
301
407
|
# @raise if a background pump task crashed
|
|
302
408
|
#
|
|
303
409
|
def dequeue_recv
|
|
304
410
|
raise @fatal_error if @fatal_error
|
|
411
|
+
return if @lifecycle.closed?
|
|
305
412
|
|
|
306
413
|
msg = routing.dequeue_recv
|
|
307
414
|
raise @fatal_error if msg.nil? && @fatal_error
|
|
@@ -323,10 +430,16 @@ module OMQ
|
|
|
323
430
|
# @param parts [Array<String>]
|
|
324
431
|
# @return [void]
|
|
325
432
|
# @raise if a background pump task crashed
|
|
433
|
+
# @raise [SocketClosedError] if the socket is closing or closed,
|
|
434
|
+
# including while blocked at HWM
|
|
326
435
|
#
|
|
327
436
|
def enqueue_send(parts)
|
|
328
437
|
raise @fatal_error if @fatal_error
|
|
438
|
+
raise_if_closed!
|
|
439
|
+
|
|
329
440
|
routing.enqueue(parts)
|
|
441
|
+
rescue Async::Queue::ClosedError, ClosedQueueError
|
|
442
|
+
raise SocketClosedError, "#{@socket_type} socket closed while sending"
|
|
330
443
|
end
|
|
331
444
|
|
|
332
445
|
|
|
@@ -369,12 +482,9 @@ module OMQ
|
|
|
369
482
|
# and the endpoint is still dialed.
|
|
370
483
|
#
|
|
371
484
|
def maybe_reconnect(endpoint)
|
|
372
|
-
return unless
|
|
373
|
-
return unless @lifecycle.open? && @lifecycle.reconnect_enabled
|
|
374
|
-
|
|
375
|
-
dialer = @dialers[endpoint]
|
|
485
|
+
return unless redials?(endpoint)
|
|
376
486
|
|
|
377
|
-
Reconnect.schedule(
|
|
487
|
+
Reconnect.schedule(@dialers[endpoint], @options, dialer_barrier(endpoint), self)
|
|
378
488
|
end
|
|
379
489
|
|
|
380
490
|
|
|
@@ -386,6 +496,7 @@ module OMQ
|
|
|
386
496
|
# @return [void]
|
|
387
497
|
#
|
|
388
498
|
def close
|
|
499
|
+
return close_unopened if @lifecycle.state == :new
|
|
389
500
|
return unless @lifecycle.open?
|
|
390
501
|
|
|
391
502
|
@lifecycle.start_closing!
|
|
@@ -403,6 +514,7 @@ module OMQ
|
|
|
403
514
|
|
|
404
515
|
stop_listeners
|
|
405
516
|
tear_down_barrier
|
|
517
|
+
close_routing_queues
|
|
406
518
|
emit_monitor_event(:closed)
|
|
407
519
|
close_monitor_queue
|
|
408
520
|
end
|
|
@@ -425,100 +537,12 @@ module OMQ
|
|
|
425
537
|
|
|
426
538
|
stop_listeners
|
|
427
539
|
tear_down_barrier
|
|
540
|
+
close_routing_queues
|
|
428
541
|
emit_monitor_event(:closed)
|
|
429
542
|
close_monitor_queue
|
|
430
543
|
end
|
|
431
544
|
|
|
432
545
|
|
|
433
|
-
# Spawns a transient pump task with error propagation.
|
|
434
|
-
#
|
|
435
|
-
# Unexpected exceptions are caught and forwarded to
|
|
436
|
-
# {#signal_fatal_error} so blocked callers (send/recv)
|
|
437
|
-
# see the real error instead of deadlocking.
|
|
438
|
-
#
|
|
439
|
-
# @param annotation [String] task annotation for debugging
|
|
440
|
-
# @param parent [Async::Task, Async::Barrier] parent for the spawned
|
|
441
|
-
# task. Defaults to the current task. Routing strategies that own
|
|
442
|
-
# loose pump tasks (Radio, Channel) pass their own +Async::Barrier+
|
|
443
|
-
# so a single +barrier.stop+ tears the lot down at strategy
|
|
444
|
-
# shutdown without per-task bookkeeping.
|
|
445
|
-
# @yield the pump loop body
|
|
446
|
-
# @return [Async::Task]
|
|
447
|
-
#
|
|
448
|
-
def spawn_pump_task(annotation:, parent: Async::Task.current, &block)
|
|
449
|
-
parent.async(transient: true, annotation: annotation) do
|
|
450
|
-
yield
|
|
451
|
-
rescue Async::Stop, Async::Cancel, Protocol::ZMTP::Error, *CONNECTION_LOST
|
|
452
|
-
# normal shutdown / expected disconnect
|
|
453
|
-
rescue => error
|
|
454
|
-
signal_fatal_error(error)
|
|
455
|
-
end
|
|
456
|
-
end
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
# Spawns a per-connection pump task on the connection's own
|
|
460
|
-
# lifecycle barrier. When any pump on the barrier exits (e.g. the
|
|
461
|
-
# send pump sees EPIPE and calls {#connection_lost}), {ConnectionLifecycle#tear_down!}
|
|
462
|
-
# calls `barrier.stop` which cancels every sibling pump for that
|
|
463
|
-
# connection — so a dead peer can no longer leave orphan send
|
|
464
|
-
# pumps blocked on `dequeue` waiting for messages that will never
|
|
465
|
-
# be written.
|
|
466
|
-
#
|
|
467
|
-
# @param conn [Protocol::ZMTP::Connection, Transport::Inproc::Pipe]
|
|
468
|
-
# @param annotation [String]
|
|
469
|
-
#
|
|
470
|
-
def spawn_conn_pump_task(conn, annotation:, &block)
|
|
471
|
-
lifecycle = @connections[conn]
|
|
472
|
-
|
|
473
|
-
unless lifecycle
|
|
474
|
-
return spawn_pump_task(annotation: annotation, &block)
|
|
475
|
-
end
|
|
476
|
-
|
|
477
|
-
lifecycle.barrier.async(transient: true, annotation: annotation) do
|
|
478
|
-
yield
|
|
479
|
-
rescue Async::Stop, Async::Cancel
|
|
480
|
-
# normal shutdown / sibling tore us down
|
|
481
|
-
rescue Protocol::ZMTP::Error, *CONNECTION_LOST => error
|
|
482
|
-
# expected disconnect — stash reason for the :disconnected
|
|
483
|
-
# monitor event, then let the lifecycle reconnect as usual
|
|
484
|
-
lifecycle.record_disconnect_reason(error)
|
|
485
|
-
rescue => error
|
|
486
|
-
signal_fatal_error(error)
|
|
487
|
-
end
|
|
488
|
-
end
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
# Wraps an unexpected pump error as {OMQ::SocketDeadError} and
|
|
492
|
-
# unblocks any callers waiting on the recv queue. The original
|
|
493
|
-
# error is preserved as +#cause+ so callers can surface the real
|
|
494
|
-
# reason.
|
|
495
|
-
#
|
|
496
|
-
# @param error [Exception]
|
|
497
|
-
#
|
|
498
|
-
def signal_fatal_error(error)
|
|
499
|
-
return unless @lifecycle.open?
|
|
500
|
-
|
|
501
|
-
@fatal_error = build_fatal_error(error)
|
|
502
|
-
routing.unblock_recv rescue nil
|
|
503
|
-
@lifecycle.peer_connected.resolve(nil) rescue nil
|
|
504
|
-
end
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
# Constructs a SocketDeadError whose +cause+ is +error+. Uses the
|
|
508
|
-
# raise-in-rescue idiom because Ruby only sets +cause+ on an
|
|
509
|
-
# exception when it is raised from inside a rescue block -- works
|
|
510
|
-
# regardless of the original caller's +$!+ state.
|
|
511
|
-
def build_fatal_error(error)
|
|
512
|
-
raise error
|
|
513
|
-
rescue
|
|
514
|
-
begin
|
|
515
|
-
raise SocketDeadError, "#{@socket_type} socket killed: #{error.message}"
|
|
516
|
-
rescue SocketDeadError => wrapped
|
|
517
|
-
wrapped
|
|
518
|
-
end
|
|
519
|
-
end
|
|
520
|
-
|
|
521
|
-
|
|
522
546
|
# Captures the socket's task tree root and starts the socket-level
|
|
523
547
|
# maintenance task. If +parent+ is given, it is used as the parent
|
|
524
548
|
# for every task spawned under this socket (connection supervisors,
|
|
@@ -533,6 +557,8 @@ module OMQ
|
|
|
533
557
|
# @param parent [#async, nil] optional Async parent
|
|
534
558
|
#
|
|
535
559
|
def capture_parent_task(parent: nil)
|
|
560
|
+
raise_if_closed!
|
|
561
|
+
|
|
536
562
|
task = @lifecycle.capture_parent_task(parent: parent, linger: @options.linger)
|
|
537
563
|
|
|
538
564
|
return unless task
|
|
@@ -541,87 +567,6 @@ module OMQ
|
|
|
541
567
|
end
|
|
542
568
|
|
|
543
569
|
|
|
544
|
-
# Emits a lifecycle event to the monitor queue, if one is attached.
|
|
545
|
-
#
|
|
546
|
-
# @param type [Symbol] event type (e.g. :listening, :connected, :disconnected)
|
|
547
|
-
# @param endpoint [String, nil] the endpoint involved
|
|
548
|
-
# @param detail [Hash, nil] extra context
|
|
549
|
-
# @return [void]
|
|
550
|
-
#
|
|
551
|
-
def emit_monitor_event(type, endpoint: nil, detail: nil)
|
|
552
|
-
return unless @monitor_queue
|
|
553
|
-
|
|
554
|
-
event = MonitorEvent.new type: type, endpoint: endpoint, detail: detail
|
|
555
|
-
|
|
556
|
-
@monitor_queue << event
|
|
557
|
-
rescue Async::Stop, ClosedQueueError
|
|
558
|
-
end
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
# Emits a verbose-only monitor event (e.g. message traces).
|
|
562
|
-
# Only emitted when {Socket#monitor} was called with +verbose: true+.
|
|
563
|
-
# Uses +**detail+ to avoid Hash allocation when verbose is off.
|
|
564
|
-
#
|
|
565
|
-
# @param type [Symbol] event type (e.g. :message_sent, :message_received)
|
|
566
|
-
# @param detail [Hash] extra context forwarded as keyword args
|
|
567
|
-
# @return [void]
|
|
568
|
-
#
|
|
569
|
-
def emit_verbose_monitor_event(type, **detail)
|
|
570
|
-
return unless @verbose_monitor
|
|
571
|
-
emit_monitor_event(type, detail: detail)
|
|
572
|
-
end
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
# Emits a :message_sent verbose event and enriches it with the
|
|
576
|
-
# on-wire (post-compression) byte size if +conn+ exposes
|
|
577
|
-
# +last_wire_size_out+ (installed by ZMTP-Zstd etc.).
|
|
578
|
-
def emit_verbose_msg_sent(conn, parts)
|
|
579
|
-
return unless @verbose_monitor
|
|
580
|
-
|
|
581
|
-
detail = { parts: parts }
|
|
582
|
-
|
|
583
|
-
if conn.respond_to? :last_wire_size_out
|
|
584
|
-
detail[:wire_size] = conn.last_wire_size_out
|
|
585
|
-
end
|
|
586
|
-
|
|
587
|
-
emit_monitor_event :message_sent, detail: detail
|
|
588
|
-
end
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
# Emits a :message_received verbose event and enriches it with the
|
|
592
|
-
# on-wire (pre-decompression) byte size if +conn+ exposes
|
|
593
|
-
# +last_wire_size_in+.
|
|
594
|
-
def emit_verbose_msg_received(conn, parts)
|
|
595
|
-
return unless @verbose_monitor
|
|
596
|
-
|
|
597
|
-
detail = { parts: parts }
|
|
598
|
-
|
|
599
|
-
if conn.respond_to? :last_wire_size_in
|
|
600
|
-
detail[:wire_size] = conn.last_wire_size_in
|
|
601
|
-
end
|
|
602
|
-
|
|
603
|
-
emit_monitor_event :message_received, detail: detail
|
|
604
|
-
end
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
# Looks up the transport module for an endpoint URI.
|
|
608
|
-
#
|
|
609
|
-
# @param endpoint [String] endpoint URI (e.g. "tcp://...", "ruby://...")
|
|
610
|
-
# @return [Module] the transport module
|
|
611
|
-
# @raise [ArgumentError] if the scheme is not registered
|
|
612
|
-
#
|
|
613
|
-
def transport_for(endpoint)
|
|
614
|
-
scheme = scheme_for(endpoint)
|
|
615
|
-
transport = registered_transport(scheme)
|
|
616
|
-
|
|
617
|
-
unless transport
|
|
618
|
-
raise ArgumentError, "unsupported transport: #{endpoint}"
|
|
619
|
-
end
|
|
620
|
-
|
|
621
|
-
transport
|
|
622
|
-
end
|
|
623
|
-
|
|
624
|
-
|
|
625
570
|
private
|
|
626
571
|
|
|
627
572
|
|
|
@@ -634,12 +579,15 @@ module OMQ
|
|
|
634
579
|
# @param io [#read, #write, #close] accepted or dialed socket
|
|
635
580
|
# @param as_server [Boolean] true for accepted connections
|
|
636
581
|
# @param endpoint [String, nil] the endpoint URI, for monitor events
|
|
582
|
+
# @param parent [Async::Barrier, nil] owner of the connection task:
|
|
583
|
+
# the dialer's barrier for dialed connections, so {#disconnect}
|
|
584
|
+
# also stops a handshake in progress
|
|
637
585
|
#
|
|
638
|
-
def spawn_connection(io, as_server:, endpoint: nil)
|
|
639
|
-
|
|
586
|
+
def spawn_connection(io, as_server:, endpoint: nil, parent: @lifecycle.barrier)
|
|
587
|
+
parent&.async(transient: true, annotation: "conn #{endpoint}") do
|
|
640
588
|
done = Async::Promise.new
|
|
641
589
|
transport = endpoint ? transport_for(endpoint) : nil
|
|
642
|
-
lifecycle = ConnectionLifecycle.new(self, endpoint: endpoint, done: done, transport: transport)
|
|
590
|
+
lifecycle = ConnectionLifecycle.new(self, endpoint: endpoint, done: done, transport: transport, dialed: !as_server)
|
|
643
591
|
lifecycle.handshake!(io, as_server: as_server)
|
|
644
592
|
done.wait
|
|
645
593
|
rescue Async::Stop, Async::Cancel
|
|
@@ -656,31 +604,35 @@ module OMQ
|
|
|
656
604
|
|
|
657
605
|
# Blocks until the routing strategy reports all send queues drained
|
|
658
606
|
# or +timeout+ seconds have elapsed. Called from the linger path on
|
|
659
|
-
# close so pending outgoing messages get a chance to flush.
|
|
607
|
+
# close so pending outgoing messages get a chance to flush. Sleeps
|
|
608
|
+
# until {#wake_drain} says something changed.
|
|
660
609
|
#
|
|
661
610
|
# @param timeout [Numeric, nil] max seconds to wait; nil = no limit
|
|
662
611
|
#
|
|
663
|
-
# TODO: replace the 1 ms busy-poll with a promise/condition that
|
|
664
|
-
# the send pump resolves when its queue hits empty. The loop exists
|
|
665
|
-
# because there is currently no signal for "send queue fully
|
|
666
|
-
# drained"; fixing it cleanly requires plumbing a notifier through
|
|
667
|
-
# every routing strategy, so it is flagged rather than fixed here.
|
|
668
|
-
#
|
|
669
612
|
def drain_send_queues(timeout)
|
|
670
613
|
return unless @routing.respond_to?(:send_queues_drained?)
|
|
671
614
|
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
end
|
|
615
|
+
deadline = timeout&.finite? ? Async::Clock.now + timeout : nil # nil, Infinity: no limit
|
|
616
|
+
@drain_signal = Async::Notification.new
|
|
675
617
|
|
|
676
|
-
until @routing.send_queues_drained?
|
|
677
|
-
|
|
678
|
-
|
|
618
|
+
until @batches_sending.zero? && @routing.send_queues_drained?
|
|
619
|
+
if deadline
|
|
620
|
+
remaining = deadline - Async::Clock.now
|
|
621
|
+
break if remaining <= 0
|
|
622
|
+
|
|
623
|
+
Async::Task.current.with_timeout(remaining) { @drain_signal.wait }
|
|
624
|
+
else
|
|
625
|
+
@drain_signal.wait
|
|
626
|
+
end
|
|
679
627
|
end
|
|
628
|
+
rescue Async::TimeoutError
|
|
629
|
+
# linger expired
|
|
680
630
|
rescue Async::Stop
|
|
681
631
|
# Parent task is being cancelled — stop draining and let close
|
|
682
632
|
# proceed with the rest of teardown instead of propagating the
|
|
683
633
|
# cancellation out of the ensure path.
|
|
634
|
+
ensure
|
|
635
|
+
@drain_signal = nil
|
|
684
636
|
end
|
|
685
637
|
|
|
686
638
|
|
|
@@ -693,73 +645,37 @@ module OMQ
|
|
|
693
645
|
#
|
|
694
646
|
def schedule_reconnect(endpoint, delay: nil)
|
|
695
647
|
dialer = @dialers[endpoint]
|
|
696
|
-
Reconnect.schedule(dialer, @options,
|
|
697
|
-
end
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
# Delegates endpoint validation to the transport if it defines one.
|
|
701
|
-
# Called from {#connect} so a bad URI fails fast instead of landing
|
|
702
|
-
# in a reconnect loop.
|
|
703
|
-
#
|
|
704
|
-
# @param endpoint [String]
|
|
705
|
-
# @raise [ArgumentError] if the transport rejects the endpoint
|
|
706
|
-
#
|
|
707
|
-
def validate_endpoint!(endpoint, transport)
|
|
708
|
-
if transport.respond_to?(:validate_endpoint!)
|
|
709
|
-
transport.validate_endpoint!(endpoint)
|
|
710
|
-
end
|
|
648
|
+
Reconnect.schedule(dialer, @options, dialer_barrier(endpoint), self, delay: delay)
|
|
711
649
|
end
|
|
712
650
|
|
|
713
651
|
|
|
714
|
-
#
|
|
715
|
-
#
|
|
652
|
+
# Returns the barrier that owns +endpoint+'s reconnect loop and
|
|
653
|
+
# dialed connection tasks, nested in the socket barrier so
|
|
654
|
+
# {#close} stops them too.
|
|
716
655
|
#
|
|
717
656
|
# @param endpoint [String]
|
|
718
|
-
# @return [
|
|
657
|
+
# @return [Async::Barrier]
|
|
719
658
|
#
|
|
720
|
-
def
|
|
721
|
-
|
|
722
|
-
if transport.respond_to?(:canonical_endpoint)
|
|
723
|
-
endpoint = transport.canonical_endpoint(endpoint)
|
|
724
|
-
end
|
|
725
|
-
|
|
726
|
-
[endpoint, transport]
|
|
659
|
+
def dialer_barrier(endpoint)
|
|
660
|
+
@dialer_barriers[endpoint] ||= Async::Barrier.new(parent: @lifecycle.barrier)
|
|
727
661
|
end
|
|
728
662
|
|
|
729
663
|
|
|
730
|
-
#
|
|
731
|
-
#
|
|
664
|
+
# Ruby in-process connects are synchronous: link now, or wait up to
|
|
665
|
+
# one reconnect interval for a bind, then keep retrying in the
|
|
666
|
+
# background like any other dialer.
|
|
732
667
|
#
|
|
733
668
|
# @param endpoint [String]
|
|
734
|
-
# @
|
|
735
|
-
#
|
|
736
|
-
def canonical_endpoint(endpoint)
|
|
737
|
-
transport = registered_transport(scheme_for(endpoint))
|
|
738
|
-
return endpoint unless transport&.respond_to?(:canonical_endpoint)
|
|
739
|
-
|
|
740
|
-
transport.canonical_endpoint(endpoint)
|
|
741
|
-
end
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
# Looks up a registered transport. If no real inproc transport is
|
|
745
|
-
# registered, inproc:// falls back to the Ruby in-process transport.
|
|
669
|
+
# @param dialer [Transport::Inproc::Dialer]
|
|
746
670
|
#
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
# Extracts the URI scheme from an endpoint.
|
|
757
|
-
#
|
|
758
|
-
# @param endpoint [String]
|
|
759
|
-
# @return [String, nil]
|
|
760
|
-
#
|
|
761
|
-
def scheme_for(endpoint)
|
|
762
|
-
endpoint[/\A([^:]+):\/\//, 1]
|
|
671
|
+
def connect_inproc(endpoint, dialer)
|
|
672
|
+
dialer.connect(wait: true)
|
|
673
|
+
rescue Errno::ECONNREFUSED
|
|
674
|
+
emit_monitor_event(:connect_delayed, endpoint: endpoint)
|
|
675
|
+
schedule_reconnect(endpoint)
|
|
676
|
+
rescue
|
|
677
|
+
@dialers.delete(endpoint)
|
|
678
|
+
raise
|
|
763
679
|
end
|
|
764
680
|
|
|
765
681
|
|
|
@@ -784,6 +700,22 @@ module OMQ
|
|
|
784
700
|
def stop_listeners
|
|
785
701
|
@listeners.each_value(&:stop)
|
|
786
702
|
@listeners.clear
|
|
703
|
+
@bind_aliases.clear
|
|
704
|
+
end
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
# Lets {#unbind} find a listener by the string the caller passed to
|
|
708
|
+
# {#bind}. Ephemeral-port requests (+:0+, +:*+) can bind many
|
|
709
|
+
# listeners, so they don't get an alias.
|
|
710
|
+
#
|
|
711
|
+
# @param requested [String] canonical endpoint passed to #bind
|
|
712
|
+
# @param bound [String] the listener's resolved endpoint
|
|
713
|
+
#
|
|
714
|
+
def remember_bind_alias(requested, bound)
|
|
715
|
+
return if requested == bound
|
|
716
|
+
return if requested.end_with?(":0", ":*")
|
|
717
|
+
|
|
718
|
+
@bind_aliases[requested] = bound
|
|
787
719
|
end
|
|
788
720
|
|
|
789
721
|
|
|
@@ -800,21 +732,40 @@ module OMQ
|
|
|
800
732
|
# Cascades teardown through the socket-level barrier. Stopping the
|
|
801
733
|
# barrier cancels every tracked task: connection supervisors (whose
|
|
802
734
|
# `ensure lost!` runs the ordered disconnect side effects), accept
|
|
803
|
-
# loops, heartbeat, maintenance
|
|
804
|
-
#
|
|
735
|
+
# loops, heartbeat, maintenance, and the per-endpoint dialer
|
|
736
|
+
# barriers with their reconnect loops.
|
|
805
737
|
#
|
|
806
738
|
def tear_down_barrier
|
|
807
739
|
@lifecycle.barrier&.stop
|
|
808
740
|
end
|
|
809
741
|
|
|
810
742
|
|
|
811
|
-
#
|
|
812
|
-
# +nil+ sentinel, so consumers iterating with {Socket#each_event}
|
|
813
|
-
# can exit cleanly.
|
|
743
|
+
# @raise [SocketClosedError] once {#close} or {#stop} has started
|
|
814
744
|
#
|
|
815
|
-
def
|
|
816
|
-
return
|
|
817
|
-
|
|
745
|
+
def raise_if_closed!
|
|
746
|
+
return if @lifecycle.alive?
|
|
747
|
+
|
|
748
|
+
raise SocketClosedError, "#{@socket_type} socket is closed"
|
|
818
749
|
end
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
# Closes a socket that never bound or connected: there are no tasks
|
|
753
|
+
# to stop, but later calls must see it closed.
|
|
754
|
+
#
|
|
755
|
+
def close_unopened
|
|
756
|
+
@lifecycle.finish_closing!
|
|
757
|
+
close_routing_queues
|
|
758
|
+
emit_monitor_event(:closed)
|
|
759
|
+
close_monitor_queue
|
|
760
|
+
end
|
|
761
|
+
|
|
762
|
+
|
|
763
|
+
# Wakes fibers blocked in a send (they raise {SocketClosedError}) or
|
|
764
|
+
# a receive (they get +nil+).
|
|
765
|
+
#
|
|
766
|
+
def close_routing_queues
|
|
767
|
+
@routing.close_queues if @routing.respond_to?(:close_queues)
|
|
768
|
+
end
|
|
769
|
+
|
|
819
770
|
end
|
|
820
771
|
end
|