connectrpc-async 0.3.0 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +736 -581
- data/lib/connectrpc/async/application.rb +14 -1
- data/lib/connectrpc/async/completion_join.rb +49 -217
- data/lib/connectrpc/async/connection.rb +180 -6
- data/lib/connectrpc/async/internal.rb +11 -5
- data/lib/connectrpc/async/server_session.rb +1 -1
- data/lib/connectrpc/async/stream_exchange.rb +23 -4
- data/lib/connectrpc/async/threaded_stream.rb +53 -13
- data/lib/connectrpc/async/transport.rb +1 -6
- data/lib/connectrpc/async/upstream_contract.rb +2 -246
- data/lib/connectrpc/async/version.rb +1 -1
- metadata +2 -2
|
@@ -13,9 +13,12 @@ module ConnectRPC
|
|
|
13
13
|
|
|
14
14
|
# One stream's mailbox. Admission, settle and release follow Call; the
|
|
15
15
|
# command queue, reply slot and flags change only under the admission lock.
|
|
16
|
+
# +shared_deadline+ is the deadline bound every wait shares once an
|
|
17
|
+
# operation ended past the deadline without the owner's answer and
|
|
18
|
+
# failed with deadline_exceeded.
|
|
16
19
|
class Stream < Call
|
|
17
20
|
attr_reader :grpc, :commands
|
|
18
|
-
attr_accessor :seq, :reply, :busy, :broken, :exited, :aborted, :finished
|
|
21
|
+
attr_accessor :seq, :reply, :busy, :broken, :exited, :aborted, :finished, :shared_deadline
|
|
19
22
|
|
|
20
23
|
def initialize(request, source, proxy, grpc)
|
|
21
24
|
super(request, 0, source, proxy)
|
|
@@ -25,6 +28,7 @@ module ConnectRPC
|
|
|
25
28
|
@seq = 0
|
|
26
29
|
@reply = nil
|
|
27
30
|
@busy = @broken = @exited = @aborted = @finished = false
|
|
31
|
+
@shared_deadline = nil
|
|
28
32
|
end
|
|
29
33
|
end
|
|
30
34
|
private_constant :Stream
|
|
@@ -325,7 +329,7 @@ module ConnectRPC
|
|
|
325
329
|
begin
|
|
326
330
|
status, reply = await_stream(stream, request, seq: seq, timeout: command.last)
|
|
327
331
|
answered = %i[reply result retired].include?(status)
|
|
328
|
-
|
|
332
|
+
expire_operation(stream, request, status, reply) unless answered
|
|
329
333
|
raise_outcome(settled(stream), request) if status == :result
|
|
330
334
|
if status == :retired
|
|
331
335
|
check_request!(request)
|
|
@@ -350,6 +354,19 @@ module ConnectRPC
|
|
|
350
354
|
end
|
|
351
355
|
end
|
|
352
356
|
|
|
357
|
+
# Raises an unanswered operation's expiry. One read of core's arbiter,
|
|
358
|
+
# on the deadline's own clock and outside the admission lock, picks the
|
|
359
|
+
# error; when that is deadline_exceeded the stream first shares the
|
|
360
|
+
# operation's deadline bound, +bound+. A clock failure while reading
|
|
361
|
+
# the arbiter propagates and shares nothing.
|
|
362
|
+
def expire_operation(stream, request, status, bound)
|
|
363
|
+
error = expiry_error(request, status)
|
|
364
|
+
if bound && error.code == "deadline_exceeded"
|
|
365
|
+
stream.engine.admission.lock.synchronize { share_deadline(stream, bound) }
|
|
366
|
+
end
|
|
367
|
+
raise error, cause: nil
|
|
368
|
+
end
|
|
369
|
+
|
|
353
370
|
# Settled outcomes are raised outside the lock: arbitration can run
|
|
354
371
|
# cancellation listeners that take it.
|
|
355
372
|
def begin_operation(stream, request, command)
|
|
@@ -411,14 +428,16 @@ module ConnectRPC
|
|
|
411
428
|
end
|
|
412
429
|
|
|
413
430
|
# Waits for the reply tagged +seq+ or the terminal outcome and returns
|
|
414
|
-
# [status, reply]
|
|
415
|
-
#
|
|
431
|
+
# [status, reply], or [:deadline or :timeout, deadline bound] on those
|
|
432
|
+
# expiries, the bound nil without a deadline. A reply is taken in the
|
|
433
|
+
# same critical section that selects it, so a concurrent break cannot
|
|
434
|
+
# withdraw it. Expiry is the
|
|
416
435
|
# deadline or the operation timeout plus the cleanup allowance, or the
|
|
417
436
|
# cancellation allowance; nil bounds wait until a signal or interrupt. An
|
|
418
437
|
# operation also ends once the scope took or abandoned the stream.
|
|
419
438
|
def await_stream(stream, request, seq: nil, timeout: nil)
|
|
420
439
|
now = Internal.now
|
|
421
|
-
|
|
440
|
+
sampled = now + request.deadline.remaining + Internal::CLEANUP_SECONDS if request.deadline
|
|
422
441
|
expires = now + timeout + Internal::CLEANUP_SECONDS if timeout
|
|
423
442
|
lock = stream.engine.admission.lock
|
|
424
443
|
lock.synchronize do
|
|
@@ -432,9 +451,11 @@ module ConnectRPC
|
|
|
432
451
|
return [:retired] if seq && stream.caller_done
|
|
433
452
|
|
|
434
453
|
now = Internal.now
|
|
454
|
+
deadline = [stream.shared_deadline, sampled].compact.min
|
|
435
455
|
return [:canceled] if stream.cancel_expires && stream.cancel_expires <= now
|
|
436
|
-
|
|
437
|
-
return [:
|
|
456
|
+
|
|
457
|
+
return [:deadline, deadline] if deadline && deadline <= now
|
|
458
|
+
return [:timeout, deadline] if expires && expires <= now
|
|
438
459
|
|
|
439
460
|
limit = [deadline, expires, stream.cancel_expires].compact.min
|
|
440
461
|
caller_mask(:immediate) do
|
|
@@ -444,14 +465,33 @@ module ConnectRPC
|
|
|
444
465
|
end
|
|
445
466
|
end
|
|
446
467
|
|
|
447
|
-
#
|
|
448
|
-
#
|
|
468
|
+
# Called under the admission lock when an unanswered operation's expiry
|
|
469
|
+
# is deadline_exceeded. Every later or concurrent wait on the stream
|
|
470
|
+
# then ends by the same deadline bound, so a stalled owner costs the
|
|
471
|
+
# stream one allowance past the deadline rather than one per wait.
|
|
472
|
+
def share_deadline(stream, deadline)
|
|
473
|
+
return if stream.shared_deadline
|
|
474
|
+
|
|
475
|
+
stream.shared_deadline = deadline
|
|
476
|
+
stream.signal.broadcast
|
|
477
|
+
end
|
|
478
|
+
|
|
449
479
|
def expired!(request, status)
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
raise Error.new(:deadline_exceeded, "Deadline exceeded"), cause: nil if status == :deadline
|
|
480
|
+
raise expiry_error(request, status), cause: nil
|
|
481
|
+
end
|
|
453
482
|
|
|
454
|
-
|
|
483
|
+
# An accepted cancellation or deadline keeps its own reason; otherwise
|
|
484
|
+
# the expired bound picks the code. Reads core's arbiter once, as
|
|
485
|
+
# check_request! does.
|
|
486
|
+
def expiry_error(request, status)
|
|
487
|
+
cancellation = request.cancellation
|
|
488
|
+
reason = cancellation ? cancellation.reason : (:deadline_exceeded if request.deadline&.expired?)
|
|
489
|
+
reason ||= :canceled if status == :canceled
|
|
490
|
+
reason ||= :deadline_exceeded if status == :deadline
|
|
491
|
+
return Error.new(:canceled, "Call canceled") if reason == :canceled
|
|
492
|
+
return Error.new(:deadline_exceeded, "Deadline exceeded") if reason == :deadline_exceeded
|
|
493
|
+
|
|
494
|
+
Error.new(:unavailable, FAILED)
|
|
455
495
|
end
|
|
456
496
|
|
|
457
497
|
# A terminal outcome observed while opening or during an operation.
|
|
@@ -535,12 +535,7 @@ module ConnectRPC
|
|
|
535
535
|
end
|
|
536
536
|
response = operation[:response]
|
|
537
537
|
if response
|
|
538
|
-
|
|
539
|
-
case stream.state
|
|
540
|
-
when :idle then stream.close!
|
|
541
|
-
when :closed then nil
|
|
542
|
-
else stream.send_reset_stream(::Protocol::HTTP2::Error::CANCEL)
|
|
543
|
-
end
|
|
538
|
+
response.stream.reset_in_cleanup
|
|
544
539
|
response.close
|
|
545
540
|
end
|
|
546
541
|
end
|
|
@@ -14,16 +14,13 @@ module ConnectRPC
|
|
|
14
14
|
# Checked once per process while the package loads, so an upstream
|
|
15
15
|
# change fails require with a named reason instead of hanging later.
|
|
16
16
|
# spec/upstream_contract_spec.rb pins the behavior behind each entry.
|
|
17
|
-
module UpstreamContract
|
|
17
|
+
module UpstreamContract
|
|
18
18
|
ISSUE = "https://github.com/jimeh/connectrpc-ruby/issues/68"
|
|
19
19
|
TESTED = { "async" => "2.46.0", "async-http" => "0.105.0", "protocol-http2" => "0.29.2" }.freeze
|
|
20
20
|
VERSIONS = {
|
|
21
21
|
"async" => "Async::VERSION", "async-http" => "Async::HTTP::VERSION",
|
|
22
22
|
"protocol-http2" => "Protocol::HTTP2::VERSION", "protocol-http1" => "Protocol::HTTP1::VERSION"
|
|
23
23
|
}.freeze
|
|
24
|
-
PROBE_SECONDS = 5
|
|
25
|
-
STEP_SECONDS = 2
|
|
26
|
-
KILL_SECONDS = 0.5
|
|
27
24
|
|
|
28
25
|
H2 = "Async::HTTP::Protocol::HTTP2"
|
|
29
26
|
CONNECTION_HOOKS = [
|
|
@@ -73,10 +70,8 @@ module ConnectRPC
|
|
|
73
70
|
end
|
|
74
71
|
].freeze
|
|
75
72
|
|
|
76
|
-
def self.verify!(package: "connectrpc-async", version: VERSION, hooks: HOOKS, tested: TESTED
|
|
77
|
-
finalization: true)
|
|
73
|
+
def self.verify!(package: "connectrpc-async", version: VERSION, hooks: HOOKS, tested: TESTED)
|
|
78
74
|
failures = hook_failures(hooks)
|
|
79
|
-
failures.concat(finalization_failures) if finalization
|
|
80
75
|
return if failures.empty?
|
|
81
76
|
|
|
82
77
|
raise UpstreamContractError, report(package, version, failures, tested)
|
|
@@ -139,245 +134,6 @@ module ConnectRPC
|
|
|
139
134
|
end
|
|
140
135
|
end
|
|
141
136
|
private_constant :Hook
|
|
142
|
-
|
|
143
|
-
# The probe runs in its own thread and reactor, so it never touches
|
|
144
|
-
# the caller's scheduler or task tree. It returns within the budget,
|
|
145
|
-
# but a probe thread that Async cannot stop stays asleep.
|
|
146
|
-
def self.finalization_failures(seconds = PROBE_SECONDS)
|
|
147
|
-
probe = FinalizationProbe.new
|
|
148
|
-
thread = Thread.new do
|
|
149
|
-
Thread.current.report_on_exception = false
|
|
150
|
-
probe.call
|
|
151
|
-
rescue Exception => e # rubocop:disable Lint/RescueException -- report any probe crash as a broken contract
|
|
152
|
-
[*probe.failures,
|
|
153
|
-
failure("async", "Task finalization probe raised #{e.class} while #{probe.crashed_step || probe.step}: " \
|
|
154
|
-
"#{e.message}")]
|
|
155
|
-
end
|
|
156
|
-
grace = [KILL_SECONDS, seconds / 10.0].min
|
|
157
|
-
return thread.value if thread.join(seconds - grace)
|
|
158
|
-
|
|
159
|
-
step = probe.step
|
|
160
|
-
failures = probe.failures
|
|
161
|
-
thread.kill
|
|
162
|
-
begin
|
|
163
|
-
thread.join(grace)
|
|
164
|
-
rescue Exception # rubocop:disable Lint/RescueException -- the timeout below is the reported failure
|
|
165
|
-
nil
|
|
166
|
-
end
|
|
167
|
-
leaked = ", and Async could not stop the probe thread" if thread.alive?
|
|
168
|
-
[*failures,
|
|
169
|
-
failure("async", "Task finalization probe did not finish within #{seconds} s while #{step}#{leaked}")]
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
# Matches CompletionJoin's per-instance hook.
|
|
173
|
-
module ConsumeSpy
|
|
174
|
-
def consume
|
|
175
|
-
result = super
|
|
176
|
-
@connectrpc_contract_observer&.call(self)
|
|
177
|
-
result
|
|
178
|
-
end
|
|
179
|
-
end
|
|
180
|
-
private_constant :ConsumeSpy
|
|
181
|
-
|
|
182
|
-
# A duck-typed scheduler entry, like CompletionJoin's Check.
|
|
183
|
-
class Pushed
|
|
184
|
-
attr_reader :inline, :fiber_alive
|
|
185
|
-
|
|
186
|
-
def initialize(fiber, done)
|
|
187
|
-
@fiber = fiber
|
|
188
|
-
@done = done
|
|
189
|
-
@consuming = true
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
def consumed = @consuming = false
|
|
193
|
-
def alive? = !@done.resolved?
|
|
194
|
-
|
|
195
|
-
def transfer
|
|
196
|
-
@inline = @consuming
|
|
197
|
-
@fiber_alive = @fiber.alive?
|
|
198
|
-
@done.resolve(true)
|
|
199
|
-
end
|
|
200
|
-
end
|
|
201
|
-
private_constant :Pushed
|
|
202
|
-
|
|
203
|
-
class ProbeError < StandardError; end
|
|
204
|
-
private_constant :ProbeError
|
|
205
|
-
|
|
206
|
-
# CompletionJoin relies on these Async behaviors: Task#finish! clears
|
|
207
|
-
# the fiber and then calls a per-instance #consume, Node#consume
|
|
208
|
-
# calls each ancestor's #consume when a descendant leaves, and
|
|
209
|
-
# Scheduler#push transfers a duck-typed object on a later iteration.
|
|
210
|
-
class FinalizationProbe
|
|
211
|
-
EXITS = { return: "normal return", raise: "a raised failure", cancel: "cancellation" }.freeze
|
|
212
|
-
|
|
213
|
-
# crashed_step keeps the step an exception left, before teardown relabels it.
|
|
214
|
-
attr_reader :step, :crashed_step
|
|
215
|
-
|
|
216
|
-
def initialize
|
|
217
|
-
@step = "starting its reactor"
|
|
218
|
-
@failures = []
|
|
219
|
-
@tasks = []
|
|
220
|
-
@gates = []
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
# A snapshot the timeout path reports while the probe may still run.
|
|
224
|
-
def failures = @failures.dup
|
|
225
|
-
|
|
226
|
-
def call
|
|
227
|
-
Sync do |root|
|
|
228
|
-
@root = root
|
|
229
|
-
EXITS.each_key do |exit|
|
|
230
|
-
leaf(exit)
|
|
231
|
-
ancestor(exit)
|
|
232
|
-
end
|
|
233
|
-
deferred_push
|
|
234
|
-
rescue Exception # rubocop:disable Lint/RescueException -- recorded only; re-raised to the thread's report
|
|
235
|
-
@crashed_step = @step
|
|
236
|
-
raise
|
|
237
|
-
ensure
|
|
238
|
-
@step = "stopping leftover probe tasks"
|
|
239
|
-
release
|
|
240
|
-
@step = "closing its reactor"
|
|
241
|
-
end
|
|
242
|
-
@failures
|
|
243
|
-
end
|
|
244
|
-
|
|
245
|
-
private
|
|
246
|
-
|
|
247
|
-
def leaf(exit)
|
|
248
|
-
@step = "finalizing a task after #{EXITS[exit]}"
|
|
249
|
-
gate = self.gate
|
|
250
|
-
task = ::Async::Task.new(@root, finished: false) { body(exit, gate) }
|
|
251
|
-
seen = observe(task)
|
|
252
|
-
task.run
|
|
253
|
-
finish(task, exit)
|
|
254
|
-
return if task.finished? && cleared?(seen)
|
|
255
|
-
|
|
256
|
-
fail!("Task finalization after #{EXITS[exit]} no longer calls a per-instance #consume once the " \
|
|
257
|
-
"task's fiber is cleared (observed #{describe(seen)})")
|
|
258
|
-
end
|
|
259
|
-
|
|
260
|
-
def ancestor(exit)
|
|
261
|
-
@step = "finalizing a parent after #{EXITS[exit]} while its child waits"
|
|
262
|
-
child_gate = gate
|
|
263
|
-
parent_gate = gate
|
|
264
|
-
started = gate
|
|
265
|
-
child = nil
|
|
266
|
-
parent = ::Async::Task.new(@root, finished: false) do |task|
|
|
267
|
-
child = task.async(finished: false) { |inner| inner.defer_cancel { child_gate.wait } }
|
|
268
|
-
@tasks << child
|
|
269
|
-
started.resolve(true)
|
|
270
|
-
body(exit, parent_gate)
|
|
271
|
-
end
|
|
272
|
-
seen = observe(parent)
|
|
273
|
-
parent.run
|
|
274
|
-
# The child's eager start queues the parent behind this task.
|
|
275
|
-
started.wait
|
|
276
|
-
finish(parent, exit)
|
|
277
|
-
if parent.finished? || seen.any? { |_, finished| finished }
|
|
278
|
-
return fail!("a parent whose block ended by #{EXITS[exit]} is finished while its child still waits")
|
|
279
|
-
end
|
|
280
|
-
|
|
281
|
-
@step = "finishing the waiting child of a parent that ended by #{EXITS[exit]}"
|
|
282
|
-
child_gate.resolve(true)
|
|
283
|
-
child&.wait
|
|
284
|
-
return if parent.finished? && cleared?(seen)
|
|
285
|
-
|
|
286
|
-
fail!("Task finalization no longer calls a parent's per-instance #consume when its last child " \
|
|
287
|
-
"finishes after the parent ended by #{EXITS[exit]} (observed #{describe(seen)})")
|
|
288
|
-
end
|
|
289
|
-
|
|
290
|
-
def deferred_push
|
|
291
|
-
@step = "running an object pushed to the scheduler from #consume"
|
|
292
|
-
done = ::Async::Promise.new
|
|
293
|
-
fiber = pushed = nil
|
|
294
|
-
task = ::Async::Task.new(@root, finished: false) { fiber = Fiber.current }
|
|
295
|
-
observe(task) do |target|
|
|
296
|
-
next if pushed || !target.fiber.nil?
|
|
297
|
-
|
|
298
|
-
pushed = Pushed.new(fiber, done)
|
|
299
|
-
Fiber.scheduler.push(pushed)
|
|
300
|
-
pushed.consumed
|
|
301
|
-
end
|
|
302
|
-
task.run
|
|
303
|
-
# Without a cleared-fiber #consume the finalization steps report it.
|
|
304
|
-
return unless pushed
|
|
305
|
-
|
|
306
|
-
begin
|
|
307
|
-
@root.with_timeout(STEP_SECONDS) { done.wait }
|
|
308
|
-
rescue ::Async::TimeoutError
|
|
309
|
-
return fail!("Fiber.scheduler.push no longer transfers a pushed object")
|
|
310
|
-
end
|
|
311
|
-
if pushed.inline
|
|
312
|
-
fail!("Fiber.scheduler.push ran a pushed object inline during #consume, not on a later iteration")
|
|
313
|
-
elsif pushed.fiber_alive
|
|
314
|
-
fail!("a pushed object ran while the finished task's original fiber was still alive")
|
|
315
|
-
end
|
|
316
|
-
end
|
|
317
|
-
|
|
318
|
-
def body(exit, gate)
|
|
319
|
-
case exit
|
|
320
|
-
when :raise then raise ProbeError, "probe failure"
|
|
321
|
-
when :cancel then gate.wait
|
|
322
|
-
end
|
|
323
|
-
end
|
|
324
|
-
|
|
325
|
-
def finish(task, exit)
|
|
326
|
-
if exit == :cancel
|
|
327
|
-
raise ProbeError, "cancellation probe task did not suspend" unless task.alive?
|
|
328
|
-
|
|
329
|
-
task.cancel
|
|
330
|
-
end
|
|
331
|
-
begin
|
|
332
|
-
task.wait
|
|
333
|
-
rescue ProbeError
|
|
334
|
-
nil
|
|
335
|
-
end
|
|
336
|
-
end
|
|
337
|
-
|
|
338
|
-
def gate
|
|
339
|
-
gate = ::Async::Promise.new
|
|
340
|
-
@gates << gate
|
|
341
|
-
gate
|
|
342
|
-
end
|
|
343
|
-
|
|
344
|
-
def observe(task, &observer)
|
|
345
|
-
seen = []
|
|
346
|
-
task.instance_variable_set(:@connectrpc_contract_observer, lambda do |target|
|
|
347
|
-
seen << [target.fiber.nil?, target.finished?]
|
|
348
|
-
observer&.call(target)
|
|
349
|
-
end)
|
|
350
|
-
task.extend(ConsumeSpy)
|
|
351
|
-
@tasks << task
|
|
352
|
-
seen
|
|
353
|
-
end
|
|
354
|
-
|
|
355
|
-
def cleared?(seen) = seen.any? { |fiber_cleared, finished| fiber_cleared && finished }
|
|
356
|
-
|
|
357
|
-
def describe(seen)
|
|
358
|
-
return "no #consume call" if seen.empty?
|
|
359
|
-
|
|
360
|
-
seen.map do |fiber_cleared, finished|
|
|
361
|
-
"#{fiber_cleared ? 'cleared' : 'live'} fiber, #{finished ? 'finished' : 'unfinished'}"
|
|
362
|
-
end.join("; ")
|
|
363
|
-
end
|
|
364
|
-
|
|
365
|
-
def fail!(text)
|
|
366
|
-
@failures << UpstreamContract.failure("async", text)
|
|
367
|
-
nil
|
|
368
|
-
end
|
|
369
|
-
|
|
370
|
-
# Lets the probe reactor end after a detected failure.
|
|
371
|
-
def release
|
|
372
|
-
@gates.each { |gate| gate.resolve(nil) unless gate.resolved? }
|
|
373
|
-
@tasks.each do |task|
|
|
374
|
-
task.stop unless task.finished?
|
|
375
|
-
rescue StandardError
|
|
376
|
-
nil
|
|
377
|
-
end
|
|
378
|
-
end
|
|
379
|
-
end
|
|
380
|
-
private_constant :FinalizationProbe
|
|
381
137
|
end
|
|
382
138
|
end
|
|
383
139
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: connectrpc-async
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- jimeh
|
|
8
8
|
bindir: bin
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2026-11-
|
|
10
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: connectrpc
|