Skip to content

Commit

Permalink
fix: fallback for servers not sending NEW_TOKEN frame
Browse files Browse the repository at this point in the history
  • Loading branch information
mxinden committed Apr 18, 2024
1 parent 2935e59 commit 892d738
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
11 changes: 5 additions & 6 deletions neqo-bin/src/client/http09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,10 @@ impl<'a> super::Handler for Handler<'a> {
Ok(false)
}

fn take_token(&mut self) -> Option<ResumptionToken> {
self.token.take()
}

fn has_token(&self) -> bool {
self.token.is_some()
fn take_token(&mut self, client: &mut Self::Client) -> Option<ResumptionToken> {
self.token
.take()
.or_else(|| client.take_resumption_token(Instant::now()))
}
}

Expand Down Expand Up @@ -262,6 +260,7 @@ impl<'b> Handler<'b> {
);
}
if fin {
qdebug!("READ[{stream_id}]: received fin");
return Ok(true);
}
}
Expand Down
10 changes: 4 additions & 6 deletions neqo-bin/src/client/http3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,10 @@ impl<'a> super::Handler for Handler<'a> {
Ok(self.url_handler.done())
}

fn take_token(&mut self) -> Option<ResumptionToken> {
self.token.take()
}

fn has_token(&self) -> bool {
self.token.is_some()
fn take_token(&mut self, client: &mut Self::Client) -> Option<ResumptionToken> {
self.token
.take()
.or_else(|| client.take_resumption_token(Instant::now()))
}
}

Expand Down
48 changes: 21 additions & 27 deletions neqo-bin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,7 @@ trait Handler {
type Client: Client;

fn handle(&mut self, client: &mut Self::Client) -> Res<bool>;
fn take_token(&mut self) -> Option<ResumptionToken>;
fn has_token(&self) -> bool;
fn take_token(&mut self, client: &mut Self::Client) -> Option<ResumptionToken>;
}

/// Network client, e.g. [`neqo_transport::Connection`] or [`neqo_http3::Http3Client`].
Expand Down Expand Up @@ -374,35 +373,20 @@ struct Runner<'a, H: Handler> {

impl<'a, H: Handler> Runner<'a, H> {
async fn run(mut self) -> Res<Option<ResumptionToken>> {
loop {
let close_reason = loop {
let handler_done = self.handler.handle(&mut self.client)?;

match (handler_done, self.args.resume, self.handler.has_token()) {
// Handler isn't done. Continue.
(false, _, _) => {},
// Handler done. Resumption token needed but not present. Continue.
(true, true, false) => {
qdebug!("Handler done. Waiting for resumption token.");
}
// Handler is done, no resumption token needed. Close.
(true, false, _) |
// Handler is done, resumption token needed and present. Close.
(true, true, true) => {
self.client.close(Instant::now(), 0, "kthxbye!");
}
}

self.process_output().await?;

if let Some(reason) = self.client.is_closed() {
if self.args.stats {
qinfo!("{:?}", self.client.stats());
match (handler_done, self.client.is_closed()) {
// more work
(false, _) => {}
// no more work, closing connection
(true, None) => {
self.client.close(Instant::now(), 0, "kthxbye!");
continue;
}
return match reason {
ConnectionError::Transport(TransportError::NoError)
| ConnectionError::Application(0) => Ok(self.handler.take_token()),
_ => Err(reason.into()),
};
// no more work, connection closed, terminating
(true, Some(reason)) => break reason,
}

if self.client.has_events() {
Expand All @@ -415,6 +399,16 @@ impl<'a, H: Handler> Runner<'a, H> {
self.timeout = None;
}
}
};

if self.args.stats {
qinfo!("{:?}", self.client.stats());
}

match close_reason {
ConnectionError::Transport(TransportError::NoError)
| ConnectionError::Application(0) => Ok(self.handler.take_token(&mut self.client)),
_ => Err(close_reason.into()),
}
}

Expand Down

0 comments on commit 892d738

Please sign in to comment.