omq 0.5.1 → 0.6.0

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 (47) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +173 -0
  3. data/README.md +21 -19
  4. data/exe/omq +6 -0
  5. data/lib/omq/cli/base_runner.rb +423 -0
  6. data/lib/omq/cli/channel.rb +8 -0
  7. data/lib/omq/cli/client_server.rb +106 -0
  8. data/lib/omq/cli/config.rb +51 -0
  9. data/lib/omq/cli/formatter.rb +75 -0
  10. data/lib/omq/cli/pair.rb +31 -0
  11. data/lib/omq/cli/peer.rb +8 -0
  12. data/lib/omq/cli/pipe.rb +249 -0
  13. data/lib/omq/cli/pub_sub.rb +14 -0
  14. data/lib/omq/cli/push_pull.rb +14 -0
  15. data/lib/omq/cli/radio_dish.rb +27 -0
  16. data/lib/omq/cli/req_rep.rb +77 -0
  17. data/lib/omq/cli/router_dealer.rb +70 -0
  18. data/lib/omq/cli/scatter_gather.rb +14 -0
  19. data/lib/omq/cli.rb +444 -0
  20. data/lib/omq/pub_sub.rb +2 -2
  21. data/lib/omq/radio_dish.rb +2 -2
  22. data/lib/omq/socket.rb +74 -27
  23. data/lib/omq/version.rb +1 -1
  24. data/lib/omq/zmtp/connection.rb +24 -3
  25. data/lib/omq/zmtp/engine.rb +179 -17
  26. data/lib/omq/zmtp/options.rb +4 -3
  27. data/lib/omq/zmtp/reactor.rb +10 -5
  28. data/lib/omq/zmtp/routing/channel.rb +8 -2
  29. data/lib/omq/zmtp/routing/fan_out.rb +38 -8
  30. data/lib/omq/zmtp/routing/pair.rb +8 -2
  31. data/lib/omq/zmtp/routing/peer.rb +7 -1
  32. data/lib/omq/zmtp/routing/push.rb +14 -7
  33. data/lib/omq/zmtp/routing/radio.rb +32 -11
  34. data/lib/omq/zmtp/routing/rep.rb +11 -7
  35. data/lib/omq/zmtp/routing/req.rb +1 -2
  36. data/lib/omq/zmtp/routing/round_robin.rb +35 -1
  37. data/lib/omq/zmtp/routing/router.rb +7 -1
  38. data/lib/omq/zmtp/routing/scatter.rb +16 -3
  39. data/lib/omq/zmtp/routing/server.rb +7 -1
  40. data/lib/omq/zmtp/routing/xsub.rb +7 -1
  41. data/lib/omq/zmtp/transport/inproc.rb +40 -5
  42. data/lib/omq/zmtp/transport/ipc.rb +9 -7
  43. data/lib/omq/zmtp/transport/tcp.rb +14 -7
  44. data/lib/omq/zmtp/writable.rb +21 -4
  45. data/lib/omq.rb +7 -0
  46. metadata +18 -3
  47. data/exe/omqcat +0 -532
@@ -12,6 +12,8 @@ module OMQ
12
12
  # their #initialize.
13
13
  #
14
14
  module FanOut
15
+ attr_reader :subscriber_joined
16
+
15
17
  private
16
18
 
17
19
  def init_fan_out(engine)
@@ -19,6 +21,9 @@ module OMQ
19
21
  @subscriptions = {} # connection => Set of prefixes
20
22
  @send_queue = Async::LimitedQueue.new(engine.options.send_hwm)
21
23
  @send_pump_started = false
24
+ @send_pump_idle = true
25
+ @conflate = engine.options.conflate
26
+ @subscriber_joined = Async::Promise.new
22
27
  end
23
28
 
24
29
  # @return [Boolean] whether the connection is subscribed to the topic
@@ -38,6 +43,7 @@ module OMQ
38
43
  #
39
44
  def on_subscribe(conn, prefix)
