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.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +181 -0
  3. data/README.md +3 -3
  4. data/lib/omq/constants.rb +9 -1
  5. data/lib/omq/drop_queue.rb +17 -0
  6. data/lib/omq/engine/connection_lifecycle.rb +34 -6
  7. data/lib/omq/engine/endpoints.rb +107 -0
  8. data/lib/omq/engine/heartbeat.rb +157 -15
  9. data/lib/omq/engine/monitoring.rb +95 -0
  10. data/lib/omq/engine/pump_tasks.rb +102 -0
  11. data/lib/omq/engine/reconnect.rb +10 -9
  12. data/lib/omq/engine/recv_pump.rb +18 -4
  13. data/lib/omq/engine/socket_lifecycle.rb +18 -4
  14. data/lib/omq/engine.rb +255 -304
  15. data/lib/omq/monitor_queue.rb +74 -0
  16. data/lib/omq/options.rb +78 -6
  17. data/lib/omq/routing/channel.rb +11 -5
  18. data/lib/omq/routing/client.rb +3 -1
  19. data/lib/omq/routing/closable_queues.rb +28 -0
  20. data/lib/omq/routing/conn_send_pump.rb +11 -9
  21. data/lib/omq/routing/dealer.rb +1 -0
  22. data/lib/omq/routing/dish.rb +5 -3
  23. data/lib/omq/routing/fan_out.rb +29 -21
  24. data/lib/omq/routing/gather.rb +4 -1
  25. data/lib/omq/routing/identity_routing.rb +136 -0
  26. data/lib/omq/routing/pair.rb +21 -12
  27. data/lib/omq/routing/peer.rb +30 -30
  28. data/lib/omq/routing/prefix_set.rb +239 -0
  29. data/lib/omq/routing/pub.rb +2 -1
  30. data/lib/omq/routing/pull.rb +2 -0
  31. data/lib/omq/routing/push.rb +1 -0
  32. data/lib/omq/routing/radio.rb +65 -54
  33. data/lib/omq/routing/rep.rb +4 -2
  34. data/lib/omq/routing/req.rb +8 -0
  35. data/lib/omq/routing/round_robin.rb +22 -17
  36. data/lib/omq/routing/router.rb +46 -28
  37. data/lib/omq/routing/scatter.rb +1 -0
  38. data/lib/omq/routing/server.rb +21 -25
  39. data/lib/omq/routing/sub.rb +26 -7
  40. data/lib/omq/routing/xpub.rb +25 -2
  41. data/lib/omq/routing/xsub.rb +78 -29
  42. data/lib/omq/routing.rb +52 -2
  43. data/lib/omq/socket.rb +4 -1
  44. data/lib/omq/transport/accept_loop.rb +60 -0
  45. data/lib/omq/transport/compressed_connection.rb +6 -0
  46. data/lib/omq/transport/inproc/pipe.rb +7 -0
  47. data/lib/omq/transport/inproc.rb +27 -33
  48. data/lib/omq/transport/ipc.rb +105 -7
  49. data/lib/omq/transport/tcp.rb +8 -5
  50. data/lib/omq/transport/udp.rb +92 -17
  51. data/lib/omq/version.rb +1 -1
  52. data/lib/omq.rb +2 -0
  53. metadata +11 -9
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OMQ
4
+ class Engine
5
+ # Monitor events. Uses the engine's +@monitor_queue+ and
6
+ # +@verbose_monitor+.
7
+ #
8
+ module Monitoring
9
+ # Emits a lifecycle event to the monitor queue, if one is attached.
10
+ #
11
+ # @param type [Symbol] event type (e.g. :listening, :connected, :disconnected)
12
+ # @param endpoint [String, nil] the endpoint involved
13
+ # @param detail [Hash, nil] extra context
14
+ # @return [void]
15
+ #
16
+ def emit_monitor_event(type, endpoint: nil, detail: nil)
17
+ return unless @monitor_queue
18
+
19
+ @monitor_queue << MonitorEvent.new(type: type, endpoint: endpoint, detail: detail)
20
+ end
21
+
22
+
23
+ # Emits a verbose-only monitor event (e.g. message traces).
24
+ # Only emitted when {Socket#monitor} was called with +verbose: true+.
25
+ # Uses +**detail+ to avoid Hash allocation when verbose is off.
26
+ #
27
+ # @param type [Symbol] event type (e.g. :message_sent, :message_received)
28
+ # @param detail [Hash] extra context forwarded as keyword args
29
+ # @return [void]
30
+ #
31
+ def emit_verbose_monitor_event(type, **detail)
32
+ return unless @verbose_monitor
33
+
34
+ push_verbose_event(type, detail)
35
+ end
36
+
37
+
38
+ # Emits a :message_sent verbose event and enriches it with the
39
+ # on-wire (post-compression) byte size if +conn+ exposes
40
+ # +last_wire_size_out+ (installed by ZMTP-Zstd etc.).
41
+ def emit_verbose_msg_sent(conn, parts)
42
+ return unless @verbose_monitor
43
+
44
+ detail = { parts: parts }
45
+
46
+ if conn.respond_to? :last_wire_size_out
47
+ detail[:wire_size] = conn.last_wire_size_out
48
+ end
49
+
50
+ push_verbose_event(:message_sent, detail)
51
+ end
52
+
53
+
54
+ # Emits a :message_received verbose event and enriches it with the
55
+ # on-wire (pre-decompression) byte size if +conn+ exposes
56
+ # +last_wire_size_in+.
57
+ def emit_verbose_msg_received(conn, parts)
58
+ return unless @verbose_monitor
59
+
60
+ detail = { parts: parts }
61
+
62
+ if conn.respond_to? :last_wire_size_in
63
+ detail[:wire_size] = conn.last_wire_size_in
64
+ end
65
+
66
+ push_verbose_event(:message_received, detail)
67
+ end
68
+
69
+
70
+ private
71
+
72
+
73
+ # Verbose events wait for the monitor to catch up (see
74
+ # {MonitorQueue#push_verbose}).
75
+ #
76
+ def push_verbose_event(type, detail)
77
+ queue = @monitor_queue or return
78
+
79
+ queue.push_verbose(MonitorEvent.new(type: type, detail: detail))
80
+ rescue Async::Stop
81
+ end
82
+
83
+
84
+ # Signals end-of-stream on the monitor queue (if any) by pushing a
85
+ # +nil+ sentinel, so consumers iterating with {Socket#each_event}
86
+ # can exit cleanly.
87
+ #
88
+ def close_monitor_queue
89
+ return unless @monitor_queue
90
+ @monitor_queue.push(nil)
91
+ end
92
+
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OMQ
4
+ class Engine
5
+ # Spawning pump tasks, and turning an unexpected pump error into
6
+ # a dead socket.
7
+ #
8
+ module PumpTasks
9
+ # Spawns a transient pump task with error propagation.
10
+ #
11
+ # Unexpected exceptions are caught and forwarded to
12
+ # {#signal_fatal_error} so blocked callers (send/recv)
13
+ # see the real error instead of deadlocking.
14
+ #
15
+ # @param annotation [String] task annotation for debugging
16
+ # @param parent [Async::Task, Async::Barrier] parent for the spawned
17
+ # task. Defaults to the current task. Routing strategies that own
18
+ # loose pump tasks (Radio, Channel) pass their own +Async::Barrier+
19
+ # so a single +barrier.stop+ tears the lot down at strategy
20
+ # shutdown without per-task bookkeeping.
21
+ # @yield the pump loop body
22
+ # @return [Async::Task]
23
+ #
24
+ def spawn_pump_task(annotation:, parent: Async::Task.current, &block)
25
+ parent.async(transient: true, annotation: annotation) do
26
+ yield
27
+ rescue Async::Stop, Async::Cancel, Protocol::ZMTP::Error, *CONNECTION_LOST
28
+ # normal shutdown / expected disconnect
29
+ rescue => error
30
+ signal_fatal_error(error)
31
+ end
32
+ end
33
+
34
+
35
+ # Spawns a per-connection pump task on the connection's own
36
+ # lifecycle barrier. When any pump on the barrier exits (e.g. the
37
+ # send pump sees EPIPE and calls {#connection_lost}), {ConnectionLifecycle#tear_down!}
38
+ # calls `barrier.stop` which cancels every sibling pump for that
39
+ # connection — so a dead peer can no longer leave orphan send
40
+ # pumps blocked on `dequeue` waiting for messages that will never
41
+ # be written.
42
+ #
43
+ # @param conn [Protocol::ZMTP::Connection, Transport::Inproc::Pipe]
44
+ # @param annotation [String]
45
+ #
46
+ def spawn_conn_pump_task(conn, annotation:, &block)
47
+ lifecycle = @connections[conn]
48
+
49
+ unless lifecycle
50
+ return spawn_pump_task(annotation: annotation, &block)
51
+ end
52
+
53
+ lifecycle.barrier.async(transient: true, annotation: annotation) do
54
+ yield
55
+ rescue Async::Stop, Async::Cancel
56
+ # normal shutdown / sibling tore us down
57
+ rescue Protocol::ZMTP::Error, *CONNECTION_LOST => error
58
+ # expected disconnect — stash reason for the :disconnected
59
+ # monitor event, then let the lifecycle reconnect as usual
60
+ lifecycle.record_disconnect_reason(error)
61
+ rescue => error
62
+ signal_fatal_error(error)
63
+ end
64
+ end
65
+
66
+
67
+ # Wraps an unexpected pump error as {OMQ::SocketDeadError} and
68
+ # unblocks any callers waiting on the recv queue. The original
69
+ # error is preserved as +#cause+ so callers can surface the real
70
+ # reason.
71
+ #
72
+ # @param error [Exception]
73
+ #
74
+ def signal_fatal_error(error)
75
+ return unless @lifecycle.open?
76
+
77
+ @fatal_error = build_fatal_error(error)
78
+ routing.unblock_recv rescue nil
79
+ @lifecycle.peer_connected.resolve(nil) rescue nil
80
+ end
81
+
82
+
83
+ private
84
+
85
+
86
+ # Constructs a SocketDeadError whose +cause+ is +error+. Uses the
87
+ # raise-in-rescue idiom because Ruby only sets +cause+ on an
88
+ # exception when it is raised from inside a rescue block -- works
89
+ # regardless of the original caller's +$!+ state.
90
+ def build_fatal_error(error)
91
+ raise error
92
+ rescue
93
+ begin
94
+ raise SocketDeadError, "#{@socket_type} socket killed: #{error.message}"
95
+ rescue SocketDeadError => wrapped
96
+ wrapped
97
+ end
98
+ end
99
+
100
+ end
101
+ end
102
+ end
@@ -10,12 +10,13 @@ module OMQ
10
10
  class Reconnect
