1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

document h2 ping-pong

This commit is contained in:
Rob Ede 2022-07-24 16:42:35 +01:00
parent 8ff489aa90
commit 4bbe60b609
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
2 changed files with 24 additions and 17 deletions

View File

@ -35,7 +35,7 @@ impl Default for ServiceConfig {
} }
impl ServiceConfig { impl ServiceConfig {
/// Create instance of `ServiceConfig` /// Create instance of `ServiceConfig`.
pub fn new( pub fn new(
keep_alive: KeepAlive, keep_alive: KeepAlive,
client_request_timeout: Duration, client_request_timeout: Duration,

View File

@ -67,7 +67,7 @@ where
timer timer
}) })
.unwrap_or_else(|| Box::pin(sleep(dur))), .unwrap_or_else(|| Box::pin(sleep(dur))),
on_flight: false, in_flight: false,
ping_pong: conn.ping_pong().unwrap(), ping_pong: conn.ping_pong().unwrap(),
}); });
@ -84,9 +84,14 @@ where
} }
struct H2PingPong { struct H2PingPong {
timer: Pin<Box<Sleep>>, /// Handle to send ping frames from the peer.
on_flight: bool,
ping_pong: PingPong, ping_pong: PingPong,
/// True when a ping has been sent and is waiting for a reply.
in_flight: bool,
/// Timeout for pong response.
timer: Pin<Box<Sleep>>,
} }
impl<T, S, B, X, U> Future for Dispatcher<T, S, B, X, U> impl<T, S, B, X, U> Future for Dispatcher<T, S, B, X, U>
@ -152,26 +157,28 @@ where
}); });
} }
Poll::Ready(None) => return Poll::Ready(Ok(())), Poll::Ready(None) => return Poll::Ready(Ok(())),
Poll::Pending => match this.ping_pong.as_mut() { Poll::Pending => match this.ping_pong.as_mut() {
Some(ping_pong) => loop { Some(ping_pong) => loop {
if ping_pong.on_flight { if ping_pong.in_flight {
// When have on flight ping pong. poll pong and and keep alive timer. // When there is an in-flight ping-pong, poll pong and and keep-alive
// on success pong received update keep alive timer to determine the next timing of // timer. On successful pong received, update keep-alive timer to
// ping pong. // determine the next timing of ping pong.
match ping_pong.ping_pong.poll_pong(cx)? { match ping_pong.ping_pong.poll_pong(cx)? {
Poll::Ready(_) => { Poll::Ready(_) => {
ping_pong.on_flight = false; ping_pong.in_flight = false;
let dead_line = this.config.keep_alive_deadline().unwrap(); let dead_line = this.config.keep_alive_deadline().unwrap();
ping_pong.timer.as_mut().reset(dead_line.into()); ping_pong.timer.as_mut().reset(dead_line.into());
} }
Poll::Pending => { Poll::Pending => {
return ping_pong.timer.as_mut().poll(cx).map(|_| Ok(())) return ping_pong.timer.as_mut().poll(cx).map(|_| Ok(()));
} }
} }
} else { } else {
// When there is no on flight ping pong. keep alive timer is used to wait for next // When there is no in-flight ping-pong, keep-alive timer is used to
// timing of ping pong. Therefore at this point it serves as an interval instead. // wait for next timing of ping-pong. Therefore, at this point it serves
// as an interval instead.
ready!(ping_pong.timer.as_mut().poll(cx)); ready!(ping_pong.timer.as_mut().poll(cx));
ping_pong.ping_pong.send_ping(Ping::opaque())?; ping_pong.ping_pong.send_ping(Ping::opaque())?;
@ -179,7 +186,7 @@ where
let dead_line = this.config.keep_alive_deadline().unwrap(); let dead_line = this.config.keep_alive_deadline().unwrap();
ping_pong.timer.as_mut().reset(dead_line.into()); ping_pong.timer.as_mut().reset(dead_line.into());
ping_pong.on_flight = true; ping_pong.in_flight = true;
} }
}, },
None => return Poll::Pending, None => return Poll::Pending,
@ -287,13 +294,13 @@ fn prepare_response(
_ => {} _ => {}
} }
let _ = match size { match size {
BodySize::None | BodySize::Stream => None, BodySize::None | BodySize::Stream => {}
BodySize::Sized(0) => { BodySize::Sized(0) => {
#[allow(clippy::declare_interior_mutable_const)] #[allow(clippy::declare_interior_mutable_const)]
const HV_ZERO: HeaderValue = HeaderValue::from_static("0"); const HV_ZERO: HeaderValue = HeaderValue::from_static("0");
res.headers_mut().insert(CONTENT_LENGTH, HV_ZERO) res.headers_mut().insert(CONTENT_LENGTH, HV_ZERO);
} }
BodySize::Sized(len) => { BodySize::Sized(len) => {
@ -302,7 +309,7 @@ fn prepare_response(
res.headers_mut().insert( res.headers_mut().insert(
CONTENT_LENGTH, CONTENT_LENGTH,
HeaderValue::from_str(buf.format(*len)).unwrap(), HeaderValue::from_str(buf.format(*len)).unwrap(),
) );
} }
}; };