mankai-sockets 0.1.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 +7 -0
- data/CHANGELOG.md +28 -0
- data/LICENSE +20 -0
- data/PROTOCOL.md +111 -0
- data/README.md +630 -0
- data/js/mankai-sockets.js +244 -0
- data/js/package.json +4 -0
- data/js/test/client.test.js +225 -0
- data/lib/mankai/sockets/authentication.rb +72 -0
- data/lib/mankai/sockets/backends/memory.rb +69 -0
- data/lib/mankai/sockets/broadcast.rb +22 -0
- data/lib/mankai/sockets/broadcaster.rb +43 -0
- data/lib/mankai/sockets/channel.rb +82 -0
- data/lib/mankai/sockets/channel_map.rb +130 -0
- data/lib/mankai/sockets/cli.rb +157 -0
- data/lib/mankai/sockets/close_code.rb +57 -0
- data/lib/mankai/sockets/context.rb +24 -0
- data/lib/mankai/sockets/endpoint.rb +60 -0
- data/lib/mankai/sockets/errors.rb +20 -0
- data/lib/mankai/sockets/executor.rb +61 -0
- data/lib/mankai/sockets/frames.rb +131 -0
- data/lib/mankai/sockets/hub.rb +85 -0
- data/lib/mankai/sockets/identity.rb +21 -0
- data/lib/mankai/sockets/instrumenter.rb +65 -0
- data/lib/mankai/sockets/limits.rb +37 -0
- data/lib/mankai/sockets/mailbox.rb +58 -0
- data/lib/mankai/sockets/message.rb +24 -0
- data/lib/mankai/sockets/origin_policy.rb +117 -0
- data/lib/mankai/sockets/protocol.rb +186 -0
- data/lib/mankai/sockets/provider_source.rb +224 -0
- data/lib/mankai/sockets/server.rb +106 -0
- data/lib/mankai/sockets/session.rb +331 -0
- data/lib/mankai/sockets/session_dependencies.rb +19 -0
- data/lib/mankai/sockets/session_factory.rb +50 -0
- data/lib/mankai/sockets/sessions.rb +49 -0
- data/lib/mankai/sockets/stream.rb +70 -0
- data/lib/mankai/sockets/subscription.rb +55 -0
- data/lib/mankai/sockets/subscriptions.rb +54 -0
- data/lib/mankai/sockets/testing/backend_examples.rb +69 -0
- data/lib/mankai/sockets/testing.rb +151 -0
- data/lib/mankai/sockets/tokens.rb +96 -0
- data/lib/mankai/sockets/transport/reactor.rb +152 -0
- data/lib/mankai/sockets/transport/socket.rb +150 -0
- data/lib/mankai/sockets/version.rb +9 -0
- data/lib/mankai/sockets.rb +40 -0
- data/lib/mankai-sockets.rb +3 -0
- data/mankai-sockets.gemspec +39 -0
- metadata +193 -0
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "dry/monads"
|
|
4
|
+
|
|
5
|
+
module Mankai
|
|
6
|
+
module Sockets
|
|
7
|
+
# The protocol state machine for one connection. Every public entry point only posts to the
|
|
8
|
+
# mailbox; all state changes happen there, one command at a time.
|
|
9
|
+
#
|
|
10
|
+
# @api private
|
|
11
|
+
# @since 0.1.0
|
|
12
|
+
class Session
|
|
13
|
+
include Dry::Monads[:result]
|
|
14
|
+
|
|
15
|
+
# @api private
|
|
16
|
+
# @since 0.1.0
|
|
17
|
+
attr_reader :id
|
|
18
|
+
|
|
19
|
+
# @api private
|
|
20
|
+
# @since 0.1.0
|
|
21
|
+
def identity_key
|
|
22
|
+
@identity&.key
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @api private
|
|
26
|
+
# @since 0.1.0
|
|
27
|
+
def initialize(id:, socket:, mailbox:, request:, authentication:, channels:, hub:, sessions:,
|
|
28
|
+
limits:, instrumenter:, clock:)
|
|
29
|
+
@id = id
|
|
30
|
+
@socket = socket
|
|
31
|
+
@mailbox = mailbox
|
|
32
|
+
@request = request
|
|
33
|
+
@authentication = authentication
|
|
34
|
+
@channels = channels
|
|
35
|
+
@hub = hub
|
|
36
|
+
@sessions = sessions
|
|
37
|
+
@limits = limits
|
|
38
|
+
@instrumenter = instrumenter
|
|
39
|
+
@clock = clock
|
|
40
|
+
@subscriptions = Subscriptions.new
|
|
41
|
+
@state = :awaiting_hello
|
|
42
|
+
@created_at = clock.call
|
|
43
|
+
@last_seen_at = @created_at
|
|
44
|
+
@last_ping_at = @created_at
|
|
45
|
+
@identity = nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @api private
|
|
49
|
+
# @since 0.1.0
|
|
50
|
+
def text_received(text)
|
|
51
|
+
post { handle_text(text) }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# @api private
|
|
55
|
+
# @since 0.1.0
|
|
56
|
+
def socket_closed(code)
|
|
57
|
+
post { finish(CloseCode.new(code: code, reason: "closed")) }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @api private
|
|
61
|
+
# @since 0.1.0
|
|
62
|
+
def broadcast_received(broadcast)
|
|
63
|
+
post { deliver(broadcast) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# @api private
|
|
67
|
+
# @since 0.1.0
|
|
68
|
+
def clock_ticked(now)
|
|
69
|
+
post { tick(now) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# @api private
|
|
73
|
+
# @since 0.1.0
|
|
74
|
+
def close_requested(close_code)
|
|
75
|
+
post { finish(close_code) }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# @api private
|
|
81
|
+
# @since 0.1.0
|
|
82
|
+
def post(&block)
|
|
83
|
+
@mailbox.post do
|
|
84
|
+
block.call unless @state == :closed
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# @api private
|
|
89
|
+
# @since 0.1.0
|
|
90
|
+
def handle_text(text)
|
|
91
|
+
@last_seen_at = @clock.call
|
|
92
|
+
result = Protocol.decode(text)
|
|
93
|
+
return finish(result.failure) if result.failure?
|
|
94
|
+
|
|
95
|
+
frame = result.value!
|
|
96
|
+
return finish(CloseCode::INVALID_FRAME) if @state == :awaiting_hello && !frame.is_a?(Frames::Hello)
|
|
97
|
+
|
|
98
|
+
case frame
|
|
99
|
+
in Frames::Hello then hello(frame)
|
|
100
|
+
in Frames::Subscribe then subscribe(frame)
|
|
101
|
+
in Frames::Unsubscribe then unsubscribe(frame)
|
|
102
|
+
in Frames::Action then action(frame)
|
|
103
|
+
in Frames::Ping then send_frame("pong")
|
|
104
|
+
in Frames::Pong then nil
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# @api private
|
|
109
|
+
# @since 0.1.0
|
|
110
|
+
def hello(frame)
|
|
111
|
+
return finish(CloseCode::DUPLICATE_HELLO) unless @state == :awaiting_hello
|
|
112
|
+
|
|
113
|
+
result = @authentication.call(@request, frame.token, @id) do |exception|
|
|
114
|
+
report(exception)
|
|
115
|
+
end
|
|
116
|
+
return finish(result.failure) if result.failure?
|
|
117
|
+
|
|
118
|
+
identity = result.value!
|
|
119
|
+
count = @sessions.count_for_identity(identity.key)
|
|
120
|
+
return finish(CloseCode::TOO_MANY_CONNECTIONS) if count >= @limits.max_connections_per_identity
|
|
121
|
+
|
|
122
|
+
@identity = identity
|
|
123
|
+
@state = :open
|
|
124
|
+
@sessions.add(self)
|
|
125
|
+
send_frame("welcome", session_id: @id)
|
|
126
|
+
@instrumenter.instrument(:connect, session_id: @id, identity_key: identity.key)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# @api private
|
|
130
|
+
# @since 0.1.0
|
|
131
|
+
def subscribe(frame)
|
|
132
|
+
return finish(CloseCode::DUPLICATE_ID) if @subscriptions.key?(frame.id)
|
|
133
|
+
if @subscriptions.size >= @limits.max_subscriptions
|
|
134
|
+
return finish(CloseCode::TOO_MANY_SUBSCRIPTIONS)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
channel = resolve_channel(frame)
|
|
138
|
+
params = channel && validated_params(channel, frame)
|
|
139
|
+
return unless channel && params
|
|
140
|
+
|
|
141
|
+
subscription = Subscription.new(
|
|
142
|
+
id: frame.id, channel: frame.channel, params: params, identity: @identity, session_id: @id
|
|
143
|
+
)
|
|
144
|
+
accepted = channel.subscribe(subscription)
|
|
145
|
+
return reject(frame.id, "unauthorized") if accepted.failure?
|
|
146
|
+
|
|
147
|
+
register_subscription(subscription, accepted.value!)
|
|
148
|
+
rescue StandardError => exception
|
|
149
|
+
report(exception)
|
|
150
|
+
reject(frame.id, "unauthorized")
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# @api private
|
|
154
|
+
# @since 0.1.0
|
|
155
|
+
def resolve_channel(frame)
|
|
156
|
+
result = @channels.fetch(frame.channel)
|
|
157
|
+
return result.value! if result.success?
|
|
158
|
+
|
|
159
|
+
reject(frame.id, "invalid")
|
|
160
|
+
nil
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# @api private
|
|
164
|
+
# @since 0.1.0
|
|
165
|
+
def validated_params(channel, frame)
|
|
166
|
+
result = channel.validate_params(frame.params)
|
|
167
|
+
return result.value! if result.success?
|
|
168
|
+
|
|
169
|
+
reject(frame.id, "invalid")
|
|
170
|
+
nil
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# @api private
|
|
174
|
+
# @since 0.1.0
|
|
175
|
+
def register_subscription(subscription, result)
|
|
176
|
+
streams = result[:streams]
|
|
177
|
+
unless streams.is_a?(Array) && streams.all? { |stream| stream.is_a?(Stream) }
|
|
178
|
+
return reject(subscription.id, "invalid")
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
data = result[:data]
|
|
182
|
+
if data && JSON.generate(data).bytesize > @limits.max_payload_size
|
|
183
|
+
report(ArgumentError.new("subscribed data is too large"))
|
|
184
|
+
return reject(subscription.id, "internal")
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
@subscriptions.add(subscription.with(streams: streams))
|
|
188
|
+
@hub.add(self, streams)
|
|
189
|
+
fields = {id: subscription.id}
|
|
190
|
+
fields[:data] = data if data
|
|
191
|
+
send_frame("subscribed", **fields)
|
|
192
|
+
@instrumenter.instrument(:subscribe, session_id: @id, channel: subscription.channel,
|
|
193
|
+
subscription_id: subscription.id)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# @api private
|
|
197
|
+
# @since 0.1.0
|
|
198
|
+
def unsubscribe(frame)
|
|
199
|
+
subscription = @subscriptions.fetch(frame.id)
|
|
200
|
+
@subscriptions.remove(frame.id)
|
|
201
|
+
@hub.remove(self)
|
|
202
|
+
@subscriptions.each { |remaining| @hub.add(self, remaining.streams) }
|
|
203
|
+
@channels.fetch(subscription.channel).value!.unsubscribe(subscription)
|
|
204
|
+
@instrumenter.instrument(:unsubscribe, session_id: @id, subscription_id: frame.id)
|
|
205
|
+
rescue KeyError
|
|
206
|
+
nil
|
|
207
|
+
rescue StandardError => exception
|
|
208
|
+
report(exception)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# @api private
|
|
212
|
+
# @since 0.1.0
|
|
213
|
+
def action(frame)
|
|
214
|
+
return reply_error(frame, "invalid") unless @subscriptions.key?(frame.id)
|
|
215
|
+
|
|
216
|
+
subscription = @subscriptions.fetch(frame.id)
|
|
217
|
+
channel = @channels.fetch(subscription.channel)
|
|
218
|
+
return reply_error(frame, "invalid") if channel.failure?
|
|
219
|
+
|
|
220
|
+
result = channel.value!.receive(subscription, Message.new(name: frame.name, data: frame.data, ref: frame.ref))
|
|
221
|
+
reply_to_action(subscription, frame, result)
|
|
222
|
+
rescue StandardError => exception
|
|
223
|
+
report(exception)
|
|
224
|
+
instrument_action(subscription, frame, :error) if subscription
|
|
225
|
+
reply_error(frame, "internal")
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# @api private
|
|
229
|
+
# @since 0.1.0
|
|
230
|
+
def reply_to_action(subscription, frame, result)
|
|
231
|
+
if result.success?
|
|
232
|
+
instrument_action(subscription, frame, :success)
|
|
233
|
+
reply_ack(frame, result.value!)
|
|
234
|
+
else
|
|
235
|
+
instrument_action(subscription, frame, :failure)
|
|
236
|
+
reply_error(frame, "unauthorized")
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# @api private
|
|
241
|
+
# @since 0.1.0
|
|
242
|
+
def instrument_action(subscription, frame, outcome)
|
|
243
|
+
@instrumenter.instrument(:action, session_id: @id, channel: subscription.channel,
|
|
244
|
+
subscription_id: subscription.id, action: frame.name, outcome: outcome)
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# @api private
|
|
248
|
+
# @since 0.1.0
|
|
249
|
+
def deliver(broadcast)
|
|
250
|
+
@subscriptions.each do |subscription|
|
|
251
|
+
next unless subscription.streams.any? { |stream| stream.name == broadcast.stream }
|
|
252
|
+
|
|
253
|
+
@socket.send_text(Protocol.encode_message(
|
|
254
|
+
id: subscription.id, stream_id: broadcast.stream, payload: broadcast.payload
|
|
255
|
+
))
|
|
256
|
+
end
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
# @api private
|
|
260
|
+
# @since 0.1.0
|
|
261
|
+
def tick(now)
|
|
262
|
+
if @state == :awaiting_hello && now - @created_at >= @limits.hello_timeout
|
|
263
|
+
return finish(CloseCode::HELLO_TIMEOUT)
|
|
264
|
+
end
|
|
265
|
+
return finish(CloseCode::IDLE) if now - @last_seen_at >= 2 * @limits.heartbeat_interval
|
|
266
|
+
return if now - @last_ping_at < @limits.heartbeat_interval
|
|
267
|
+
|
|
268
|
+
@last_ping_at = now
|
|
269
|
+
send_frame("ping")
|
|
270
|
+
@socket.ping
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# @api private
|
|
274
|
+
# @since 0.1.0
|
|
275
|
+
def finish(close_code)
|
|
276
|
+
@state = :closed
|
|
277
|
+
@subscriptions.each { |subscription| unsubscribe_channel(subscription) }
|
|
278
|
+
@hub.remove(self)
|
|
279
|
+
@sessions.remove(self)
|
|
280
|
+
@socket.close(close_code) unless @socket.closed?
|
|
281
|
+
@instrumenter.instrument(:disconnect, session_id: @id, identity_key: @identity&.key,
|
|
282
|
+
code: close_code.code)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# @api private
|
|
286
|
+
# @since 0.1.0
|
|
287
|
+
def unsubscribe_channel(subscription)
|
|
288
|
+
@channels.fetch(subscription.channel).value!.unsubscribe(subscription)
|
|
289
|
+
rescue StandardError => exception
|
|
290
|
+
report(exception)
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
# @api private
|
|
294
|
+
# @since 0.1.0
|
|
295
|
+
def send_frame(type, **fields)
|
|
296
|
+
@socket.send_text(Protocol.encode(type, **fields))
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# @api private
|
|
300
|
+
# @since 0.1.0
|
|
301
|
+
def reject(id, reason)
|
|
302
|
+
send_frame("rejected", id: id, reason: reason)
|
|
303
|
+
@instrumenter.instrument(:reject, session_id: @id, subscription_id: id, reason: reason)
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
# @api private
|
|
307
|
+
# @since 0.1.0
|
|
308
|
+
def reply_ack(frame, value)
|
|
309
|
+
return unless frame.ref
|
|
310
|
+
|
|
311
|
+
fields = {ref: frame.ref}
|
|
312
|
+
fields[:data] = value[:data] if value.is_a?(Hash) && value[:data]
|
|
313
|
+
send_frame("ack", **fields)
|
|
314
|
+
end
|
|
315
|
+
|
|
316
|
+
# @api private
|
|
317
|
+
# @since 0.1.0
|
|
318
|
+
def reply_error(frame, reason)
|
|
319
|
+
return unless frame.ref
|
|
320
|
+
|
|
321
|
+
send_frame("error", ref: frame.ref, reason: reason)
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# @api private
|
|
325
|
+
# @since 0.1.0
|
|
326
|
+
def report(exception)
|
|
327
|
+
@instrumenter.instrument(:error, session_id: @id, exception: exception)
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
module Sockets
|
|
5
|
+
# The shared collaborators every session needs. Passed to Server as one argument so its
|
|
6
|
+
# constructor does not grow with each new dependency.
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
9
|
+
# @since 0.1.0
|
|
10
|
+
SessionDependencies = Data.define(:authentication, :channels) do
|
|
11
|
+
# @api private
|
|
12
|
+
# @since 0.1.0
|
|
13
|
+
def initialize(authentication:, channels:)
|
|
14
|
+
super
|
|
15
|
+
freeze
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rack"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
|
|
6
|
+
module Mankai
|
|
7
|
+
module Sockets
|
|
8
|
+
# Builds the transport, mailbox and protocol state machine for one accepted connection.
|
|
9
|
+
#
|
|
10
|
+
# @api private
|
|
11
|
+
# @since 0.1.0
|
|
12
|
+
class SessionFactory
|
|
13
|
+
# @api private
|
|
14
|
+
# @since 0.1.0
|
|
15
|
+
def initialize(executor:, reactor:, authentication:, channels:, hub:, sessions:, limits:,
|
|
16
|
+
instrumenter:, clock:)
|
|
17
|
+
@executor = executor
|
|
18
|
+
@reactor = reactor
|
|
19
|
+
@authentication = authentication
|
|
20
|
+
@channels = channels
|
|
21
|
+
@hub = hub
|
|
22
|
+
@sessions = sessions
|
|
23
|
+
@limits = limits
|
|
24
|
+
@instrumenter = instrumenter
|
|
25
|
+
@clock = clock
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @api private
|
|
29
|
+
# @since 0.1.0
|
|
30
|
+
def call(io, env)
|
|
31
|
+
socket = Transport::Socket.new(io: io, env: env, reactor: @reactor, limits: @limits)
|
|
32
|
+
session = Session.new(
|
|
33
|
+
id: SecureRandom.uuid,
|
|
34
|
+
socket: socket,
|
|
35
|
+
mailbox: Mailbox.new(executor: @executor),
|
|
36
|
+
request: Rack::Request.new(env),
|
|
37
|
+
authentication: @authentication,
|
|
38
|
+
channels: @channels,
|
|
39
|
+
hub: @hub,
|
|
40
|
+
sessions: @sessions,
|
|
41
|
+
limits: @limits,
|
|
42
|
+
instrumenter: @instrumenter,
|
|
43
|
+
clock: @clock
|
|
44
|
+
)
|
|
45
|
+
socket.start(session)
|
|
46
|
+
session
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
module Sockets
|
|
5
|
+
# The sessions in this process, indexed by id and searchable by identity key. One mutex
|
|
6
|
+
# guards the whole table.
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
9
|
+
# @since 0.1.0
|
|
10
|
+
class Sessions
|
|
11
|
+
# @api private
|
|
12
|
+
# @since 0.1.0
|
|
13
|
+
def initialize
|
|
14
|
+
@items = {}
|
|
15
|
+
@mutex = Mutex.new
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# @api private
|
|
19
|
+
# @since 0.1.0
|
|
20
|
+
def add(session)
|
|
21
|
+
@mutex.synchronize { @items[session.id] = session }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @api private
|
|
25
|
+
# @since 0.1.0
|
|
26
|
+
def remove(session)
|
|
27
|
+
@mutex.synchronize { @items.delete(session.id) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# @api private
|
|
31
|
+
# @since 0.1.0
|
|
32
|
+
def each(&block)
|
|
33
|
+
@mutex.synchronize { @items.values.dup }.each(&block)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @api private
|
|
37
|
+
# @since 0.1.0
|
|
38
|
+
def for_identity(key)
|
|
39
|
+
@mutex.synchronize { @items.values.select { |session| session.identity_key == key } }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @api private
|
|
43
|
+
# @since 0.1.0
|
|
44
|
+
def count_for_identity(key)
|
|
45
|
+
for_identity(key).size
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
module Sockets
|
|
5
|
+
# A colon-separated broadcast name, e.g. "chat:42". Built from parts so callers cannot put
|
|
6
|
+
# a colon in one by accident. Names starting with "mankai." are reserved for the gem.
|
|
7
|
+
#
|
|
8
|
+
# @api public
|
|
9
|
+
# @since 0.1.0
|
|
10
|
+
Stream = Data.define(:name)
|
|
11
|
+
|
|
12
|
+
# @api private
|
|
13
|
+
# @since 0.1.0
|
|
14
|
+
class Stream
|
|
15
|
+
# @api private
|
|
16
|
+
# @since 0.1.0
|
|
17
|
+
MAX_BYTES = 255
|
|
18
|
+
# @api private
|
|
19
|
+
# @since 0.1.0
|
|
20
|
+
RESERVED_PREFIX = "mankai."
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
# Builds a stream from parts, joined with ":".
|
|
24
|
+
#
|
|
25
|
+
# @param parts [Array<String, Symbol, Integer>]
|
|
26
|
+
# @return [Stream]
|
|
27
|
+
#
|
|
28
|
+
# @api public
|
|
29
|
+
# @since 0.1.0
|
|
30
|
+
def of(*parts)
|
|
31
|
+
raise ArgumentError, "a stream needs at least one part" if parts.empty?
|
|
32
|
+
|
|
33
|
+
parts.each { |part| validate_part(part) }
|
|
34
|
+
new(name: parts.map(&:to_s).join(":"))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
# @api private
|
|
40
|
+
# @since 0.1.0
|
|
41
|
+
def validate_part(part)
|
|
42
|
+
unless part.is_a?(String) || part.is_a?(Symbol) || part.is_a?(Integer)
|
|
43
|
+
raise ArgumentError, "stream parts must be Strings, Symbols or Integers"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
text = part.to_s
|
|
47
|
+
raise ArgumentError, "stream parts must be non-empty" if text.empty?
|
|
48
|
+
|
|
49
|
+
return unless text.include?(":")
|
|
50
|
+
|
|
51
|
+
raise ArgumentError, "stream parts cannot contain ':'"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @api public
|
|
56
|
+
# @since 0.1.0
|
|
57
|
+
def initialize(name:)
|
|
58
|
+
raise ArgumentError, "stream name must be a non-empty String" unless name.is_a?(String) && !name.empty?
|
|
59
|
+
raise ArgumentError, "stream name is longer than #{MAX_BYTES} bytes" if name.bytesize > MAX_BYTES
|
|
60
|
+
|
|
61
|
+
if name.start_with?(RESERVED_PREFIX)
|
|
62
|
+
raise ArgumentError, "stream names starting with #{RESERVED_PREFIX.inspect} are reserved"
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
super
|
|
66
|
+
freeze
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
module Sockets
|
|
5
|
+
# One client subscription. The streams are empty until the channel's `subscribe` succeeds,
|
|
6
|
+
# and replace the old collection with `with(streams:)`.
|
|
7
|
+
#
|
|
8
|
+
# @api public
|
|
9
|
+
# @since 0.1.0
|
|
10
|
+
Subscription = Data.define(:id, :channel, :params, :identity, :streams, :session_id)
|
|
11
|
+
|
|
12
|
+
# @api public
|
|
13
|
+
# @since 0.1.0
|
|
14
|
+
class Subscription
|
|
15
|
+
# @api public
|
|
16
|
+
# @since 0.1.0
|
|
17
|
+
def initialize(id:, channel:, params: {}, identity: nil, streams: [], session_id: nil)
|
|
18
|
+
check_id!(id)
|
|
19
|
+
check_channel!(channel)
|
|
20
|
+
raise ArgumentError, "subscription params must be a Hash" unless params.is_a?(Hash)
|
|
21
|
+
unless streams.is_a?(Array) && streams.all? { |stream| stream.is_a?(Stream) }
|
|
22
|
+
raise ArgumentError, "subscription streams must be an Array of Streams"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
super(
|
|
26
|
+
id: id,
|
|
27
|
+
channel: channel,
|
|
28
|
+
params: params.freeze,
|
|
29
|
+
identity: identity,
|
|
30
|
+
streams: streams.freeze,
|
|
31
|
+
session_id: session_id
|
|
32
|
+
)
|
|
33
|
+
freeze
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
# @api private
|
|
39
|
+
# @since 0.1.0
|
|
40
|
+
def check_id!(id)
|
|
41
|
+
return if id.is_a?(String) && !id.empty?
|
|
42
|
+
|
|
43
|
+
raise ArgumentError, "subscription id must be a non-empty String"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @api private
|
|
47
|
+
# @since 0.1.0
|
|
48
|
+
def check_channel!(channel)
|
|
49
|
+
return if channel.is_a?(String) && !channel.empty?
|
|
50
|
+
|
|
51
|
+
raise ArgumentError, "subscription channel must be a non-empty String"
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Mankai
|
|
4
|
+
module Sockets
|
|
5
|
+
# The subscriptions of one session. Only the session's mailbox touches it, so it needs no
|
|
6
|
+
# lock of its own.
|
|
7
|
+
#
|
|
8
|
+
# @api private
|
|
9
|
+
# @since 0.1.0
|
|
10
|
+
class Subscriptions
|
|
11
|
+
# @api private
|
|
12
|
+
# @since 0.1.0
|
|
13
|
+
def initialize
|
|
14
|
+
@items = {}
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @api private
|
|
18
|
+
# @since 0.1.0
|
|
19
|
+
def add(subscription)
|
|
20
|
+
@items[subscription.id] = subscription
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @api private
|
|
24
|
+
# @since 0.1.0
|
|
25
|
+
def remove(id)
|
|
26
|
+
@items.delete(id)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# @api private
|
|
30
|
+
# @since 0.1.0
|
|
31
|
+
def fetch(id)
|
|
32
|
+
@items.fetch(id)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @api private
|
|
36
|
+
# @since 0.1.0
|
|
37
|
+
def each(&block)
|
|
38
|
+
@items.each_value(&block)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# @api private
|
|
42
|
+
# @since 0.1.0
|
|
43
|
+
def size
|
|
44
|
+
@items.size
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @api private
|
|
48
|
+
# @since 0.1.0
|
|
49
|
+
def key?(id)
|
|
50
|
+
@items.key?(id)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Shared examples for the Backend contract:
|
|
4
|
+
#
|
|
5
|
+
# require "mankai/sockets/testing/backend_examples"
|
|
6
|
+
#
|
|
7
|
+
# RSpec.describe MyBackend do
|
|
8
|
+
# subject(:backend) { MyBackend.new }
|
|
9
|
+
#
|
|
10
|
+
# it_behaves_like "a mankai-sockets backend"
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# @api public
|
|
14
|
+
# @since 0.1.0
|
|
15
|
+
RSpec.shared_examples "a mankai-sockets backend" do
|
|
16
|
+
define_method(:wait_until) do |timeout: 2, &condition|
|
|
17
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
18
|
+
until condition.call
|
|
19
|
+
raise "condition not met within #{timeout}s" if Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
|
|
20
|
+
|
|
21
|
+
sleep 0.01
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
before { backend.start }
|
|
26
|
+
after { backend.stop }
|
|
27
|
+
|
|
28
|
+
it "delivers broadcasts with increasing ids" do
|
|
29
|
+
received = []
|
|
30
|
+
backend.subscribe("chat:1", ->(message) { received << message })
|
|
31
|
+
ids = [
|
|
32
|
+
backend.publish("chat:1", JSON.generate(body: "one")),
|
|
33
|
+
backend.publish("chat:1", JSON.generate(body: "two"))
|
|
34
|
+
]
|
|
35
|
+
expect(ids).to eq(ids.uniq.sort)
|
|
36
|
+
wait_until { received.size == 2 }
|
|
37
|
+
expect(received.map(&:stream)).to eq(["chat:1", "chat:1"])
|
|
38
|
+
expect(received.map(&:payload)).to eq([JSON.generate(body: "one"), JSON.generate(body: "two")])
|
|
39
|
+
expect(received.map(&:id)).to eq(received.map(&:id).uniq.sort)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "stops delivering after unsubscribe" do
|
|
43
|
+
received = []
|
|
44
|
+
listener = ->(message) { received << message }
|
|
45
|
+
backend.subscribe("chat:1", listener)
|
|
46
|
+
backend.unsubscribe("chat:1", listener)
|
|
47
|
+
backend.publish("chat:1", JSON.generate(body: "gone"))
|
|
48
|
+
sleep 0.2
|
|
49
|
+
expect(received).to be_empty
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "keeps streams separate and delivers to several listeners" do
|
|
53
|
+
first = []
|
|
54
|
+
second = []
|
|
55
|
+
backend.subscribe("chat:1", ->(message) { first << message })
|
|
56
|
+
backend.subscribe("chat:2", ->(message) { second << message })
|
|
57
|
+
backend.subscribe("chat:1", ->(message) { first << message })
|
|
58
|
+
backend.publish("chat:1", JSON.generate(body: "a"))
|
|
59
|
+
backend.publish("chat:2", JSON.generate(body: "b"))
|
|
60
|
+
wait_until { first.size == 2 && second.size == 1 }
|
|
61
|
+
expect(first.map(&:payload)).to all(include("a"))
|
|
62
|
+
expect(second.map(&:payload)).to eq([JSON.generate(body: "b")])
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "starts and stops idempotently" do
|
|
66
|
+
expect { backend.start }.not_to raise_error
|
|
67
|
+
expect { backend.stop }.not_to raise_error
|
|
68
|
+
end
|
|
69
|
+
end
|