2019-05-15 07:07:19 +00:00
|
|
|
// Implement simple flow control for h2 client
|
|
|
|
//
|
|
|
|
// See also: hyper/src/proto/h2/mod.rs
|
|
|
|
|
2019-08-23 11:43:17 +00:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{format_err, Error};
|
2021-07-09 13:29:42 +00:00
|
|
|
use bytes::Bytes;
|
2019-08-23 11:43:17 +00:00
|
|
|
use futures::{ready, Future};
|
2019-08-22 11:46:31 +00:00
|
|
|
use h2::SendStream;
|
2019-05-15 07:07:19 +00:00
|
|
|
|
|
|
|
pub struct PipeToSendStream {
|
|
|
|
body_tx: SendStream<Bytes>,
|
|
|
|
data: Option<Bytes>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PipeToSendStream {
|
|
|
|
pub fn new(data: Bytes, tx: SendStream<Bytes>) -> PipeToSendStream {
|
|
|
|
PipeToSendStream {
|
|
|
|
body_tx: tx,
|
|
|
|
data: Some(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for PipeToSendStream {
|
2019-08-23 11:43:17 +00:00
|
|
|
type Output = Result<(), Error>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let this = self.get_mut();
|
2019-05-15 07:07:19 +00:00
|
|
|
|
2019-09-11 10:31:39 +00:00
|
|
|
if this.data != None {
|
|
|
|
// just reserve 1 byte to make sure there's some
|
|
|
|
// capacity available. h2 will handle the capacity
|
|
|
|
// management for the actual body chunk.
|
|
|
|
this.body_tx.reserve_capacity(1);
|
2019-05-15 07:07:19 +00:00
|
|
|
|
2019-09-11 10:31:39 +00:00
|
|
|
if this.body_tx.capacity() == 0 {
|
|
|
|
loop {
|
|
|
|
match ready!(this.body_tx.poll_capacity(cx)) {
|
|
|
|
Some(Err(err)) => return Poll::Ready(Err(Error::from(err))),
|
|
|
|
Some(Ok(0)) => {}
|
|
|
|
Some(Ok(_)) => break,
|
|
|
|
None => return Poll::Ready(Err(format_err!("protocol canceled"))),
|
2019-05-15 07:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-26 09:36:01 +00:00
|
|
|
} else if let Poll::Ready(reset) = this.body_tx.poll_reset(cx) {
|
|
|
|
return Poll::Ready(Err(match reset {
|
|
|
|
Ok(reason) => format_err!("stream received RST_STREAM: {:?}", reason),
|
|
|
|
Err(err) => Error::from(err),
|
|
|
|
}));
|
2019-05-15 07:07:19 +00:00
|
|
|
}
|
2019-09-11 10:31:39 +00:00
|
|
|
|
|
|
|
this.body_tx
|
|
|
|
.send_data(this.data.take().unwrap(), true)
|
|
|
|
.map_err(Error::from)?;
|
|
|
|
|
2019-10-26 09:36:01 +00:00
|
|
|
Poll::Ready(Ok(()))
|
2019-09-11 10:31:39 +00:00
|
|
|
} else {
|
|
|
|
if let Poll::Ready(reset) = this.body_tx.poll_reset(cx) {
|
|
|
|
return Poll::Ready(Err(match reset {
|
|
|
|
Ok(reason) => format_err!("stream received RST_STREAM: {:?}", reason),
|
|
|
|
Err(err) => Error::from(err),
|
|
|
|
}));
|
|
|
|
}
|
2019-10-26 09:36:01 +00:00
|
|
|
Poll::Ready(Ok(()))
|
2019-05-15 07:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|