40
45
  @subscriptions[conn] << prefix
46
+ @subscriber_joined.resolve(conn) unless @subscriber_joined.resolved?
41
47
  end
42
48
 
43
49
  # Called when a cancel command is received from a peer.
@@ -50,23 +56,48 @@ module OMQ
50
56
  @subscriptions[conn]&.delete(prefix)
51
57
  end
52
58
 
59
+ # @return [Boolean] true when the send pump is idle (not sending a batch)
60
+ def send_pump_idle? = @send_pump_idle
61
+
62
+
53
63
  def start_send_pump
54
64
  @send_pump_started = true
55
- @tasks << Reactor.spawn_pump do
65
+ @tasks << @engine.spawn_pump_task(annotation: "send pump") do
56
66
  loop do
67
+ @send_pump_idle = true
57
68
  batch = [@send_queue.dequeue]
69
+ @send_pump_idle = false
58
70
  Routing.drain_send_queue(@send_queue, batch)
59
71
 
60
72
  written = Set.new
61
- batch.each do |parts|
62
- topic = parts.first || "".b
63
- @connections.each do |conn|
64
- next unless subscribed?(conn, topic)
73
+
74
+ if @conflate
75
+ # Keep only the last matching message per connection.
76
+ latest = {} # conn => parts
77
+ batch.each do |parts|
78
+ topic = parts.first || "".b
79
+ @connections.each do |conn|
80
+ next unless subscribed?(conn, topic)
81
+ latest[conn] = parts
82
+ end
83
+ end
84
+ latest.each do |conn, parts|
65
85
  begin
66
86
  conn.write_message(parts)
67
87
  written << conn
68
88
  rescue *ZMTP::CONNECTION_LOST
69
- # connection dead — will be cleaned up
89
+ end
90
+ end
91
+ else
92
+ batch.each do |parts|
93
+ topic = parts.first || "".b
94
+ @connections.each do |conn|
95
+ next unless subscribed?(conn, topic)
96
+ begin
97
+ conn.write_message(parts)
98
+ written << conn
99
+ rescue *ZMTP::CONNECTION_LOST
100
+ end
70
101
  end
71
102
  end
72
103
  end
@@ -74,14 +105,13 @@ module OMQ
74
105
  written.each do |conn|
75
106
  conn.flush
76
107
  rescue *ZMTP::CONNECTION_LOST
77
- # connection dead — will be cleaned up
78
108
  end
79
109
  end
80
110
  end
81
111
  end
82
112
 
83
113
  def start_subscription_listener(conn)
84
- @tasks << Reactor.spawn_pump do
114
+ @tasks << Reactor.spawn_pump(annotation: "recv pump") do
85
115
  loop do
86
116
  frame = conn.read_frame
87
117
  next unless frame.command?
@@ -17,7 +17,8 @@ module OMQ
17
17
  @connection = nil
18
18
  @recv_queue = Async::LimitedQueue.new(engine.options.recv_hwm)
19
19
  @send_queue = Async::LimitedQueue.new(engine.options.send_hwm)
20
- @tasks = []
20
+ @tasks = []
21
+ @send_pump_idle = true
21
22
  end
22
23
 
23
24
  # @return [Async::LimitedQueue]
@@ -59,10 +60,15 @@ module OMQ
59
60
 
60
61
  private
61
62
 
63
+ def send_pump_idle? = @send_pump_idle
64
+
65
+
62
66
  def start_send_pump(conn)
63
- @send_pump = Reactor.spawn_pump do
67
+ @send_pump = @engine.spawn_pump_task(annotation: "send pump") do
64
68
  loop do
69
+ @send_pump_idle = true
65
70
  batch = [@send_queue.dequeue]
71
+ @send_pump_idle = false
66
72
  Routing.drain_send_queue(@send_queue, batch)
67
73
  batch.each { |parts| conn.write_message(parts) }
68
74
  conn.flush
@@ -21,6 +21,7 @@ module OMQ
21
21
  @connections_by_routing_id = {}