11
11
  # @param dialer [Transport::TCP::Dialer, etc.] stateful dialer factory
12
12
  # @param options [Options]
13
- # @param parent_task [Async::Task]
13
+ # @param parent [#async] owner of the retry task (the dialer's barrier)
14
14
  # @param engine [Engine]
15
15
  # @param delay [Numeric, nil] initial delay (defaults to reconnect_interval)
16
+ # @return [Async::Task]
16
17
  #
17
- def self.schedule(dialer, options, parent_task, engine, delay: nil)
18
- new(engine, dialer, options).run(parent_task, delay: delay)
18
+ def self.schedule(dialer, options, parent, engine, delay: nil)
19
+ new(engine, dialer, options).run(parent, delay: delay)
19
20
  end
20
21
 
21
22
 
@@ -32,13 +33,13 @@ module OMQ
32
33
 
33
34
  # Spawns a background task that retries the connection with exponential backoff.
34
35
  #
35
- # @param parent_task [Async::Task]
36
+ # @param parent [#async]
36
37
  # @param delay [Numeric, nil] initial delay override
37
- # @return [void]
38
+ # @return [Async::Task]
38
39
  #
39
- def run(parent_task, delay: nil)
40
+ def run(parent, delay: nil)
40
41
  endpoint = @dialer.endpoint
