connectrpc-hyper 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 +110 -11
- data/ext/connectrpc_hyper/src/body.rs +88 -2
- data/ext/connectrpc_hyper/src/call.rs +396 -49
- data/ext/connectrpc_hyper/src/failure.rs +7 -0
- data/ext/connectrpc_hyper/src/lib.rs +57 -1
- data/ext/connectrpc_hyper/src/pool.rs +17 -6
- data/ext/connectrpc_hyper/src/runtime.rs +230 -24
- data/lib/connectrpc/hyper/errors.rb +8 -4
- data/lib/connectrpc/hyper/exchange.rb +3 -3
- data/lib/connectrpc/hyper/transport.rb +5 -1
- data/lib/connectrpc/hyper/version.rb +1 -1
- metadata +2 -2
|
@@ -3,21 +3,23 @@
|
|
|
3
3
|
//! independent (core allows one reader and one writer at once), each taken
|
|
4
4
|
//! out of its lock for the duration of an operation. `abort` fires a signal
|
|
5
5
|
//! that every wait and the request body observe, so it wins over busy
|
|
6
|
-
//! operations instead of waiting for them.
|
|
6
|
+
//! operations instead of waiting for them. On HTTP/2 a response that ends
|
|
7
|
+
//! while the upload is open ends the upload too (`Upload`).
|
|
7
8
|
use std::future::Future;
|
|
8
9
|
use std::pin::{Pin, pin};
|
|
9
10
|
use std::sync::atomic::{AtomicBool, AtomicU16, Ordering};
|
|
10
|
-
use std::sync::{Arc, Mutex};
|
|
11
|
-
use std::task::Poll;
|
|
11
|
+
use std::sync::{Arc, Mutex, OnceLock};
|
|
12
|
+
use std::task::{Context, Poll, Waker};
|
|
12
13
|
use std::time::Instant;
|
|
13
14
|
|
|
14
15
|
use bytes::Bytes;
|
|
15
16
|
use http_body_util::BodyExt;
|
|
16
|
-
use hyper::body::Incoming;
|
|
17
|
+
use hyper::body::{Frame, Incoming};
|
|
17
18
|
use hyper::client::conn::TrySendError;
|
|
18
|
-
use tokio::sync::
|
|
19
|
+
use tokio::sync::oneshot::error::TryRecvError;
|
|
20
|
+
use tokio::sync::{Notify, mpsc, oneshot};
|
|
19
21
|
|
|
20
|
-
use crate::body::CallBody;
|
|
22
|
+
use crate::body::{CallBody, Upload};
|
|
21
23
|
use crate::failure::{Failure, TlsFault};
|
|
22
24
|
use crate::gvl::Signal;
|
|
23
25
|
use crate::holds::Holds;
|
|
@@ -129,7 +131,64 @@ async fn answered<T, E>(
|
|
|
129
131
|
promise.await.map_err(Unanswered::Failed)
|
|
130
132
|
}
|
|
131
133
|
|
|
132
|
-
type
|
|
134
|
+
type Outcome = Result<(hyper::Response<Incoming>, Lease), Failure>;
|
|
135
|
+
|
|
136
|
+
/// A response head and its lease.
|
|
137
|
+
struct Answer {
|
|
138
|
+
response: hyper::Response<Incoming>,
|
|
139
|
+
lease: Lease,
|
|
140
|
+
/// HEADERS carried END_STREAM: a gRPC trailers-only response. h2 keeps
|
|
141
|
+
/// trailers that arrived right behind the head in its queue, so this is
|
|
142
|
+
/// false when the status came in a separate trailing HEADERS frame. Read
|
|
143
|
+
/// when the head is published: h2 stops reporting the end once the
|
|
144
|
+
/// stream was reset, as the upload's end may reset it (`Upload`).
|
|
145
|
+
end_stream: bool,
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
type Delivered = Result<Answer, Failure>;
|
|
149
|
+
|
|
150
|
+
/// Publishes a dispatch's response outcome. `respond` owns it, so a task that
|
|
151
|
+
/// is never polled, refused by a sealed tracker or aborted drops it too.
|
|
152
|
+
/// Publication order: a failure goes into `head_failure` before it is sent,
|
|
153
|
+
/// and `responded` fires only after the outcome was sent or the sender
|
|
154
|
+
/// dropped (`Drop`). So once `responded` fired, the outcome is in
|
|
155
|
+
/// `head_failure` (a failure), in the receiver (a response), or the closed
|
|
156
|
+
/// channel says none will come. An HTTP/2 head that carried END_STREAM ends
|
|
157
|
+
/// the response once it was sent, so the upload ends even if nobody reads;
|
|
158
|
+
/// it is the first point that observes the response's end.
|
|
159
|
+
struct Responder {
|
|
160
|
+
deliver: Option<oneshot::Sender<Delivered>>,
|
|
161
|
+
head_failure: Arc<OnceLock<Failure>>,
|
|
162
|
+
responded: Arc<Signal>,
|
|
163
|
+
upload: Arc<Upload>,
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
impl Responder {
|
|
167
|
+
fn publish(mut self, outcome: Outcome) {
|
|
168
|
+
let delivered = outcome.map(|(response, lease)| Answer {
|
|
169
|
+
end_stream: http_body::Body::is_end_stream(response.body()),
|
|
170
|
+
response,
|
|
171
|
+
lease,
|
|
172
|
+
});
|
|
173
|
+
let ended = matches!(&delivered, Ok(answer) if answer.end_stream && answer.lease.is_h2());
|
|
174
|
+
if let Err(failure) = &delivered {
|
|
175
|
+
let _ = self.head_failure.set(failure.clone());
|
|
176
|
+
}
|
|
177
|
+
if let Some(deliver) = self.deliver.take() {
|
|
178
|
+
let _ = deliver.send(delivered);
|
|
179
|
+
}
|
|
180
|
+
if ended {
|
|
181
|
+
self.upload.end_response();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
impl Drop for Responder {
|
|
187
|
+
fn drop(&mut self) {
|
|
188
|
+
drop(self.deliver.take());
|
|
189
|
+
self.responded.fire();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
133
192
|
|
|
134
193
|
enum Dispatch {
|
|
135
194
|
Pending,
|
|
@@ -151,12 +210,30 @@ pub struct RecvHalf {
|
|
|
151
210
|
buffer: Bytes,
|
|
152
211
|
pub trailers: Option<http::HeaderMap>,
|
|
153
212
|
done: bool,
|
|
213
|
+
/// The head's failure, or a body error `read` or a write's `probe`
|
|
214
|
+
/// consumed: the outcome stays here for the operations after it, since
|
|
215
|
+
/// hyper hands out a delivery, and an HTTP/1.1 body error, only once.
|
|
216
|
+
failed: Option<Failure>,
|
|
154
217
|
/// The response's connection's TLS failure record, for body errors.
|
|
155
218
|
fault: Arc<TlsFault>,
|
|
219
|
+
/// The call's upload, once an HTTP/2 response was installed.
|
|
220
|
+
upload: Option<Arc<Upload>>,
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
impl RecvHalf {
|
|
224
|
+
/// The response ended (END_STREAM), as this half consumed it.
|
|
225
|
+
fn end_response(&self) {
|
|
226
|
+
if let Some(upload) = &self.upload {
|
|
227
|
+
upload.end_response();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
156
230
|
}
|
|
157
231
|
|
|
158
232
|
pub struct SendHalf {
|
|
159
233
|
tx: Option<mpsc::Sender<Bytes>>,
|
|
234
|
+
/// The stream's own failure, once a write that found the stream ended
|
|
235
|
+
/// learned it.
|
|
236
|
+
ended: Option<Failure>,
|
|
160
237
|
}
|
|
161
238
|
|
|
162
239
|
pub struct CallState {
|
|
@@ -169,6 +246,14 @@ pub struct CallState {
|
|
|
169
246
|
/// Fires when the request was stranded: its body, which owns the upload
|
|
170
247
|
/// channel's receiver, may stay alive, so a blocked `write` returns.
|
|
171
248
|
stranded: Arc<Signal>,
|
|
249
|
+
/// The response's failure, and the signal that its outcome was
|
|
250
|
+
/// published, as `Responder` orders them.
|
|
251
|
+
head_failure: Arc<OnceLock<Failure>>,
|
|
252
|
+
responded: Arc<Signal>,
|
|
253
|
+
/// How the upload ends on HTTP/2 (`Upload`).
|
|
254
|
+
upload: Arc<Upload>,
|
|
255
|
+
/// Notified whenever an operation returns its half (`restore`).
|
|
256
|
+
restored: Notify,
|
|
172
257
|
aborted: AtomicBool,
|
|
173
258
|
finished: AtomicBool,
|
|
174
259
|
failed: AtomicBool,
|
|
@@ -201,13 +286,20 @@ impl CallState {
|
|
|
201
286
|
cancel,
|
|
202
287
|
abort: Signal::new(),
|
|
203
288
|
stranded: Signal::new(),
|
|
289
|
+
head_failure: Arc::new(OnceLock::new()),
|
|
290
|
+
responded: Signal::new(),
|
|
291
|
+
upload: Upload::new(),
|
|
292
|
+
restored: Notify::new(),
|
|
204
293
|
aborted: AtomicBool::new(false),
|
|
205
294
|
finished: AtomicBool::new(false),
|
|
206
295
|
failed: AtomicBool::new(false),
|
|
207
296
|
ended: Arc::new(AtomicBool::new(false)),
|
|
208
297
|
body: Mutex::new(Some(rx)),
|
|
209
298
|
dispatch: tokio::sync::Mutex::new(Dispatch::Pending),
|
|
210
|
-
send: Mutex::new(Some(SendHalf {
|
|
299
|
+
send: Mutex::new(Some(SendHalf {
|
|
300
|
+
tx: Some(tx),
|
|
301
|
+
ended: None,
|
|
302
|
+
})),
|
|
211
303
|
recv: Mutex::new(Some(RecvHalf::default())),
|
|
212
304
|
lease: Mutex::new(None),
|
|
213
305
|
inflight: Mutex::new(Some(inflight)),
|
|
@@ -233,12 +325,17 @@ impl CallState {
|
|
|
233
325
|
}
|
|
234
326
|
|
|
235
327
|
/// Returns a half after an operation, unless the call was aborted
|
|
236
|
-
/// meanwhile; then the half is dropped here instead.
|
|
328
|
+
/// meanwhile; then the half is dropped here instead. Wakes a write that
|
|
329
|
+
/// waits for the response's end (`response_end`): the operation may have
|
|
330
|
+
/// taken the write's wake with the receive half.
|
|
237
331
|
pub fn restore<T>(&self, slot: &Mutex<Option<T>>, half: T) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
332
|
+
{
|
|
333
|
+
let mut guard = slot.lock().unwrap_or_else(|e| e.into_inner());
|
|
334
|
+
if !self.aborted.load(Ordering::SeqCst) {
|
|
335
|
+
*guard = Some(half);
|
|
336
|
+
}
|
|
241
337
|
}
|
|
338
|
+
self.restored.notify_waiters();
|
|
242
339
|
}
|
|
243
340
|
|
|
244
341
|
pub fn mark_failed(&self) {
|
|
@@ -274,8 +371,25 @@ impl CallState {
|
|
|
274
371
|
.unwrap_or_else(|e| e.into_inner())
|
|
275
372
|
.take()
|
|
276
373
|
.expect("request body is dispatched once");
|
|
374
|
+
// Only an HTTP/2 request is replayed from a body hyper dropped unread
|
|
375
|
+
// (its mark can say it was never sent). On HTTP/1.1 the receiver must
|
|
376
|
+
// not outlive that body: a body hyper never polls (a declared
|
|
377
|
+
// Content-Length of 0) would otherwise keep the upload channel open
|
|
378
|
+
// under a blocked write.
|
|
277
379
|
let (home, homed) = oneshot::channel();
|
|
278
|
-
let
|
|
380
|
+
let home = if lease.is_h2() {
|
|
381
|
+
home
|
|
382
|
+
} else {
|
|
383
|
+
oneshot::channel().0
|
|
384
|
+
};
|
|
385
|
+
let upload = lease.is_h2().then(|| self.upload.clone());
|
|
386
|
+
let body = CallBody::stream(
|
|
387
|
+
rx,
|
|
388
|
+
self.ended.clone(),
|
|
389
|
+
self.abort.clone(),
|
|
390
|
+
home,
|
|
391
|
+
upload.clone(),
|
|
392
|
+
);
|
|
279
393
|
let request = match self.spec.build(body, self.deadline, lease.is_h2()) {
|
|
280
394
|
Ok(request) => request,
|
|
281
395
|
Err(failure) => {
|
|
@@ -297,6 +411,7 @@ impl CallState {
|
|
|
297
411
|
require_h2: self.require_h2,
|
|
298
412
|
deadline: self.deadline,
|
|
299
413
|
ended: self.ended.clone(),
|
|
414
|
+
upload,
|
|
300
415
|
lease,
|
|
301
416
|
sent,
|
|
302
417
|
mark,
|
|
@@ -305,7 +420,12 @@ impl CallState {
|
|
|
305
420
|
self.abort.clone(),
|
|
306
421
|
self.cancel.clone(),
|
|
307
422
|
self.stranded.clone(),
|
|
308
|
-
|
|
423
|
+
Responder {
|
|
424
|
+
deliver: Some(deliver),
|
|
425
|
+
head_failure: self.head_failure.clone(),
|
|
426
|
+
responded: self.responded.clone(),
|
|
427
|
+
upload: self.upload.clone(),
|
|
428
|
+
},
|
|
309
429
|
));
|
|
310
430
|
*state = Dispatch::Dispatched(Some(delivered));
|
|
311
431
|
Ok(())
|
|
@@ -315,6 +435,9 @@ impl CallState {
|
|
|
315
435
|
if recv.head.is_some() {
|
|
316
436
|
return Ok(());
|
|
317
437
|
}
|
|
438
|
+
if let Some(failure) = &recv.failed {
|
|
439
|
+
return Err(failure.clone());
|
|
440
|
+
}
|
|
318
441
|
if recv.pending.is_none() {
|
|
319
442
|
self.dispatch().await?;
|
|
320
443
|
let mut state = self.dispatch.lock().await;
|
|
@@ -326,11 +449,34 @@ impl CallState {
|
|
|
326
449
|
.pending
|
|
327
450
|
.as_mut()
|
|
328
451
|
.ok_or_else(|| Failure::new("state", "response head already taken"))?;
|
|
329
|
-
let delivered = pending.await;
|
|
452
|
+
let delivered = pending.await.ok();
|
|
330
453
|
recv.pending = None;
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
454
|
+
self.install(recv, delivered)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/// Installs the delivered response on `recv`, or keeps its failure there.
|
|
458
|
+
/// `None`: `respond` ended without delivering, which happens only on this
|
|
459
|
+
/// call's abort, or when forced teardown aborted it or a sealed tracker
|
|
460
|
+
/// refused it. Never awaits, so a write can install a head the response
|
|
461
|
+
/// task already delivered (`probe`).
|
|
462
|
+
fn install(&self, recv: &mut RecvHalf, delivered: Option<Delivered>) -> Result<(), Failure> {
|
|
463
|
+
let installed = self.take_response(recv, delivered);
|
|
464
|
+
if let Err(failure) = &installed {
|
|
465
|
+
recv.failed = Some(failure.clone());
|
|
466
|
+
}
|
|
467
|
+
installed
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
fn take_response(
|
|
471
|
+
&self,
|
|
472
|
+
recv: &mut RecvHalf,
|
|
473
|
+
delivered: Option<Delivered>,
|
|
474
|
+
) -> Result<(), Failure> {
|
|
475
|
+
let Answer {
|
|
476
|
+
response,
|
|
477
|
+
lease,
|
|
478
|
+
end_stream,
|
|
479
|
+
} = delivered.ok_or_else(|| {
|
|
334
480
|
if self.abort.fired() {
|
|
335
481
|
Failure::new("aborted", "exchange aborted")
|
|
336
482
|
} else {
|
|
@@ -349,13 +495,12 @@ impl CallState {
|
|
|
349
495
|
if let Some(fault) = lease.fault() {
|
|
350
496
|
recv.fault = fault.clone();
|
|
351
497
|
}
|
|
498
|
+
if lease.is_h2() {
|
|
499
|
+
recv.upload = Some(self.upload.clone());
|
|
500
|
+
}
|
|
352
501
|
*slot = Some(lease);
|
|
353
502
|
}
|
|
354
503
|
let (parts, body) = response.into_parts();
|
|
355
|
-
// HEADERS carried END_STREAM: a gRPC trailers-only response. h2 keeps
|
|
356
|
-
// trailers that arrived right behind the head in its queue, so this is
|
|
357
|
-
// false when the status came in a separate trailing HEADERS frame.
|
|
358
|
-
let end_stream = http_body::Body::is_end_stream(&body);
|
|
359
504
|
recv.done = end_stream;
|
|
360
505
|
recv.head = Some(Head {
|
|
361
506
|
status: parts.status.as_u16(),
|
|
@@ -377,53 +522,197 @@ impl CallState {
|
|
|
377
522
|
let take = max.max(1).min(recv.buffer.len());
|
|
378
523
|
return Ok(Some(recv.buffer.split_to(take)));
|
|
379
524
|
}
|
|
525
|
+
if let Some(failure) = &recv.failed {
|
|
526
|
+
return Err(failure.clone());
|
|
527
|
+
}
|
|
380
528
|
if recv.done {
|
|
381
529
|
return Ok(None);
|
|
382
530
|
}
|
|
383
531
|
let body = recv.body.as_mut().expect("body after head");
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
Some(Ok(frame)) => match frame.into_data() {
|
|
387
|
-
Ok(data) => recv.buffer = data,
|
|
388
|
-
Err(frame) => {
|
|
389
|
-
if let Ok(trailers) = frame.into_trailers() {
|
|
390
|
-
recv.trailers = Some(trailers);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
},
|
|
394
|
-
Some(Err(error)) => return Err(recv.fault.failure(&error)),
|
|
395
|
-
}
|
|
532
|
+
let frame = body.frame().await;
|
|
533
|
+
apply(recv, frame)?;
|
|
396
534
|
}
|
|
397
535
|
}
|
|
398
536
|
|
|
399
537
|
/// Queues `bytes` once, then returns when hyper has pulled them, which it
|
|
400
538
|
/// does only while the stream has send window: a peer that does not read
|
|
401
539
|
/// blocks the writer.
|
|
540
|
+
///
|
|
541
|
+
/// A write that starts after the stream ended fails with the stream's
|
|
542
|
+
/// own failure when the response side knows it (`ended`). The write in
|
|
543
|
+
/// flight when it ends returns: its bytes were queued, as grpc-go's
|
|
544
|
+
/// `SendMsg` returns once it queued a message and fails the next call.
|
|
545
|
+
///
|
|
546
|
+
/// On HTTP/2 the response's end ends the upload (`Upload`), so a write
|
|
547
|
+
/// that starts after the transport observed it, or that still waits to
|
|
548
|
+
/// queue its bytes then, fails the same way and queues nothing; one that
|
|
549
|
+
/// waits after queueing returns.
|
|
402
550
|
pub async fn write(self: &Arc<Self>, send: &mut SendHalf, bytes: Bytes) -> Result<(), Failure> {
|
|
403
551
|
self.dispatch().await?;
|
|
404
|
-
|
|
552
|
+
if send.tx.is_none() {
|
|
405
553
|
return Err(Failure::new("state", "request body already closed"));
|
|
406
|
-
}
|
|
554
|
+
}
|
|
555
|
+
if self.is_h2() {
|
|
556
|
+
// An end that already arrived, if nobody read it.
|
|
557
|
+
self.probe(&mut Context::from_waker(Waker::noop()));
|
|
558
|
+
if self.upload.response_ended() {
|
|
559
|
+
return Err(self.ended(send).await);
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
let tx = send.tx.as_ref().expect("checked above");
|
|
407
563
|
// A closed channel means hyper dropped the request body: the stream
|
|
408
|
-
// ended or was reset,
|
|
409
|
-
//
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
564
|
+
// ended or was reset, or its connection ended. A stranded request's
|
|
565
|
+
// body may never be dropped or read, so `stranded` ends the wait too,
|
|
566
|
+
// checked first so a write that starts after it fails.
|
|
567
|
+
let queued = tokio::select! {
|
|
568
|
+
biased;
|
|
569
|
+
() = self.stranded.wait() => false,
|
|
570
|
+
() = self.response_end() => false,
|
|
571
|
+
reserved = tx.reserve() => reserved.map(|permit| permit.send(bytes)).is_ok(),
|
|
413
572
|
};
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
Err(_) => return Ok(()),
|
|
573
|
+
if !queued {
|
|
574
|
+
return Err(self.ended(send).await);
|
|
417
575
|
}
|
|
576
|
+
let tx = send.tx.as_ref().expect("a write holds its sender");
|
|
418
577
|
tokio::select! {
|
|
419
578
|
_ = tx.reserve() => {}
|
|
420
579
|
() = self.stranded.wait() => {}
|
|
580
|
+
() = self.response_end() => {}
|
|
421
581
|
}
|
|
422
582
|
Ok(())
|
|
423
583
|
}
|
|
424
584
|
|
|
585
|
+
/// Resolves once the transport observed the response's end on HTTP/2;
|
|
586
|
+
/// never on HTTP/1.1. While it waits it polls the receive half with this
|
|
587
|
+
/// task's waker whenever the half is in its slot (`probe`), so an end
|
|
588
|
+
/// that arrives while nobody reads is observed too. An operation that
|
|
589
|
+
/// takes the half may replace that waker, so returning the half
|
|
590
|
+
/// (`restore`) wakes this wait to poll again; the notification is
|
|
591
|
+
/// registered before the slot is checked.
|
|
592
|
+
async fn response_end(&self) {
|
|
593
|
+
if !self.is_h2() {
|
|
594
|
+
return std::future::pending().await;
|
|
595
|
+
}
|
|
596
|
+
let mut ended = pin!(self.upload.wait());
|
|
597
|
+
let mut restored = pin!(self.restored.notified());
|
|
598
|
+
std::future::poll_fn(|cx| {
|
|
599
|
+
if ended.as_mut().poll(cx).is_ready() {
|
|
600
|
+
return Poll::Ready(());
|
|
601
|
+
}
|
|
602
|
+
while restored.as_mut().poll(cx).is_ready() {
|
|
603
|
+
restored.set(self.restored.notified());
|
|
604
|
+
}
|
|
605
|
+
self.probe(cx);
|
|
606
|
+
match self.upload.response_ended() {
|
|
607
|
+
true => Poll::Ready(()),
|
|
608
|
+
false => Poll::Pending,
|
|
609
|
+
}
|
|
610
|
+
})
|
|
611
|
+
.await;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/// Why a write found the stream ended. On HTTP/2, and for a stranded
|
|
615
|
+
/// request, waits for `respond` to publish the response's outcome,
|
|
616
|
+
/// inside the write, so the write's deadline, cancellation, abort,
|
|
617
|
+
/// shutdown and interrupt waits bound it. That wait is short: hyper's
|
|
618
|
+
/// HTTP/2 pipe drops the body only once the stream closed, which also
|
|
619
|
+
/// resolves the response (`Call::write` rejects a chunk h2 would refuse
|
|
620
|
+
/// as too large, which would drop the body alone); a stranded
|
|
621
|
+
/// request's failure is published right after `stranded` fires; a
|
|
622
|
+
/// response that ended the upload (`Upload`) was already published; and
|
|
623
|
+
/// while a retry runs the replacement body holds the receiver, so the
|
|
624
|
+
/// channel stays open. HTTP/1.1 does not wait: hyper also drops a body
|
|
625
|
+
/// its own framing ended (a declared Content-Length reached, or an
|
|
626
|
+
/// HTTP/1.0 connection) while the response is pending, and waiting for
|
|
627
|
+
/// that response could last indefinitely. A failure the response side
|
|
628
|
+
/// does not know yet is forced teardown's once `shutdown` fired (it
|
|
629
|
+
/// fires before teardown drops any body), otherwise the generic `ended`
|
|
630
|
+
/// on HTTP/2, and on HTTP/1.1 `closed`, since there its connection
|
|
631
|
+
/// ending is what usually drops the body. Each is recomputed by the
|
|
632
|
+
/// next write.
|
|
633
|
+
async fn ended(self: &Arc<Self>, send: &mut SendHalf) -> Failure {
|
|
634
|
+
if let Some(failure) = &send.ended {
|
|
635
|
+
return failure.clone();
|
|
636
|
+
}
|
|
637
|
+
if self.is_h2() || self.stranded.fired() {
|
|
638
|
+
// Spec hold point: armed with a zero bound, it counts writes that
|
|
639
|
+
// wait for the response's outcome.
|
|
640
|
+
self.core.holds.block("write:outcome");
|
|
641
|
+
self.responded.wait().await;
|
|
642
|
+
}
|
|
643
|
+
// A failed head is known even while a reader holds the receive half.
|
|
644
|
+
if let Some(failure) = self.head_failure.get() {
|
|
645
|
+
return failure.clone();
|
|
646
|
+
}
|
|
647
|
+
match self.probe(&mut Context::from_waker(Waker::noop())) {
|
|
648
|
+
Some(failure) => {
|
|
649
|
+
send.ended = Some(failure.clone());
|
|
650
|
+
failure
|
|
651
|
+
}
|
|
652
|
+
None if self.core.shutdown.fired() => Failure::shutdown(),
|
|
653
|
+
None if self.is_h2() => Failure::ended(),
|
|
654
|
+
None => Failure::closed(),
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/// The stream's failure as the receive half shows it now, if it does:
|
|
659
|
+
/// installs a delivered head, then reads frames that are already there
|
|
660
|
+
/// until the buffer holds data or the body ended, which ends the
|
|
661
|
+
/// response (`apply`). Never awaits, and never takes the half out of its
|
|
662
|
+
/// slot, so a concurrent `read` never sees an overlap; a reader that
|
|
663
|
+
/// holds the half reports the failure itself. A pending head or frame
|
|
664
|
+
/// registers `cx`. Lock order: `recv`, then `lease` (`install`);
|
|
665
|
+
/// `dispatch` is only tried. Nothing takes `recv` while it holds either.
|
|
666
|
+
fn probe(&self, cx: &mut Context<'_>) -> Option<Failure> {
|
|
667
|
+
let mut slot = self.recv.lock().unwrap_or_else(|e| e.into_inner());
|
|
668
|
+
let recv = slot.as_mut()?;
|
|
669
|
+
if let Some(failure) = &recv.failed {
|
|
670
|
+
return Some(failure.clone());
|
|
671
|
+
}
|
|
672
|
+
if recv.head.is_none() {
|
|
673
|
+
if recv.pending.is_none() {
|
|
674
|
+
let Ok(mut state) = self.dispatch.try_lock() else {
|
|
675
|
+
return None;
|
|
676
|
+
};
|
|
677
|
+
if let Dispatch::Dispatched(delivered) = &mut *state {
|
|
678
|
+
recv.pending = delivered.take();
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
let pending = recv.pending.as_mut()?;
|
|
682
|
+
let delivered = match pending.try_recv() {
|
|
683
|
+
Ok(delivered) => Some(delivered),
|
|
684
|
+
Err(TryRecvError::Empty) => match Pin::new(pending).poll(cx) {
|
|
685
|
+
Poll::Ready(delivered) => delivered.ok(),
|
|
686
|
+
Poll::Pending => return None,
|
|
687
|
+
},
|
|
688
|
+
Err(TryRecvError::Closed) => None,
|
|
689
|
+
};
|
|
690
|
+
recv.pending = None;
|
|
691
|
+
if let Err(failure) = self.install(recv, delivered) {
|
|
692
|
+
return Some(failure);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
// Data, trailers and the end, with room for empty DATA frames.
|
|
696
|
+
for _ in 0..PROBE_POLLS {
|
|
697
|
+
if !recv.buffer.is_empty() || recv.done {
|
|
698
|
+
return None;
|
|
699
|
+
}
|
|
700
|
+
let body = recv.body.as_mut()?;
|
|
701
|
+
let Poll::Ready(frame) = http_body::Body::poll_frame(Pin::new(body), cx) else {
|
|
702
|
+
return None;
|
|
703
|
+
};
|
|
704
|
+
if let Err(failure) = apply(recv, frame) {
|
|
705
|
+
return Some(failure);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
// More frames may be there: a waiting write polls again.
|
|
709
|
+
cx.waker().wake_by_ref();
|
|
710
|
+
None
|
|
711
|
+
}
|
|
712
|
+
|
|
425
713
|
pub fn close_send(&self) {
|
|
426
714
|
if let Some(half) = self.send.lock().unwrap_or_else(|e| e.into_inner()).as_mut() {
|
|
715
|
+
self.upload.close();
|
|
427
716
|
half.tx = None;
|
|
428
717
|
}
|
|
429
718
|
}
|
|
@@ -445,7 +734,13 @@ impl CallState {
|
|
|
445
734
|
self.core.release_h1(&mut lease);
|
|
446
735
|
}
|
|
447
736
|
}
|
|
448
|
-
|
|
737
|
+
// The writer's side closes with the call; on HTTP/2 the drain already
|
|
738
|
+
// ended the response, which decided the upload's end.
|
|
739
|
+
let send = self.send.lock().unwrap_or_else(|e| e.into_inner()).take();
|
|
740
|
+
if send.is_some() {
|
|
741
|
+
self.upload.close();
|
|
742
|
+
}
|
|
743
|
+
drop(send);
|
|
449
744
|
// The head and trailers stay readable; the h2 stream handle goes, so
|
|
450
745
|
// nothing holds the connection open after the call.
|
|
451
746
|
if let Some(recv) = self.recv.lock().unwrap_or_else(|e| e.into_inner()).as_mut() {
|
|
@@ -481,11 +776,58 @@ impl CallState {
|
|
|
481
776
|
}
|
|
482
777
|
}
|
|
483
778
|
|
|
779
|
+
/// Frames `probe` reads at most without waiting.
|
|
780
|
+
const PROBE_POLLS: usize = 8;
|
|
781
|
+
|
|
782
|
+
/// Applies one response body frame to `recv`, as `read` and `probe` both
|
|
783
|
+
/// consume the body. A body error stays on `recv` for the operations after
|
|
784
|
+
/// it: hyper's HTTP/1.1 body hands it out once. The end, trailers and a DATA
|
|
785
|
+
/// frame that carried END_STREAM (its bytes stay buffered) end the
|
|
786
|
+
/// response. hyper reads a peer's RST_STREAM(NO_ERROR) as the end too, after
|
|
787
|
+
/// which its pipe already dropped the upload.
|
|
788
|
+
fn apply(
|
|
789
|
+
recv: &mut RecvHalf,
|
|
790
|
+
frame: Option<Result<Frame<Bytes>, hyper::Error>>,
|
|
791
|
+
) -> Result<(), Failure> {
|
|
792
|
+
match frame {
|
|
793
|
+
None => {
|
|
794
|
+
recv.done = true;
|
|
795
|
+
recv.end_response();
|
|
796
|
+
}
|
|
797
|
+
Some(Ok(frame)) => match frame.into_data() {
|
|
798
|
+
Ok(data) => {
|
|
799
|
+
recv.buffer = data;
|
|
800
|
+
if recv
|
|
801
|
+
.body
|
|
802
|
+
.as_ref()
|
|
803
|
+
.is_some_and(http_body::Body::is_end_stream)
|
|
804
|
+
{
|
|
805
|
+
recv.end_response();
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
Err(frame) => {
|
|
809
|
+
if let Ok(trailers) = frame.into_trailers() {
|
|
810
|
+
recv.trailers = Some(trailers);
|
|
811
|
+
recv.end_response();
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
},
|
|
815
|
+
Some(Err(error)) => {
|
|
816
|
+
let failure = recv.fault.failure(&error);
|
|
817
|
+
recv.failed = Some(failure.clone());
|
|
818
|
+
return Err(failure);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
Ok(())
|
|
822
|
+
}
|
|
823
|
+
|
|
484
824
|
/// The first dispatch of a streaming call, as `respond` retries it.
|
|
485
825
|
struct Attempt {
|
|
486
826
|
require_h2: bool,
|
|
487
827
|
deadline: Option<Instant>,
|
|
488
828
|
ended: Arc<AtomicBool>,
|
|
829
|
+
/// The upload's end, when the first dispatch went out on HTTP/2.
|
|
830
|
+
upload: Option<Arc<Upload>>,
|
|
489
831
|
lease: Lease,
|
|
490
832
|
sent: Sent,
|
|
491
833
|
mark: Option<Arc<Mark>>,
|
|
@@ -506,12 +848,13 @@ async fn respond(
|
|
|
506
848
|
abort: Arc<Signal>,
|
|
507
849
|
cancel: Arc<Signal>,
|
|
508
850
|
stranded: Arc<Signal>,
|
|
509
|
-
|
|
851
|
+
responder: Responder,
|
|
510
852
|
) {
|
|
511
853
|
let Attempt {
|
|
512
854
|
require_h2,
|
|
513
855
|
deadline,
|
|
514
856
|
ended,
|
|
857
|
+
upload,
|
|
515
858
|
lease,
|
|
516
859
|
sent,
|
|
517
860
|
mark,
|
|
@@ -552,7 +895,7 @@ async fn respond(
|
|
|
552
895
|
// these retries.
|
|
553
896
|
core.holds.reach("retry:unprocessed").await;
|
|
554
897
|
// Retried at most once: this body has no receiver to return.
|
|
555
|
-
CallBody::stream(rx, ended, abort.clone(), oneshot::channel().0)
|
|
898
|
+
CallBody::stream(rx, ended, abort.clone(), oneshot::channel().0, upload)
|
|
556
899
|
}
|
|
557
900
|
};
|
|
558
901
|
let exclude = lease.id();
|
|
@@ -575,12 +918,16 @@ async fn respond(
|
|
|
575
918
|
Err(Unanswered::Stranded) => Err(strand(&lease)),
|
|
576
919
|
}
|
|
577
920
|
};
|
|
578
|
-
let
|
|
921
|
+
let outcome = tokio::select! {
|
|
579
922
|
result = work => result,
|
|
580
923
|
_ = abort.wait() => return,
|
|
581
924
|
_ = core.shutdown.wait() => Err(Failure::shutdown()),
|
|
582
925
|
};
|
|
583
|
-
|
|
926
|
+
// Spec hold point: it parks the outcome before it is published.
|
|
927
|
+
core.holds.reach("respond:publish").await;
|
|
928
|
+
responder.publish(outcome);
|
|
929
|
+
// Spec hold point: armed with a zero bound, it counts outcomes published.
|
|
930
|
+
core.holds.block("respond:published");
|
|
584
931
|
}
|
|
585
932
|
|
|
586
933
|
/// `future`'s output, or None once `deadline` passes.
|
|
@@ -76,6 +76,13 @@ impl Failure {
|
|
|
76
76
|
Self::new("closed", "connection closed")
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
/// A write after hyper dropped the request body, when the stream's own
|
|
80
|
+
/// failure is not known: the server ended or reset the stream, or its
|
|
81
|
+
/// connection ended.
|
|
82
|
+
pub fn ended() -> Self {
|
|
83
|
+
Self::new("ended", "request stream ended")
|
|
84
|
+
}
|
|
85
|
+
|
|
79
86
|
/// Classifies by type, HTTP/2 code and errno, never by message text. hyper
|
|
80
87
|
/// 1.13 fails a request waiting for a stream slot, when its connection
|
|
81
88
|
/// fails with a code other than NO_ERROR, with only that code
|