22
22
  @tasks = []
23
23
  @send_pump_started = false
24
+ @send_pump_idle = true
24
25
  end
25
26
 
26
27
  # @return [Async::LimitedQueue]
@@ -59,11 +60,16 @@ module OMQ
59
60
 
60
61
  private
61
62
 
63
+ def send_pump_idle? = @send_pump_idle
64
+
65
+
62
66
  def start_send_pump
63
67
  @send_pump_started = true
64
- @tasks << Reactor.spawn_pump do
68
+ @tasks << @engine.spawn_pump_task(annotation: "send pump") do
65
69
  loop do
70
+ @send_pump_idle = true
66
71
  batch = [@send_queue.dequeue]
72
+ @send_pump_idle = false
67
73
  Routing.drain_send_queue(@send_queue, batch)
68
74
 
69
75
  written = Set.new
@@ -16,37 +16,44 @@ module OMQ
16
16
  init_round_robin(engine)
17
17
  end
18
18
 
19
+
19
20
  # @return [Async::LimitedQueue]
20
21
  #
21
22
  attr_reader :send_queue
22
23
 
24
+
23
25
  # PUSH is write-only.
24
26
  #
25
27
  def recv_queue
26
28
  raise "PUSH sockets cannot receive"
27
29
  end
28
30
 
31
+
29
32
  # @param connection [Connection]
30
33
  #
31
34
  def connection_added(connection)
32
35
  @connections << connection
33
36
  signal_connection_available
34
37
  start_send_pump unless @send_pump_started
35
- start_monitor(connection)
38
+ start_reaper(connection)
36
39
  end
37
40
 
41
+
38
42
  # @param connection [Connection]
39
43
  #
40
44
  def connection_removed(connection)
41
45
  @connections.delete(connection)
42
46
  end
43
47
 
48
+
44
49
  # @param parts [Array<String>]
45
50
  #
46
51
  def enqueue(parts)
47
52
  @send_queue.enqueue(parts)
48
53
  end
49
54
 
55
+
56
+ # Stops all background tasks (send pump, reapers).
50
57
  #
51
58
  def stop
52
59
  @tasks.each(&:stop)
@@ -55,13 +62,13 @@ module OMQ
55
62
 
56
63
  private
57
64
 
58
- # Monitors a connection for disconnection.
59
- # Write-only sockets have no recv pump, so without this monitor
60
- # a dead peer is only detected on the next send — which may
61
- # succeed if the kernel send buffer absorbs the data.
65
+
66
+ # Detects peer disconnection on write-only sockets. Without
67
+ # this, a dead peer is only noticed on the next send — which
68
+ # may succeed if the kernel send buffer absorbs the data.
62
69
  #
63
- def start_monitor(conn)
64
- @tasks << Reactor.spawn_pump do
70
+ def start_reaper(conn)
71
+ @tasks << Reactor.spawn_pump(annotation: "reaper") do
65
72
  conn.receive_message # blocks until peer disconnects
66
73
  rescue *ZMTP::CONNECTION_LOST
67
74
  @engine.connection_lost(conn)
@@ -21,6 +21,7 @@ module OMQ
21
21
  @groups = {} # connection => Set of joined groups
22
22
  @send_queue = Async::LimitedQueue.new(engine.options.send_hwm)
23
23
  @send_pump_started = false
24
+ @conflate = engine.options.conflate
24
25
  @tasks = []
25
26
  end
26
27
 
@@ -67,23 +68,44 @@ module OMQ
67
68
 
68
69
  def start_send_pump
69
70
  @send_pump_started = true
70
- @tasks << Reactor.spawn_pump do
71
+ @tasks << @engine.spawn_pump_task(annotation: "send pump") do
71
72
  loop do
73
+ @send_pump_idle = true
72
74
  batch = [@send_queue.dequeue]
75
+ @send_pump_idle = false
73
76
  Routing.drain_send_queue(@send_queue, batch)
74
77
 