41
- parent_task.async(transient: true, annotation: "reconnect #{endpoint}") do
42
+ parent.async(transient: true, annotation: "reconnect #{endpoint}") do
42
43
  retry_loop(delay: delay)
43
44
  rescue Async::Stop
44
45
  rescue => error
@@ -62,10 +63,10 @@ module OMQ
62
63
  break
63
64
  rescue Protocol::ZMTP::FatalHandshakeError
64
65
  break
65
- rescue *CONNECTION_LOST, *CONNECTION_FAILED, Protocol::ZMTP::Error
66
+ rescue *CONNECTION_LOST, *CONNECTION_FAILED, Protocol::ZMTP::Error => error
66
67
  delay = next_delay(delay, max_delay)
67
68
  @engine.emit_monitor_event :connect_retried,
68
- endpoint: @dialer.endpoint, detail: { interval: delay }
69
+ endpoint: @dialer.endpoint, detail: { interval: delay, error: error }
69
70
  end
70
71
  end
71
72
  end
@@ -49,6 +49,7 @@ module OMQ
49
49
  @recv_queue = recv_queue
50
50
  @engine = engine
51
51
  @count_bytes = conn.instance_of?(Protocol::ZMTP::Connection) || conn.respond_to?(:last_wire_size_in)
52
+ @pausable = conn.respond_to?(:pause_reading) && recv_queue.respond_to?(:limited?)
52
53
  end
