omq 0.29.0 → 0.30.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +171 -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 +239 -301
- 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 +59 -21
- data/lib/omq/transport/ipc.rb +69 -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/socket.rb
CHANGED
|
@@ -61,6 +61,8 @@ module OMQ
|
|
|
61
61
|
:heartbeat_ttl, :heartbeat_ttl=,
|
|
62
62
|
:heartbeat_timeout, :heartbeat_timeout=,
|
|
63
63
|
:max_message_size, :max_message_size=,
|
|
64
|
+
:max_message_parts, :max_message_parts=,
|
|
65
|
+
:max_peer_subscriptions, :max_peer_subscriptions=,
|
|
64
66
|
:sndbuf, :sndbuf=,
|
|
65
67
|
:rcvbuf, :rcvbuf=,
|
|
66
68
|
:on_mute, :on_mute=,
|
|
@@ -208,7 +210,7 @@ module OMQ
|
|
|
208
210
|
|
|
209
211
|
ensure_parent_task
|
|
210
212
|
|
|
211
|
-
queue =
|
|
213
|
+
queue = MonitorQueue.new
|
|
212
214
|
@engine.monitor_queue = queue
|
|
213
215
|
@engine.verbose_monitor = verbose
|
|
214
216
|
|
|
@@ -220,6 +222,7 @@ module OMQ
|
|
|
220
222
|
rescue Async::Stop
|
|
221
223
|
ensure
|
|
222
224
|
@engine.monitor_queue = nil
|
|
225
|
+
queue.close
|
|
223
226
|
block.call(MonitorEvent.new(type: :monitor_stopped))
|
|
224
227
|
end
|
|
225
228
|
end
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OMQ
|
|
4
|
+
module Transport
|
|
5
|
+
# Error handling shared by stream listener accept loops (TCP, IPC).
|
|
6
|
+
#
|
|
7
|
+
# A listener must survive errors that a later accept(2) recovers
|
|
8
|
+
# from. Without this, one EMFILE ended the accept task for good and
|
|
9
|
+
# left the endpoint bound but deaf.
|
|
10
|
+
#
|
|
11
|
+
module AcceptLoop
|
|
12
|
+
# accept(2) ran out of a resource. Retry after a pause so a full fd
|
|
13
|
+
# table doesn't spin the loop.
|
|
14
|
+
BACKOFF_ERRORS = [
|
|
15
|
+
Errno::EMFILE,
|
|
16
|
+
Errno::ENFILE,
|
|
17
|
+
Errno::ENOBUFS,
|
|
18
|
+
Errno::ENOMEM,
|
|
19
|
+
].freeze
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# One client failed during accept(2) or socket setup. Drop it and
|
|
23
|
+
# accept the next one right away.
|
|
24
|
+
CLIENT_ERRORS = %i[ECONNABORTED ECONNRESET EPROTO EPERM EINVAL ENOTCONN].filter_map do |name|
|
|
25
|
+
Errno.const_get(name) if Errno.const_defined?(name)
|
|
26
|
+
end.freeze
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
# Every error an accept loop recovers from.
|
|
30
|
+
RECOVERABLE_ERRORS = (BACKOFF_ERRORS + CLIENT_ERRORS).freeze
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# Reports a recoverable accept error and backs off if the process
|
|
34
|
+
# is out of a resource.
|
|
35
|
+
#
|
|
36
|
+
# @param error [SystemCallError]
|
|
37
|
+
# @param engine [Engine]
|
|
38
|
+
# @param endpoint [String]
|
|
39
|
+
# @return [void]
|
|
40
|
+
#
|
|
41
|
+
def self.recover(error, engine, endpoint)
|
|
42
|
+
engine.emit_monitor_event(:accept_failed, endpoint: endpoint, detail: { error: error })
|
|
43
|
+
|
|
44
|
+
if BACKOFF_ERRORS.include?(error.class)
|
|
45
|
+
sleep backoff(engine.options)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# @param options [Options]
|
|
51
|
+
# @return [Numeric] the shortest reconnect interval
|
|
52
|
+
#
|
|
53
|
+
def self.backoff(options)
|
|
54
|
+
ri = options.reconnect_interval
|
|
55
|
+
ri.is_a?(Range) ? ri.begin : ri
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
@@ -6,11 +6,17 @@ require_relative "compression_arena"
|
|
|
6
6
|
module OMQ
|
|
7
7
|
module Transport
|
|
8
8
|
class CompressedConnection < SimpleDelegator
|
|
9
|
+
# The most a message may decode to when `max_message_size` is unset:
|
|
10
|
+
# just under 1 GiB, as in omq.rs. Sizes a peer declares are checked
|
|
11
|
+
# against it before anything is allocated for them.
|
|
12
|
+
DEFAULT_DECODE_BUDGET = (1 << 30) - 1
|
|
13
|
+
|
|
9
14
|
attr_reader :last_wire_size_in
|
|
10
15
|
|
|
11
16
|
def initialize(conn, max_message_size:, arena_sentinel:, protocol_error:)
|
|
12
17
|
super(conn)
|
|
13
18
|
@max_message_size = max_message_size
|
|
19
|
+
@decode_budget = max_message_size || DEFAULT_DECODE_BUDGET
|
|
14
20
|
@arena_sentinel = arena_sentinel
|
|
15
21
|
@protocol_error = protocol_error
|
|
16
22
|
@pending_messages = []
|
|
@@ -122,6 +122,9 @@ module OMQ
|
|
|
122
122
|
else
|
|
123
123
|
(@pending_direct ||= []) << parts
|
|
124
124
|
end
|
|
125
|
+
rescue Async::Queue::ClosedError, ClosedQueueError
|
|
126
|
+
# The peer socket closed and with it the queue we write into.
|
|
127
|
+
raise IOError, "closed"
|
|
125
128
|
end
|
|
126
129
|
|
|
127
130
|
|
|
@@ -186,6 +189,8 @@ module OMQ
|
|
|
186
189
|
def send_command(command)
|
|
187
190
|
raise IOError, "closed" if @closed
|
|
188
191
|
@send_queue.enqueue([:command, command])
|
|
192
|
+
rescue Async::Queue::ClosedError, ClosedQueueError
|
|
193
|
+
raise IOError, "closed"
|
|
189
194
|
end
|
|
190
195
|
|
|
191
196
|
|
|
@@ -215,6 +220,8 @@ module OMQ
|
|
|
215
220
|
@closed = true
|
|
216
221
|
@closed_signal.resolve(true)
|
|
217
222
|
@send_queue&.enqueue(nil) # close sentinel
|
|
223
|
+
rescue Async::Queue::ClosedError, ClosedQueueError
|
|
224
|
+
# peer already closed its queues
|
|
218
225
|
end
|
|
219
226
|
|
|
220
227
|
|
data/lib/omq/transport/inproc.rb
CHANGED
|
@@ -74,16 +74,36 @@ module OMQ
|
|
|
74
74
|
end
|
|
75
75
|
|
|
76
76
|
|
|
77
|
+
# Creates an inproc dialer.
|
|
78
|
+
#
|
|
79
|
+
# @param endpoint [String] e.g. "ruby://my-endpoint"
|
|
80
|
+
# @param engine [Engine] the connecting engine
|
|
81
|
+
# @return [Dialer]
|
|
82
|
+
#
|
|
83
|
+
def dialer(endpoint, engine, **)
|
|
84
|
+
Dialer.new(canonical_endpoint(endpoint), engine)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
|
|
77
88
|
# Connects to a bound inproc endpoint.
|
|
78
89
|
#
|
|
79
90
|
# @param endpoint [String] e.g. "ruby://my-endpoint"
|
|
80
91
|
# @param engine [Engine] the connecting engine
|
|
92
|
+
# @param wait [Boolean] wait up to one reconnect interval for a bind
|
|
81
93
|
# @return [void]
|
|
94
|
+
# @raise [Errno::ECONNREFUSED] if nothing is bound. It is in
|
|
95
|
+
# {CONNECTION_FAILED}, so {Engine::Reconnect} retries.
|
|
82
96
|
#
|
|
83
|
-
def connect(endpoint, engine, **)
|
|
97
|
+
def connect(endpoint, engine, wait: false, **)
|
|
84
98
|
endpoint = canonical_endpoint(endpoint)
|
|
85
99
|
bound_engine = @mutex.synchronize { @registry[endpoint] }
|
|
86
|
-
|
|
100
|
+
|
|
101
|
+
if bound_engine.nil? && wait
|
|
102
|
+
bound_engine = await_bind(endpoint, engine)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
raise Errno::ECONNREFUSED, endpoint unless bound_engine
|
|
106
|
+
|
|
87
107
|
establish_link(engine, bound_engine, endpoint)
|
|
88
108
|
end
|
|
89
109
|
|
|
@@ -134,7 +154,8 @@ module OMQ
|
|
|
134
154
|
client_pipe, server_pipe = make_pipe_pair client_engine, server_engine,
|
|
135
155
|
client_type, server_type, needs_cmds
|
|
136
156
|
|
|
137
|
-
|
|
157
|
+
# A refused client closes its pipe; don't hand the server a dead peer.
|
|
158
|
+
client_engine.connection_ready(client_pipe, endpoint: endpoint, dialed: true) or return
|
|
138
159
|
server_engine.connection_ready(server_pipe, endpoint: endpoint)
|
|
139
160
|
end
|
|
140
161
|
|
|
@@ -176,9 +197,13 @@ module OMQ
|
|
|
176
197
|
end
|
|
177
198
|
|
|
178
199
|
|
|
200
|
+
# Waits up to one reconnect interval for +endpoint+ to be bound.
|
|
201
|
+
# Matches ZMQ 4.x: connect to unbound inproc succeeds silently,
|
|
202
|
+
# and the caller retries in the background.
|
|
203
|
+
#
|
|
204
|
+
# @return [Engine, nil] the bound engine, or nil on timeout
|
|
205
|
+
#
|
|
179
206
|
def await_bind(endpoint, engine)
|
|
180
|
-
# Endpoint not bound yet — wait briefly then start background retry.
|
|
181
|
-
# Matches ZMQ 4.x: connect to unbound inproc succeeds silently.
|
|
182
207
|
ri = engine.options.reconnect_interval
|
|
183
208
|
timeout = ri.is_a?(Range) ? ri.begin : ri
|
|
184
209
|
promise = Async::Promise.new
|
|
@@ -196,31 +221,44 @@ module OMQ
|
|
|
196
221
|
@waiters[endpoint].delete(promise)
|
|
197
222
|
end
|
|
198
223
|
|
|
199
|
-
start_connect_retry(endpoint, engine)
|
|
200
224
|
nil
|
|
201
225
|
end
|
|
202
226
|
end
|
|
227
|
+
end
|
|
203
228
|
|
|
204
229
|
|
|
205
|
-
|
|
206
|
-
|
|
230
|
+
# An inproc dialer: stateful factory for outgoing connections.
|
|
231
|
+
#
|
|
232
|
+
# Created once per {Engine#connect}, stored in +@dialers[endpoint]+.
|
|
233
|
+
# {Engine#connect} calls {#connect} synchronously with +wait: true+;
|
|
234
|
+
# {Engine::Reconnect} calls it while nothing is bound, and again
|
|
235
|
+
# after the bound peer goes away.
|
|
236
|
+
#
|
|
237
|
+
class Dialer
|
|
238
|
+
# @return [String] the endpoint this dialer connects to
|
|
207
239
|
#
|
|
208
|
-
|
|
209
|
-
|
|
240
|
+
attr_reader :endpoint
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
# @param endpoint [String] e.g. "ruby://my-endpoint"
|
|
244
|
+
# @param engine [Engine]
|
|
210
245
|
#
|
|
211
|
-
def
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
bound_engine = @mutex.synchronize { @registry[endpoint] }
|
|
246
|
+
def initialize(endpoint, engine)
|
|
247
|
+
@endpoint = endpoint
|
|
248
|
+
@engine = engine
|
|
249
|
+
end
|
|
216
250
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
251
|
+
|
|
252
|
+
# Links to the bound endpoint.
|
|
253
|
+
#
|
|
254
|
+
# @param wait [Boolean] wait up to one reconnect interval for a bind
|
|
255
|
+
# @return [void]
|
|
256
|
+
# @raise [Errno::ECONNREFUSED] if nothing is bound
|
|
257
|
+
#
|
|
258
|
+
def connect(wait: false)
|
|
259
|
+
Inproc.connect(@endpoint, @engine, wait: wait)
|
|
223
260
|
end
|
|
261
|
+
|
|
224
262
|
end
|
|
225
263
|
|
|
226
264
|
|
data/lib/omq/transport/ipc.rb
CHANGED
|
@@ -30,12 +30,17 @@ module OMQ
|
|
|
30
30
|
# @param engine [Engine]
|
|
31
31
|
# @return [Listener]
|
|
32
32
|
#
|
|
33
|
+
# @raise [Errno::EADDRINUSE] if a live socket or a non-socket file
|
|
34
|
+
# already occupies the path
|
|
35
|
+
#
|
|
33
36
|
def listener(endpoint, engine, **)
|
|
34
37
|
path = parse_path(endpoint)
|
|
35
38
|
sock_path = to_socket_path(path)
|
|
36
39
|
|
|
37
|
-
#
|
|
38
|
-
|
|
40
|
+
# Reject a too-long path before touching the filesystem.
|
|
41
|
+
::Socket.sockaddr_un(sock_path)
|
|
42
|
+
|
|
43
|
+
remove_stale_socket(path) unless abstract?(path)
|
|
39
44
|
|
|
40
45
|
server = UNIXServer.new(sock_path)
|
|
41
46
|
|
|
@@ -43,6 +48,29 @@ module OMQ
|
|
|
43
48
|
end
|
|
44
49
|
|
|
45
50
|
|
|
51
|
+
# Removes a socket file left behind by a process that is gone.
|
|
52
|
+
# A socket that still accepts connections, or a file that isn't
|
|
53
|
+
# a socket, is never replaced.
|
|
54
|
+
#
|
|
55
|
+
# @param path [String] filesystem path
|
|
56
|
+
# @raise [Errno::EADDRINUSE]
|
|
57
|
+
#
|
|
58
|
+
def remove_stale_socket(path)
|
|
59
|
+
return unless File.exist?(path) || File.symlink?(path)
|
|
60
|
+
|
|
61
|
+
unless File.socket?(path)
|
|
62
|
+
raise Errno::EADDRINUSE, "#{path} exists and is not a socket"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
probe_socket(path)
|
|
66
|
+
raise Errno::EADDRINUSE, "#{path} is in use"
|
|
67
|
+
rescue Errno::ECONNREFUSED
|
|
68
|
+
File.delete(path)
|
|
69
|
+
rescue Errno::ENOENT
|
|
70
|
+
# vanished meanwhile
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
|
|
46
74
|
# Creates an IPC dialer for an endpoint.
|
|
47
75
|
#
|
|
48
76
|
# @param endpoint [String]
|
|
@@ -62,11 +90,11 @@ module OMQ
|
|
|
62
90
|
#
|
|
63
91
|
def apply_buffer_sizes(sock, options)
|
|
64
92
|
if options.sndbuf
|
|
65
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, options.sndbuf)
|
|
93
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options.sndbuf)
|
|
66
94
|
end
|
|
67
95
|
|
|
68
96
|
if options.rcvbuf
|
|
69
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVBUF, options.rcvbuf)
|
|
97
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options.rcvbuf)
|
|
70
98
|
end
|
|
71
99
|
end
|
|
72
100
|
|
|
@@ -94,6 +122,19 @@ module OMQ
|
|
|
94
122
|
def abstract?(path)
|
|
95
123
|
path.start_with?("@")
|
|
96
124
|
end
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# Connects to +path+ and hangs up. Raises ECONNREFUSED if nothing
|
|
131
|
+
# listens there.
|
|
132
|
+
#
|
|
133
|
+
def probe_socket(path)
|
|
134
|
+
UNIXSocket.new(path).close
|
|
135
|
+
rescue Errno::EAGAIN
|
|
136
|
+
# Backlog full: someone is listening.
|
|
137
|
+
end
|
|
97
138
|
end
|
|
98
139
|
|
|
99
140
|
|
|
@@ -152,6 +193,14 @@ module OMQ
|
|
|
152
193
|
@path = path
|
|
153
194
|
@engine = engine
|
|
154
195
|
@task = nil
|
|
196
|
+
|
|
197
|
+
# Remember which file we created, by absolute path and inode,
|
|
198
|
+
# so #stop never unlinks a file that another socket bound
|
|
199
|
+
# since, even after a chdir.
|
|
200
|
+
unless IPC.abstract?(path)
|
|
201
|
+
@file_path = File.expand_path(path)
|
|
202
|
+
@file_id = file_id(@file_path)
|
|
203
|
+
end
|
|
155
204
|
end
|
|
156
205
|
|
|
157
206
|
|
|
@@ -170,6 +219,9 @@ module OMQ
|
|
|
170
219
|
Async::Task.current.defer_stop do
|
|
171
220
|
yield IO::Stream::Buffered.wrap(client)
|
|
172
221
|
end
|
|
222
|
+
rescue *AcceptLoop::RECOVERABLE_ERRORS => error
|
|
223
|
+
client&.close rescue nil
|
|
224
|
+
AcceptLoop.recover(error, @engine, @endpoint)
|
|
173
225
|
end
|
|
174
226
|
rescue Async::Stop
|
|
175
227
|
rescue IOError
|
|
@@ -190,12 +242,22 @@ module OMQ
|
|
|
190
242
|
@task&.stop
|
|
191
243
|
@server.close rescue nil
|
|
192
244
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
File.delete(@path) rescue nil
|
|
245
|
+
if @file_id && file_id(@file_path) == @file_id
|
|
246
|
+
File.delete(@file_path) rescue nil
|
|
196
247
|
end
|
|
197
248
|
end
|
|
198
249
|
|
|
250
|
+
|
|
251
|
+
private
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def file_id(path)
|
|
255
|
+
stat = File.lstat(path)
|
|
256
|
+
[stat.dev, stat.ino]
|
|
257
|
+
rescue SystemCallError
|
|
258
|
+
nil
|
|
259
|
+
end
|
|
260
|
+
|
|
199
261
|
end
|
|
200
262
|
end
|
|
201
263
|
end
|
data/lib/omq/transport/tcp.rb
CHANGED
|
@@ -29,6 +29,8 @@ module OMQ
|
|
|
29
29
|
# @return [Listener]
|
|
30
30
|
#
|
|
31
31
|
def listener(endpoint, engine, **)
|
|
32
|
+
# libzmq's wildcard port: any free port, like :0.
|
|
33
|
+
endpoint = endpoint.sub(/:\*\z/, ":0")
|
|
32
34
|
host, port = self.parse_endpoint(endpoint)
|
|
33
35
|
lookup_host = normalize_bind_host(host)
|
|
34
36
|
|
|
@@ -115,9 +117,7 @@ module OMQ
|
|
|
115
117
|
# doesn't block the retry loop. Floor at 0.5s for real-network latency.
|
|
116
118
|
#
|
|
117
119
|
def connect_timeout(options)
|
|
118
|
-
|
|
119
|
-
ri = ri.end if ri.is_a?(Range)
|
|
120
|
-
[ri, 0.5].max
|
|
120
|
+
options.attempt_timeout
|
|
121
121
|
end
|
|
122
122
|
|
|
123
123
|
|
|
@@ -140,11 +140,11 @@ module OMQ
|
|
|
140
140
|
#
|
|
141
141
|
def apply_buffer_sizes(sock, options)
|
|
142
142
|
if options.sndbuf
|
|
143
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_SNDBUF, options.sndbuf)
|
|
143
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, options.sndbuf)
|
|
144
144
|
end
|
|
145
145
|
|
|
146
146
|
if options.rcvbuf
|
|
147
|
-
sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_RCVBUF, options.rcvbuf)
|
|
147
|
+
sock.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_RCVBUF, options.rcvbuf)
|
|
148
148
|
end
|
|
149
149
|
end
|
|
150
150
|
end
|
|
@@ -234,6 +234,9 @@ module OMQ
|
|
|
234
234
|
Async::Task.current.defer_stop do
|
|
235
235
|
yield IO::Stream::Buffered.wrap(client)
|
|
236
236
|
end
|
|
237
|
+
rescue *AcceptLoop::RECOVERABLE_ERRORS => error
|
|
238
|
+
client&.close rescue nil
|
|
239
|
+
AcceptLoop.recover(error, @engine, @endpoint)
|
|
237
240
|
end
|
|
238
241
|
rescue Async::Stop
|
|
239
242
|
rescue IOError
|
data/lib/omq/transport/udp.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "socket"
|
|
4
4
|
require "uri"
|
|
5
5
|
require "set"
|
|
6
|
+
require "ipaddr"
|
|
6
7
|
|
|
7
8
|
module OMQ
|
|
8
9
|
module Transport
|
|
@@ -27,15 +28,50 @@ module OMQ
|
|
|
27
28
|
# @param engine [Engine]
|
|
28
29
|
# @return [Listener]
|
|
29
30
|
#
|
|
31
|
+
# A multicast address joins its group, and several DISH sockets
|
|
32
|
+
# may bind the same group and port.
|
|
33
|
+
#
|
|
30
34
|
def listener(endpoint, engine, **)
|
|
31
35
|
host, port = parse_endpoint(endpoint)
|
|
32
|
-
host
|
|
33
|
-
socket
|
|
34
|
-
|
|
36
|
+
addr = Addrinfo.udp(host == "*" ? "0.0.0.0" : host, port)
|
|
37
|
+
socket = UDPSocket.new(addr.afamily)
|
|
38
|
+
|
|
39
|
+
if multicast?(addr)
|
|
40
|
+
socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, true)
|
|
41
|
+
socket.bind(addr.ip_address, port)
|
|
42
|
+
join_group(socket, addr)
|
|
43
|
+
else
|
|
44
|
+
socket.bind(addr.ip_address, port)
|
|
45
|
+
end
|
|
46
|
+
|
|
35
47
|
Listener.new(endpoint, socket, engine)
|
|
36
48
|
end
|
|
37
49
|
|
|
38
50
|
|
|
51
|
+
# @param addr [Addrinfo]
|
|
52
|
+
# @return [Boolean]
|
|
53
|
+
#
|
|
54
|
+
def multicast?(addr)
|
|
55
|
+
addr.ipv4_multicast? || addr.ipv6_multicast?
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
# Joins the multicast group +addr+ on the default interface.
|
|
60
|
+
#
|
|
61
|
+
# @param socket [UDPSocket]
|
|
62
|
+
# @param addr [Addrinfo]
|
|
63
|
+
#
|
|
64
|
+
def join_group(socket, addr)
|
|
65
|
+
group = IPAddr.new(addr.ip_address)
|
|
66
|
+
|
|
67
|
+
if addr.ipv6?
|
|
68
|
+
socket.setsockopt(::Socket::IPPROTO_IPV6, ::Socket::IPV6_JOIN_GROUP, group.hton + [0].pack("I"))
|
|
69
|
+
else
|
|
70
|
+
socket.setsockopt(::Socket::IPPROTO_IP, ::Socket::IP_ADD_MEMBERSHIP, group.hton + IPAddr.new("0.0.0.0").hton)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
|
|
39
75
|
# Creates a UDP dialer for a RADIO socket.
|
|
40
76
|
#
|
|
41
77
|
# @param endpoint [String] e.g. "udp://127.0.0.1:5555"
|
|
@@ -69,6 +105,22 @@ module OMQ
|
|
|
69
105
|
end
|
|
70
106
|
|
|
71
107
|
|
|
108
|
+
# Sizes the send buffer of a RADIO socket. Uses +sndbuf+ if set;
|
|
109
|
+
# otherwise makes sure a maximum-size datagram fits (macOS
|
|
110
|
+
# defaults to 9216 bytes and refuses larger sends).
|
|
111
|
+
#
|
|
112
|
+
# @param socket [UDPSocket]
|
|
113
|
+
# @param options [Options]
|
|
114
|
+
#
|
|
115
|
+
def apply_send_buffer(socket, options)
|
|
116
|
+
size = options.sndbuf
|
|
117
|
+
size ||= MAX_DATAGRAM_SIZE unless socket.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF).int >= MAX_DATAGRAM_SIZE
|
|
118
|
+
socket.setsockopt(::Socket::SOL_SOCKET, ::Socket::SO_SNDBUF, size) if size
|
|
119
|
+
rescue SystemCallError
|
|
120
|
+
# keep the OS default
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
|
|
72
124
|
# Decodes a UDP datagram into [group, body].
|
|
73
125
|
#
|
|
74
126
|
# @param data [String] raw datagram bytes
|
|
@@ -110,10 +162,15 @@ module OMQ
|
|
|
110
162
|
#
|
|
111
163
|
# @return [void]
|
|
112
164
|
#
|
|
165
|
+
# Resolves the host once, so sends don't hit DNS, and picks the
|
|
166
|
+
# socket family to match (IPv4 or IPv6).
|
|
167
|
+
#
|
|
113
168
|
def connect
|
|
114
169
|
host, port = UDP.parse_endpoint(@endpoint)
|
|
115
|
-
|
|
116
|
-
|
|
170
|
+
addr = Addrinfo.udp(host, port)
|
|
171
|
+
socket = UDPSocket.new(addr.afamily)
|
|
172
|
+
UDP.apply_send_buffer(socket, @engine.options)
|
|
173
|
+
conn = RadioConnection.new(socket, addr)
|
|
117
174
|
@engine.connection_ready(conn, endpoint: @endpoint)
|
|
118
175
|
end
|
|
119
176
|
|
|
@@ -127,13 +184,11 @@ module OMQ
|
|
|
127
184
|
#
|
|
128
185
|
class RadioConnection
|
|
129
186
|
# @param socket [UDPSocket]
|
|
130
|
-
# @param
|
|
131
|
-
# @param port [Integer]
|
|
187
|
+
# @param addr [Addrinfo] resolved destination
|
|
132
188
|
#
|
|
133
|
-
def initialize(socket,
|
|
134
|
-
@socket
|
|
135
|
-
@
|
|
136
|
-
@port = port
|
|
189
|
+
def initialize(socket, addr)
|
|
190
|
+
@socket = socket
|
|
191
|
+
@sockaddr = addr.to_sockaddr
|
|
137
192
|
end
|
|
138
193
|
|
|
139
194
|
|
|
@@ -141,11 +196,16 @@ module OMQ
|
|
|
141
196
|
#
|
|
142
197
|
# @param parts [Array<String>] [group, body]
|
|
143
198
|
#
|
|
199
|
+
# Drops what UDP can't carry: a datagram over
|
|
200
|
+
# {MAX_DATAGRAM_SIZE}, or one the OS refuses as too large.
|
|
201
|
+
#
|
|
144
202
|
def write_message(parts)
|
|
145
203
|
group, body = parts
|
|
146
204
|
datagram = UDP.encode_datagram(group.to_s, body.to_s)
|
|
147
|
-
|
|
148
|
-
|
|
205
|
+
return if datagram.bytesize > MAX_DATAGRAM_SIZE
|
|
206
|
+
|
|
207
|
+
@socket.send(datagram, 0, @sockaddr)
|
|
208
|
+
rescue Errno::ECONNREFUSED, Errno::ENETUNREACH, Errno::EMSGSIZE
|
|
149
209
|
# UDP fire-and-forget — drop silently
|
|
150
210
|
end
|
|
151
211
|
|
|
@@ -178,6 +238,10 @@ module OMQ
|
|
|
178
238
|
# never transmitted on the wire.
|
|
179
239
|
#
|
|
180
240
|
class DishConnection
|
|
241
|
+
# Datagrams skipped in a row before yielding to other fibers.
|
|
242
|
+
SKIP_BUDGET = 64
|
|
243
|
+
|
|
244
|
+
|
|
181
245
|
# @param socket [UDPSocket] bound socket
|
|
182
246
|
#
|
|
183
247
|
def initialize(socket)
|
|
@@ -194,13 +258,24 @@ module OMQ
|
|
|
194
258
|
# @return [Array<String>] [group, body], both binary-frozen
|
|
195
259
|
#
|
|
196
260
|
def receive_message
|
|
261
|
+
skipped = 0
|
|
262
|
+
|
|
197
263
|
loop do
|
|
198
264
|
data, = @socket.recvfrom_nonblock(MAX_DATAGRAM_SIZE)
|
|
199
265
|
parts = UDP.decode_datagram(data)
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
266
|
+
|
|
267
|
+
if parts && @groups.include?(parts[0].b)
|
|
268
|
+
return parts
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# Malformed or for a group we didn't join. A flood of these
|
|
272
|
+
# must not keep the reactor from running other fibers.
|
|
273
|
+
skipped += 1
|
|
274
|
+
|
|
275
|
+
if skipped >= SKIP_BUDGET
|
|
276
|
+
skipped = 0
|
|
277
|
+
Async::Task.current.yield
|
|
278
|
+
end
|
|
204
279
|
rescue IO::WaitReadable
|
|
205
280
|
@socket.wait_readable
|
|
206
281
|
retry
|
data/lib/omq/version.rb
CHANGED
data/lib/omq.rb
CHANGED
|
@@ -7,6 +7,7 @@ require "io/stream"
|
|
|
7
7
|
# Core
|
|
8
8
|
require_relative "omq/version"
|
|
9
9
|
require_relative "omq/constants"
|
|
10
|
+
require_relative "omq/monitor_queue"
|
|
10
11
|
require_relative "omq/backend"
|
|
11
12
|
require_relative "omq/reactor"
|
|
12
13
|
require_relative "omq/options"
|
|
@@ -28,6 +29,7 @@ require_relative "omq/engine"
|
|
|
28
29
|
OMQ::Backend.register(:ruby, OMQ::Engine)
|
|
29
30
|
|
|
30
31
|
# Transport
|
|
32
|
+
require_relative "omq/transport/accept_loop"
|
|
31
33
|
require_relative "omq/transport/inproc"
|
|
32
34
|
require_relative "omq/transport/tcp"
|
|
33
35
|
require_relative "omq/transport/ipc"
|