75
78
  written = Set.new
76
- batch.each do |parts|
77
- group = parts[0]
78
- body = parts[1] || "".b
79
- @connections.each do |conn|
80
- next unless @groups[conn]&.include?(group)
79
+
80
+ if @conflate
81
+ # Keep only the last matching message per connection.
82
+ latest = {} # conn => [group, body]
83
+ batch.each do |parts|
84
+ group = parts[0]
85
+ body = parts[1] || "".b
86
+ @connections.each do |conn|
87
+ next unless @groups[conn]&.include?(group)
88
+ latest[conn] = [group, body]
89
+ end
90
+ end
91
+ latest.each do |conn, msg|
81
92
  begin
82
- # Wire format: group frame (MORE) + body frame
83
- conn.write_message([group, body])
93
+ conn.write_message(msg)
84
94
  written << conn
85
95
  rescue *ZMTP::CONNECTION_LOST
86
- # connection dead — will be cleaned up
96
+ end
97
+ end
98
+ else
99
+ batch.each do |parts|
100
+ group = parts[0]
101
+ body = parts[1] || "".b
102
+ @connections.each do |conn|
103
+ next unless @groups[conn]&.include?(group)
104
+ begin
105
+ conn.write_message([group, body])
106
+ written << conn
107
+ rescue *ZMTP::CONNECTION_LOST
108
+ end
87
109
  end
88
110
  end
89
111
  end
@@ -91,14 +113,13 @@ module OMQ
91
113
  written.each do |conn|
92
114
  conn.flush
93
115
  rescue *ZMTP::CONNECTION_LOST
94
- # connection dead — will be cleaned up
95
116
  end
96
117
  end
97
118
  end
98
119
  end
99
120
 
100
121
  def start_group_listener(conn)
101
- @tasks << Reactor.spawn_pump do
122
+ @tasks << Reactor.spawn_pump(annotation: "recv pump") do
102
123
  loop do
103
124
  frame = conn.read_frame
104
125
  next unless frame.command?
@@ -19,6 +19,7 @@ module OMQ
19
19
  @pending_replies = []
20
20
  @tasks = []
21
21
  @send_pump_started = false
22
+ @send_pump_idle = true
22
23
  end
23
24
 
24
25
  # @return [Async::LimitedQueue]
@@ -29,13 +30,11 @@ module OMQ
29
30
  #
30
31
  def connection_added(connection)
31
32
  transform = ->(msg) {
32
- envelope = []
33
- while msg.first && !msg.first.empty?
34
- envelope << msg.shift
35
- end
36
- msg.shift # remove empty delimiter
33
+ delimiter = msg.index(&:empty?) || msg.size
34
+ envelope = msg[0, delimiter]
35
+ body = msg[(delimiter + 1)..] || []
37
36
  @pending_replies << { conn: connection, envelope: envelope }
38
- msg
37
+ body
39
38
  }
40
39
  task = @engine.start_recv_pump(connection, @recv_queue, transform: transform)
41
40
  @tasks << task if task
@@ -64,11 +63,16 @@ module OMQ
64
63
 
65
64
  private
66
65
 
66
+ def send_pump_idle? = @send_pump_idle
67
+
68
+
67
69
  def start_send_pump
68
70
  @send_pump_started = true
69
- @tasks << Reactor.spawn_pump do
71
+ @tasks << @engine.spawn_pump_task(annotation: "send pump") do
70
72
  loop do
73
+ @send_pump_idle = true
71
74
  batch = [@send_queue.dequeue]
75
+ @send_pump_idle = false
72
76
  Routing.drain_send_queue(@send_queue, batch)
73
77
 
74
78
  written = Set.new
@@ -61,8 +61,7 @@ module OMQ
61
61
  # REQ strips the leading empty delimiter frame on receive.
62
62
  #
63
63
  def transform_recv(msg)
64
- msg.shift if msg.first&.empty?
65
- msg
64
+ msg.first&.empty? ? msg[1..] : msg
66
65
  end
