Skip to content

Commit

Permalink
quinn-rs#2008: Make max_idle_timeout negotiation commutative.
Browse files Browse the repository at this point in the history
  • Loading branch information
mstyura committed Oct 9, 2024
1 parent 15a4dce commit b02b846
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions quinn-proto/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3290,12 +3290,11 @@ impl Connection {

fn set_peer_params(&mut self, params: TransportParameters) {
self.streams.set_params(&params);
self.idle_timeout = match (self.config.max_idle_timeout, params.max_idle_timeout) {
(None, VarInt(0)) => None,
(None, x) => Some(x),
(Some(x), VarInt(0)) => Some(x),
(Some(x), y) => Some(cmp::min(x, y)),
};
self.idle_timeout = negotiate_max_idle_timeout(
self.config.max_idle_timeout.unwrap_or_default(),
params.max_idle_timeout,
);
trace!("negotiated max idle timeout {:?}", self.idle_timeout);
if let Some(ref info) = params.preferred_address {
self.rem_cids.insert(frame::NewConnectionId {
sequence: 1,
Expand Down Expand Up @@ -3774,3 +3773,32 @@ impl SentFrames {
&& self.retransmits.is_empty(streams)
}
}

fn negotiate_max_idle_timeout(x: VarInt, y: VarInt) -> Option<VarInt> {
match (x, y) {
(VarInt(0), VarInt(0)) => None,
(VarInt(0), y) => Some(y),
(x, VarInt(0)) => Some(x),
(x, y) => Some(cmp::min(x, y)),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn negotiate_max_idle_timeout_commutative() {
let test_params = [
(VarInt(0), VarInt(0), None),
(VarInt(2), VarInt(0), Some(VarInt(2))),
(VarInt(0), VarInt(3), Some(VarInt(3))),
(VarInt(1), VarInt(4), Some(VarInt(1))),
];

for (left, right, result) in test_params {
assert_eq!(negotiate_max_idle_timeout(left, right), result);
assert_eq!(negotiate_max_idle_timeout(right, left), result);
}
}
}

0 comments on commit b02b846

Please sign in to comment.