53
54
 
54
55
 
@@ -90,7 +91,7 @@ module OMQ
90
91
  # @return [Async::Task]
91
92
  #
92
93
  def start_with_transform(parent, transform)
93
- conn, recv_queue, engine, count_bytes = @conn, @recv_queue, @engine, @count_bytes
94
+ conn, engine, count_bytes = @conn, @engine, @count_bytes
94
95
 
95
96
  parent.async(transient: true, annotation: "recv pump") do |task|
96
97
  loop do
@@ -125,7 +126,7 @@ module OMQ
125
126
  # async scheduler is FIFO on the ready list, so this
126
127
  # preserves log-before-stdout ordering for -vvv traces.
127
128
  engine.emit_verbose_msg_received(conn, transformed)
128
- recv_queue.enqueue(transformed)
129
+ enqueue(transformed)
129
130
  end
130
131
 
131
132
  task.yield
@@ -141,13 +142,26 @@ module OMQ
141
142
  end
142
143
 
143
144
 
145
+ # Enqueues +msg+. When the queue is full, marks the connection's
146
+ # reading as paused while waiting, so the heartbeat doesn't take
147
+ # our own backpressure for a silent peer.
148
+ #
149
+ def enqueue(msg)
150
+ if @pausable && @recv_queue.limited?
151
+ @conn.pause_reading { @recv_queue.enqueue(msg) }
152
+ else
153
+ @recv_queue.enqueue(msg)
154
+ end
155
+ end
156
+
157
+
144
158
  # Recv loop without transform — the hot path for native OMQ use.
145
159
  #
146
160
  # @param parent [Async::Task, Async::Barrier]
147
161
  # @return [Async::Task]
148
162
  #
149
163
  def start_direct(parent)
150
- conn, recv_queue, engine, count_bytes = @conn, @recv_queue, @engine, @count_bytes
164
+ conn, engine, count_bytes = @conn, @engine, @count_bytes
151
165
 
152
166
  parent.async(transient: true, annotation: "recv pump") do |task|
153
167
  loop do
@@ -159,7 +173,7 @@ module OMQ
159
173
  msg.each { |part| part.freeze }
160
174
  msg.freeze
161
175
  engine.emit_verbose_msg_received(conn, msg)
162
- recv_queue.enqueue(msg)
176
+ enqueue(msg)
163
177
 
164
178
  count += 1
165
179
 
@@ -80,10 +80,24 @@ module OMQ
80
80
  end
81
81
 
82
82
 
83
- def open? = @state == :open
84
- def closing? = @state == :closing
85
- def closed? = @state == :closed
86
- def alive? = @state == :new || @state == :open
83
+ def open?
84
+ @state == :open
85
+ end
86
+
87
+
88
+ def closing?
89
+ @state == :closing
90
+ end
91
+
92
+
93
+ def closed?
94
+ @state == :closed
95
+ end
96
+
97
+
98
+ def alive?
99
+ @state == :new || @state == :open
100
+ end
87
101
 
88
102
 
89
103
  # Captures the socket's task tree root. Transitions `:new → :open`.