67
66
  end
68
67
  end
@@ -15,20 +15,31 @@ module OMQ
15
15
  module RoundRobin
16
16
  private
17
17
 
18
+
19
+ # Initializes round-robin state for the including class.
20
+ #
21
+ # @param engine [Engine]
22
+ #
18
23
  def init_round_robin(engine)
19
24
  @connections = []
20
25
  @cycle = @connections.cycle
21
26
  @connection_available = Async::Promise.new
22
27
  @send_queue = Async::LimitedQueue.new(engine.options.send_hwm)
23
28
  @send_pump_started = false
29
+ @send_pump_idle = true
24
30
  end
25
31
 
32
+
33
+ # Resolves the connection-available promise so blocked
34
+ # senders can proceed.
35
+ #
26
36
  def signal_connection_available
27
37
  unless @connection_available.resolved?
28
38
  @connection_available.resolve(true)
29
39
  end
30
40
  end
31
41
 
42
+
32
43
  # Blocks until a connection is available, then returns
33
44
  # the next one in round-robin order.
34
45
  #
@@ -43,6 +54,7 @@ module OMQ
43
54
  retry
44
55
  end
45
56
 
57
+
46
58
  # Transforms parts before sending. Override in subclasses
47
59
  # (e.g. REQ prepends an empty delimiter frame).
48
60
  #
@@ -51,11 +63,21 @@ module OMQ
51
63
  #
52
64
  def transform_send(parts) = parts
53
65
 
66
+
67
+ # Starts the background send pump that dequeues messages
68
+ # and dispatches them round-robin across connections.
69
+ #
70
+ # @return [Boolean] true when the send pump is idle (not sending a batch)
71
+ def send_pump_idle? = @send_pump_idle
72
+
73
+
54
74
  def start_send_pump
55
75
  @send_pump_started = true
56
- @tasks << Reactor.spawn_pump do
76
+ @tasks << @engine.spawn_pump_task(annotation: "send pump") do
57
77
  loop do
78
+ @send_pump_idle = true
58
79
  batch = [@send_queue.dequeue]
80
+ @send_pump_idle = false
59
81
  Routing.drain_send_queue(@send_queue, batch)
60
82
 
61
83
  if batch.size == 1
@@ -67,6 +89,12 @@ module OMQ
67
89
  end
68
90
  end
69
91
 
92
+
93
+ # Sends a single message, retrying on a new connection if
94
+ # the current one is lost.
95
+ #
96
+ # @param parts [Array<String>]
97
+ #
70
98
  def send_with_retry(parts)
71
99
  conn = next_connection
72
100
  conn.send_message(transform_send(parts))
@@ -75,6 +103,12 @@ module OMQ
75
103
  retry
76
104
  end
77
105
 
106
+
107
+ # Sends a batch of messages, writing without flushing for
108
+ # throughput. Falls back to #send_with_retry on failure.
109
+ #
110
+ # @param batch [Array<Array<String>>]
111
+ #
78
112
  def send_batch(batch)
79
113
  written = Set.new
80
114
  batch.each_with_index do |parts, i|
@@ -21,6 +21,7 @@ module OMQ
21
21
  @connections_by_identity = {}
22
22
  @tasks = []
23
23
  @send_pump_started = false
24
+ @send_pump_idle = true
24
25
  end
25
26
 
26
27
  # @return [Async::LimitedQueue]
@@ -68,11 +69,16 @@ module OMQ
68
69
 
69
70
  private
70
71
 
72
+ def send_pump_idle? = @send_pump_idle
73
+
74
+
71
75
  def start_send_pump
72
76
  @send_pump_started = true
73
- @tasks << Reactor.spawn_pump do
77
+ @tasks << @engine.spawn_pump_task(annotation: "send pump") do
74
78
  loop do
79
+ @send_pump_idle = true
75
80
  batch = [@send_queue.dequeue]
81
+ @send_pump_idle = false
76
82
  Routing.drain_send_queue(@send_queue, batch)
