daytona 0.233.0 → 0.234.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/lib/daytona/common/event_dispatcher.rb +123 -8
- data/lib/daytona/common/event_subscription_manager.rb +24 -0
- data/lib/daytona/common/socketio_client.rb +112 -23
- data/lib/daytona/daytona.rb +2 -1
- data/lib/daytona/sandbox.rb +443 -158
- data/lib/daytona/sandbox_nodes.rb +6 -1
- data/lib/daytona/sdk/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a42c7fa412e61eb200c67ce741d6a870db0d7f9a6e1c5934d05ffba8c028668e
|
|
4
|
+
data.tar.gz: f3d4b22563636e965d7e545f18dd5173dbe48ced693baa686675d684f06d4f06
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4c105dd54dbd06f1d0236176cd9164106105ada331b53b94701cb50221fbcf5b837b03b9ac056c2d27ef3cfd8e945cf04c3d49c1f3251105ae2a78fb9f8364b9
|
|
7
|
+
data.tar.gz: 12a7e8bf038e97e6b75682bd59a60b57fdff281c9edb70f1f8c31e34cb52ded63b39ca5193343daaa0848827aa7206072f1dafb5a08efc2ee27d53c68b0c0518
|
|
@@ -8,9 +8,21 @@ require_relative 'socketio_client'
|
|
|
8
8
|
module Daytona
|
|
9
9
|
# Manages a Socket.IO connection and dispatches events to per-resource handlers.
|
|
10
10
|
# Generic — works for sandboxes, volumes, snapshots, runners, etc.
|
|
11
|
+
#
|
|
12
|
+
# The connection opens lazily with the first subscription and closes DISCONNECT_DELAY seconds
|
|
13
|
+
# after the last one goes away. The server is told which resource ids have listeners
|
|
14
|
+
# (subscribe / unsubscribe events), so it only sends events for those. Older servers ignore
|
|
15
|
+
# this and send every organization event; incoming events are always filtered by id.
|
|
11
16
|
class EventDispatcher
|
|
12
17
|
DISCONNECT_DELAY = 30
|
|
13
18
|
|
|
19
|
+
# Most resource ids per subscribe / unsubscribe message.
|
|
20
|
+
SUBSCRIBE_BATCH_SIZE = 100
|
|
21
|
+
|
|
22
|
+
# Local event, not from the server, passed to the handlers of an id whose subscribe the server rejected:
|
|
23
|
+
# no events will come for it.
|
|
24
|
+
SUBSCRIPTION_REJECTED_EVENT = 'daytona.subscription.rejected'
|
|
25
|
+
|
|
14
26
|
# @param api_url [String]
|
|
15
27
|
# @param token [String]
|
|
16
28
|
# @param organization_id [String, nil]
|
|
@@ -22,9 +34,18 @@ module Daytona
|
|
|
22
34
|
@sdk_version = sdk_version
|
|
23
35
|
@client = nil
|
|
24
36
|
@connected = false
|
|
37
|
+
# Successful connects so far, reconnects included. Events emitted while disconnected are lost, so a
|
|
38
|
+
# changed epoch means a gap. Guarded by @mutex.
|
|
39
|
+
@connection_epoch = 0
|
|
25
40
|
@failed = false
|
|
26
41
|
@fail_error = nil
|
|
27
42
|
@listeners = {}
|
|
43
|
+
# The ids the server serves, or will after the next connect: every id with listeners, except one whose
|
|
44
|
+
# subscribe the server rejected (including past its per-socket limit), which stays unserved until its
|
|
45
|
+
# last listener goes. Maps each id to the number it was served under, so a late rejection of an earlier
|
|
46
|
+
# subscribe of the same id cannot unserve a newer one.
|
|
47
|
+
@served = {}
|
|
48
|
+
@serve_token = 0
|
|
28
49
|
@registered_events = Set.new
|
|
29
50
|
@mutex = Mutex.new
|
|
30
51
|
@disconnect_timer = nil
|
|
@@ -90,8 +111,11 @@ module Daytona
|
|
|
90
111
|
|
|
91
112
|
@mutex.synchronize do
|
|
92
113
|
@connected = true
|
|
114
|
+
@connection_epoch += 1
|
|
93
115
|
@failed = false
|
|
94
116
|
@fail_error = nil
|
|
117
|
+
# Subscriptions belong to the socket: send the whole set after every (re)connect.
|
|
118
|
+
emit_subscribe_locked(@served.keys)
|
|
95
119
|
schedule_delayed_disconnect_locked if @listeners.empty?
|
|
96
120
|
end
|
|
97
121
|
rescue StandardError => e
|
|
@@ -117,24 +141,24 @@ module Daytona
|
|
|
117
141
|
@disconnect_timer = nil
|
|
118
142
|
@disconnect_generation += 1
|
|
119
143
|
|
|
144
|
+
first_listener = !@listeners.key?(resource_id)
|
|
120
145
|
@listeners[resource_id] ||= []
|
|
121
146
|
@listeners[resource_id] << handler
|
|
122
147
|
# Mark events in filter under lock to avoid dropping first event
|
|
123
148
|
events.each { |evt| @registered_events.add(evt) }
|
|
149
|
+
serve_locked(resource_id) if first_listener
|
|
124
150
|
end
|
|
125
151
|
|
|
126
152
|
ensure_connected
|
|
127
153
|
|
|
154
|
+
removed = false
|
|
128
155
|
lambda {
|
|
129
|
-
return if @close_requested || @closed
|
|
130
|
-
|
|
131
|
-
should_schedule = false
|
|
132
156
|
@mutex.synchronize do
|
|
133
|
-
@
|
|
134
|
-
unsubscribe_resource_locked(resource_id) if @listeners[resource_id] && @listeners[resource_id].empty?
|
|
135
|
-
should_schedule = @listeners.empty?
|
|
157
|
+
next if removed || @closed
|
|
136
158
|
|
|
137
|
-
|
|
159
|
+
removed = true
|
|
160
|
+
remove_listener_locked(resource_id, handler)
|
|
161
|
+
schedule_delayed_disconnect_locked if @listeners.empty?
|
|
138
162
|
end
|
|
139
163
|
}
|
|
140
164
|
end
|
|
@@ -144,6 +168,25 @@ module Daytona
|
|
|
144
168
|
@connected
|
|
145
169
|
end
|
|
146
170
|
|
|
171
|
+
# @param resource_id [String]
|
|
172
|
+
# @return [Boolean] Whether the server is asked to send events for +resource_id+: it has listeners and
|
|
173
|
+
# the server has not rejected its subscribe
|
|
174
|
+
def served?(resource_id)
|
|
175
|
+
@mutex.synchronize { @served.key?(resource_id) }
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# @param resource_id [String]
|
|
179
|
+
# @return [Boolean] Whether events for +resource_id+ can arrive now: the connection is up and the id is
|
|
180
|
+
# served, read together under one lock so a disconnect cannot fall between the two reads
|
|
181
|
+
def event_backed?(resource_id)
|
|
182
|
+
@mutex.synchronize { @connected && @served.key?(resource_id) }
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# @return [Integer] The number of successful connects so far
|
|
186
|
+
def connection_epoch
|
|
187
|
+
@mutex.synchronize { @connection_epoch }
|
|
188
|
+
end
|
|
189
|
+
|
|
147
190
|
# @return [Boolean]
|
|
148
191
|
def failed?
|
|
149
192
|
@failed
|
|
@@ -163,8 +206,79 @@ module Daytona
|
|
|
163
206
|
|
|
164
207
|
private
|
|
165
208
|
|
|
166
|
-
|
|
209
|
+
# Removes this exact handler once (handlers can compare equal, e.g. two Method objects of the same
|
|
210
|
+
# receiver), and tells the server when the resource loses its last listener.
|
|
211
|
+
def remove_listener_locked(resource_id, handler)
|
|
212
|
+
handlers = @listeners[resource_id]
|
|
213
|
+
return unless handlers
|
|
214
|
+
|
|
215
|
+
index = handlers.index { |h| h.equal?(handler) }
|
|
216
|
+
handlers.delete_at(index) if index
|
|
217
|
+
return unless handlers.empty?
|
|
218
|
+
|
|
167
219
|
@listeners.delete(resource_id)
|
|
220
|
+
emit_ids_locked('unsubscribe', [resource_id]) if @served.delete(resource_id) && @connected
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Serves an id that gained its first listener. While disconnected only the set changes; the next connect
|
|
224
|
+
# sends it. The server enforces its per-socket limit and rejects the excess in the acknowledgement.
|
|
225
|
+
def serve_locked(resource_id)
|
|
226
|
+
@served[resource_id] = (@serve_token += 1)
|
|
227
|
+
emit_subscribe_locked([resource_id]) if @connected
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Sends subscribe messages for +ids+, each with an acknowledgement callback that handles a rejection.
|
|
231
|
+
# Silence is not a rejection: servers that predate explicit subscriptions never acknowledge.
|
|
232
|
+
def emit_subscribe_locked(ids)
|
|
233
|
+
client = @client
|
|
234
|
+
return if ids.empty? || client.nil?
|
|
235
|
+
|
|
236
|
+
ids.each_slice(SUBSCRIBE_BATCH_SIZE) do |batch|
|
|
237
|
+
tokens = batch.to_h { |id| [id, @served[id]] }
|
|
238
|
+
client.emit_with_ack('subscribe', { sandboxIds: batch }) { |ack| handle_subscribe_ack(client, tokens, ack) }
|
|
239
|
+
end
|
|
240
|
+
rescue StandardError
|
|
241
|
+
nil
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Unserves the ids of a rejected subscribe message (any error code) that still have listeners and are
|
|
245
|
+
# served by that subscribe. They stay unserved until their last listener goes, and their handlers are
|
|
246
|
+
# told, so that waits read now and poll.
|
|
247
|
+
#
|
|
248
|
+
# @param client [SocketIOClient] The connection the subscribe went out on
|
|
249
|
+
# @param tokens [Hash{String => Integer}] The message's ids, with the number each was served under
|
|
250
|
+
# @param ack [Object] The acknowledgement: { 'ok' => true } or { 'ok' => false, 'error' => ... }
|
|
251
|
+
def handle_subscribe_ack(client, tokens, ack)
|
|
252
|
+
return unless ack.is_a?(Hash) && ack['ok'] == false
|
|
253
|
+
|
|
254
|
+
handlers = @mutex.synchronize { @client.equal?(client) ? unserve_rejected_locked(tokens) : [] }
|
|
255
|
+
handlers.each do |handler|
|
|
256
|
+
handler.call(SUBSCRIPTION_REJECTED_EVENT, nil)
|
|
257
|
+
rescue StandardError
|
|
258
|
+
nil
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# @param tokens [Hash{String => Integer}] Rejected ids, with the number each was served under
|
|
263
|
+
# @return [Array<Proc>] The handlers of the ids unserved: those still served by that subscribe
|
|
264
|
+
def unserve_rejected_locked(tokens)
|
|
265
|
+
tokens.flat_map do |id, token|
|
|
266
|
+
next [] unless @served[id] == token && @listeners[id]&.any?
|
|
267
|
+
|
|
268
|
+
@served.delete(id)
|
|
269
|
+
@listeners[id].dup
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Fire-and-forget; sent under the lock so subscribe / unsubscribe messages keep their order.
|
|
274
|
+
def emit_ids_locked(event_name, ids)
|
|
275
|
+
return if ids.empty? || @client.nil?
|
|
276
|
+
|
|
277
|
+
ids.each_slice(SUBSCRIBE_BATCH_SIZE) do |batch|
|
|
278
|
+
@client.emit(event_name, { sandboxIds: batch })
|
|
279
|
+
end
|
|
280
|
+
rescue StandardError
|
|
281
|
+
nil
|
|
168
282
|
end
|
|
169
283
|
|
|
170
284
|
def schedule_delayed_disconnect_locked
|
|
@@ -293,6 +407,7 @@ module Daytona
|
|
|
293
407
|
@connected = false
|
|
294
408
|
@connecting = false
|
|
295
409
|
@listeners.clear
|
|
410
|
+
@served.clear
|
|
296
411
|
@registered_events.clear
|
|
297
412
|
|
|
298
413
|
thread_state
|
|
@@ -77,6 +77,30 @@ module Daytona
|
|
|
77
77
|
end
|
|
78
78
|
end
|
|
79
79
|
|
|
80
|
+
# @return [Boolean] Whether the dispatcher currently has a live connection, i.e. whether
|
|
81
|
+
# subscribed events can arrive. Always false after #shutdown.
|
|
82
|
+
def connected?
|
|
83
|
+
return false if @dispatcher.nil? || @mutex.synchronize { @closed }
|
|
84
|
+
|
|
85
|
+
@dispatcher.connected?
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# @return [Integer] The dispatcher's count of successful connects; it changes on every reconnect, so
|
|
89
|
+
# callers can tell whether events may have been missed between two points in time. 0 without a
|
|
90
|
+
# dispatcher.
|
|
91
|
+
def connection_epoch
|
|
92
|
+
@dispatcher.nil? ? 0 : @dispatcher.connection_epoch
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# @param resource_id [String]
|
|
96
|
+
# @return [Boolean] Whether state events for +resource_id+ can arrive now: the manager is open, the
|
|
97
|
+
# connection is up, and +resource_id+ is currently subscribed (served), so not rejected by the server
|
|
98
|
+
def event_backed?(resource_id)
|
|
99
|
+
return false if @dispatcher.nil? || @mutex.synchronize { @closed }
|
|
100
|
+
|
|
101
|
+
@dispatcher.event_backed?(resource_id)
|
|
102
|
+
end
|
|
103
|
+
|
|
80
104
|
def unsubscribe(sub_id)
|
|
81
105
|
subscription = @mutex.synchronize do
|
|
82
106
|
removed = @subscriptions.delete(sub_id)
|
|
@@ -12,7 +12,7 @@ require 'uri'
|
|
|
12
12
|
|
|
13
13
|
module Daytona
|
|
14
14
|
# Minimal Engine.IO/Socket.IO v4 client over raw WebSocket.
|
|
15
|
-
# Supports connect with auth, heartbeat, and event
|
|
15
|
+
# Supports connect with auth, heartbeat, event reception and fire-and-forget event emission.
|
|
16
16
|
#
|
|
17
17
|
# Engine.IO v4 heartbeat protocol (WebSocket transport):
|
|
18
18
|
# - Server sends PING (type 2) every pingInterval ms
|
|
@@ -30,6 +30,7 @@ module Daytona
|
|
|
30
30
|
SIO_CONNECT = '0'
|
|
31
31
|
SIO_DISCONNECT = '1'
|
|
32
32
|
SIO_EVENT = '2'
|
|
33
|
+
SIO_ACK = '3'
|
|
33
34
|
SIO_CONNECT_ERROR = '4'
|
|
34
35
|
|
|
35
36
|
attr_reader :connected
|
|
@@ -61,8 +62,16 @@ module Daytona
|
|
|
61
62
|
@last_server_activity = Time.now
|
|
62
63
|
@ws = nil
|
|
63
64
|
@close_requested = false
|
|
65
|
+
# Acknowledgement callbacks of emitted events, by packet id, with the monotonic time they expire. Guarded
|
|
66
|
+
# by @mutex, like @connected: dropped, never invoked, when the connection goes or they expire.
|
|
67
|
+
@pending_acks = {}
|
|
68
|
+
@next_ack_id = 0
|
|
64
69
|
end
|
|
65
70
|
|
|
71
|
+
# How long an acknowledgement callback waits. A server may never acknowledge (older servers do not), so a
|
|
72
|
+
# callback is dropped after it.
|
|
73
|
+
ACK_TIMEOUT = 10
|
|
74
|
+
|
|
66
75
|
# Establish the WebSocket connection and perform Socket.IO handshake.
|
|
67
76
|
# @return [Boolean] true if connection succeeded
|
|
68
77
|
# @raise [StandardError] on connection failure
|
|
@@ -93,21 +102,12 @@ module Daytona
|
|
|
93
102
|
end
|
|
94
103
|
|
|
95
104
|
ws.on :error do |_e|
|
|
96
|
-
was_connected = client.
|
|
97
|
-
prev = client.instance_variable_get(:@connected)
|
|
98
|
-
client.instance_variable_set(:@connected, false)
|
|
99
|
-
prev
|
|
100
|
-
end
|
|
105
|
+
was_connected = client.send(:mark_disconnected)
|
|
101
106
|
connected_queue.push(:error) unless was_connected
|
|
102
107
|
end
|
|
103
108
|
|
|
104
109
|
ws.on :close do
|
|
105
|
-
|
|
106
|
-
was_connected = mutex.synchronize do
|
|
107
|
-
prev = client.instance_variable_get(:@connected)
|
|
108
|
-
client.instance_variable_set(:@connected, false)
|
|
109
|
-
prev
|
|
110
|
-
end
|
|
110
|
+
was_connected = client.send(:mark_disconnected)
|
|
111
111
|
on_disconnect = client.instance_variable_get(:@on_disconnect)
|
|
112
112
|
close_requested = client.instance_variable_get(:@close_requested)
|
|
113
113
|
begin
|
|
@@ -151,6 +151,47 @@ module Daytona
|
|
|
151
151
|
@mutex.synchronize { @connected }
|
|
152
152
|
end
|
|
153
153
|
|
|
154
|
+
# Send a Socket.IO event without requesting an acknowledgement. Nothing is buffered:
|
|
155
|
+
# the event is dropped when the connection is not established. Never raises.
|
|
156
|
+
#
|
|
157
|
+
# @param event_name [String]
|
|
158
|
+
# @param data [Object] JSON-serializable payload
|
|
159
|
+
# @return [Boolean] true if the event was written to the socket
|
|
160
|
+
def emit(event_name, data)
|
|
161
|
+
return false unless connected?
|
|
162
|
+
|
|
163
|
+
send_raw("#{EIO_MESSAGE}#{SIO_EVENT}#{JSON.generate([event_name, data])}")
|
|
164
|
+
rescue StandardError
|
|
165
|
+
false
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# #emit with an acknowledgement callback, called on the reader thread with the server's first ack
|
|
169
|
+
# argument. It is called at most once, and not at all if the connection goes or no ack arrives within
|
|
170
|
+
# ACK_TIMEOUT seconds.
|
|
171
|
+
#
|
|
172
|
+
# @param event_name [String]
|
|
173
|
+
# @param data [Object] JSON-serializable payload
|
|
174
|
+
# @yieldparam ack [Object, nil] The first acknowledgement argument
|
|
175
|
+
# @return [Boolean] true if the event was written to the socket
|
|
176
|
+
def emit_with_ack(event_name, data, &on_ack)
|
|
177
|
+
ack_id = @mutex.synchronize do
|
|
178
|
+
next unless @connected
|
|
179
|
+
|
|
180
|
+
now = monotonic_now
|
|
181
|
+
@pending_acks.delete_if { |_id, (_callback, expires_at)| expires_at <= now }
|
|
182
|
+
@next_ack_id += 1
|
|
183
|
+
@pending_acks[@next_ack_id] = [on_ack, now + ACK_TIMEOUT]
|
|
184
|
+
@next_ack_id
|
|
185
|
+
end
|
|
186
|
+
return false unless ack_id
|
|
187
|
+
|
|
188
|
+
sent = send_raw("#{EIO_MESSAGE}#{SIO_EVENT}#{ack_id}#{JSON.generate([event_name, data])}")
|
|
189
|
+
@mutex.synchronize { @pending_acks.delete(ack_id) } unless sent
|
|
190
|
+
sent
|
|
191
|
+
rescue StandardError
|
|
192
|
+
false
|
|
193
|
+
end
|
|
194
|
+
|
|
154
195
|
# Gracefully close the connection.
|
|
155
196
|
def close
|
|
156
197
|
@close_requested = true
|
|
@@ -159,7 +200,7 @@ module Daytona
|
|
|
159
200
|
|
|
160
201
|
send_raw(EIO_CLOSE) if @ws
|
|
161
202
|
@ws&.close
|
|
162
|
-
|
|
203
|
+
mark_disconnected
|
|
163
204
|
rescue StandardError
|
|
164
205
|
# Ignore errors during close
|
|
165
206
|
end
|
|
@@ -216,6 +257,9 @@ module Daytona
|
|
|
216
257
|
query_parts << "organizationId=#{URI.encode_www_form_component(@organization_id)}" if @organization_id
|
|
217
258
|
query_parts << "source=#{URI.encode_www_form_component(@source)}" if @source
|
|
218
259
|
query_parts << "sdkVersion=#{URI.encode_www_form_component(@sdk_version)}" if @sdk_version
|
|
260
|
+
# Receive events only for explicitly subscribed sandboxes. Servers without support ignore
|
|
261
|
+
# it and send all organization events, which the dispatcher still filters by id.
|
|
262
|
+
query_parts << 'subscriptionMode=explicit'
|
|
219
263
|
|
|
220
264
|
port_str = (parsed.scheme == 'https' && port == 443) || (parsed.scheme == 'http' && port == 80) ? '' : ":#{port}"
|
|
221
265
|
base_path = parsed.path.to_s.chomp('/')
|
|
@@ -263,7 +307,8 @@ module Daytona
|
|
|
263
307
|
handle_socketio_packet(raw[1..], connected_queue)
|
|
264
308
|
|
|
265
309
|
when EIO_CLOSE
|
|
266
|
-
|
|
310
|
+
# Server-initiated close: reconnect (and re-subscribe) as for a Socket.IO disconnect.
|
|
311
|
+
handle_server_disconnect
|
|
267
312
|
end
|
|
268
313
|
end
|
|
269
314
|
|
|
@@ -285,22 +330,61 @@ module Daytona
|
|
|
285
330
|
rescue JSON::ParserError
|
|
286
331
|
data[1..]
|
|
287
332
|
end
|
|
288
|
-
|
|
333
|
+
mark_disconnected
|
|
289
334
|
connected_queue&.push("Auth rejected: #{error_msg}")
|
|
290
335
|
|
|
291
336
|
when SIO_EVENT
|
|
292
337
|
handle_event(data[1..])
|
|
293
338
|
|
|
339
|
+
when SIO_ACK
|
|
340
|
+
handle_ack(data[1..])
|
|
341
|
+
|
|
294
342
|
when SIO_DISCONNECT
|
|
295
|
-
|
|
296
|
-
prev = @connected
|
|
297
|
-
@connected = false
|
|
298
|
-
prev
|
|
299
|
-
end
|
|
300
|
-
@on_disconnect&.call if was_connected && !@close_requested
|
|
343
|
+
handle_server_disconnect
|
|
301
344
|
end
|
|
302
345
|
end
|
|
303
346
|
|
|
347
|
+
# The server ended the connection: report it once, unless the client is closing.
|
|
348
|
+
def handle_server_disconnect
|
|
349
|
+
was_connected = mark_disconnected
|
|
350
|
+
@on_disconnect&.call if was_connected && !@close_requested
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
# Marks the connection down and drops the pending acknowledgement callbacks.
|
|
354
|
+
#
|
|
355
|
+
# @return [Boolean] Whether it was connected
|
|
356
|
+
def mark_disconnected
|
|
357
|
+
@mutex.synchronize do
|
|
358
|
+
was_connected = @connected
|
|
359
|
+
@connected = false
|
|
360
|
+
@pending_acks.clear
|
|
361
|
+
was_connected
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
# Runs the callback of an ACK packet, "<id>[args...]" (optionally after a "/ns," prefix), with its first
|
|
366
|
+
# argument. Unknown, expired or dropped ids are ignored.
|
|
367
|
+
def handle_ack(data)
|
|
368
|
+
match = %r{\A(?:/[^,]*,)?(\d+)(\[.*)\z}m.match(data.to_s)
|
|
369
|
+
return unless match
|
|
370
|
+
|
|
371
|
+
args = JSON.parse(match[2])
|
|
372
|
+
take_pending_ack(match[1].to_i)&.call(args.is_a?(Array) ? args.first : nil)
|
|
373
|
+
rescue StandardError
|
|
374
|
+
# A malformed ack or a failing callback must not break the reader
|
|
375
|
+
nil
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
# @return [Proc, nil] The callback of +ack_id+, removed; nil when unknown or expired
|
|
379
|
+
def take_pending_ack(ack_id)
|
|
380
|
+
@mutex.synchronize do
|
|
381
|
+
callback, expires_at = @pending_acks.delete(ack_id)
|
|
382
|
+
callback if expires_at && expires_at > monotonic_now
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def monotonic_now = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
|
|
387
|
+
|
|
304
388
|
def handle_event(json_str)
|
|
305
389
|
return unless @on_event
|
|
306
390
|
|
|
@@ -337,7 +421,7 @@ module Daytona
|
|
|
337
421
|
|
|
338
422
|
if Time.now - @last_server_activity > @ping_interval + @ping_timeout
|
|
339
423
|
# No server activity within expected window — connection is dead
|
|
340
|
-
|
|
424
|
+
mark_disconnected
|
|
341
425
|
@on_disconnect&.call unless @close_requested
|
|
342
426
|
# Force-close the dead WebSocket to ensure cleanup
|
|
343
427
|
@ws&.close rescue nil # rubocop:disable Style/RescueModifier
|
|
@@ -349,12 +433,17 @@ module Daytona
|
|
|
349
433
|
end
|
|
350
434
|
end
|
|
351
435
|
|
|
436
|
+
# @return [Boolean] false when there is no socket or the write failed
|
|
352
437
|
def send_raw(msg)
|
|
353
438
|
@write_mutex.synchronize do
|
|
354
|
-
@ws
|
|
439
|
+
return false unless @ws
|
|
440
|
+
|
|
441
|
+
@ws.send(msg)
|
|
442
|
+
true
|
|
355
443
|
end
|
|
356
444
|
rescue StandardError
|
|
357
445
|
# Ignore write errors
|
|
446
|
+
false
|
|
358
447
|
end
|
|
359
448
|
end
|
|
360
449
|
end
|
data/lib/daytona/daytona.rb
CHANGED
|
@@ -81,7 +81,8 @@ module Daytona
|
|
|
81
81
|
source: SOURCE_RUBY,
|
|
82
82
|
sdk_version: Sdk::VERSION
|
|
83
83
|
)
|
|
84
|
-
|
|
84
|
+
# No connection yet: the dispatcher connects in the background when a sandbox wait first
|
|
85
|
+
# subscribes to state events.
|
|
85
86
|
end
|
|
86
87
|
|
|
87
88
|
@subscription_manager = EventSubscriptionManager.new(@event_dispatcher)
|