77
83
 
78
84
  written = Set.new
@@ -16,37 +16,44 @@ module OMQ
16
16
  init_round_robin(engine)
17
17
  end
18
18
 
19
+
19
20
  # @return [Async::LimitedQueue]
20
21
  #
21
22
  attr_reader :send_queue
22
23
 
24
+
23
25
  # SCATTER is write-only.
24
26
  #
25
27
  def recv_queue
26
28
  raise "SCATTER sockets cannot receive"
27
29
  end
28
30
 
31
+
29
32
  # @param connection [Connection]
30
33
  #
31
34
  def connection_added(connection)
32
35
  @connections << connection
33
36
  signal_connection_available
34
37
  start_send_pump unless @send_pump_started
35
- start_monitor(connection)
38
+ start_reaper(connection)
36
39
  end
37
40
 
41
+
38
42
  # @param connection [Connection]
39
43
  #
40
44
  def connection_removed(connection)
41
45
  @connections.delete(connection)
42
46
  end
43
47
 
48
+
44
49
  # @param parts [Array<String>]
45
50
  #
46
51
  def enqueue(parts)
47
52
  @send_queue.enqueue(parts)
48
53
  end
49
54
 
55
+
56
+ # Stops all background tasks (send pump, reapers).
50
57
  #
51
58
  def stop
52
59
  @tasks.each(&:stop)
@@ -55,8 +62,14 @@ module OMQ
55
62
 
56
63
  private
57
64
 
58
- def start_monitor(conn)
59
- @tasks << Reactor.spawn_pump do
65
+
66
+ # Detects peer disconnection on write-only sockets by
67
+ # blocking on a receive that only returns on disconnect.
68
+ #
69
+ # @param conn [Connection]
70
+ #
71
+ def start_reaper(conn)
72
+ @tasks << Reactor.spawn_pump(annotation: "reaper") do
60
73
  conn.receive_message
61
74
  rescue *ZMTP::CONNECTION_LOST
62
75
  @engine.connection_lost(conn)
@@ -21,6 +21,7 @@ module OMQ
21
21
  @connections_by_routing_id = {}
22
22
  @tasks = []
23
23
  @send_pump_started = false
24
+ @send_pump_idle = true
24
25
  end
25
26
 
26
27
  # @return [Async::LimitedQueue]
@@ -59,11 +60,16 @@ module OMQ
59
60
 
60
61
  private
61
62
 
63
+ def send_pump_idle? = @send_pump_idle
64
+
65
+
62
66
  def start_send_pump
63
67
  @send_pump_started = true
64
- @tasks << Reactor.spawn_pump do
68
+ @tasks << @engine.spawn_pump_task(annotation: "send pump") do
65
69
  loop do
70
+ @send_pump_idle = true
66
71
  batch = [@send_queue.dequeue]
72
+ @send_pump_idle = false
67
73
  Routing.drain_send_queue(@send_queue, batch)
68
74
 
69
75
  written = Set.new
@@ -19,6 +19,7 @@ module OMQ
19
19
  @send_queue = Async::LimitedQueue.new(engine.options.send_hwm)
20
20
  @tasks = []
21
21
  @send_pump_started = false
22
+ @send_pump_idle = true
22
23
  end
23
24
 
24
25
  # @return [Async::LimitedQueue]
@@ -54,11 +55,16 @@ module OMQ
54
55
 
55
56
  private
56
57
 
58
+ def send_pump_idle? = @send_pump_idle
59
+
60
+
57
61
  def start_send_pump
58
62
  @send_pump_started = true
59
- @tasks << Reactor.spawn_pump do
63
+ @tasks << @engine.spawn_pump_task(annotation: "send pump") do
60
64
  loop do
65
+ @send_pump_idle = true
61
66
  parts = @send_queue.dequeue
67
+ @send_pump_idle = false
62
68
  frame = parts.first&.b
63
69
  next if frame.nil? || frame.empty?
64
70