Debian Codegraph @

third_party/rust/neqo-transport/src/connection/idle.rs

raw
1 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4 // option. This file may not be copied, modified, or distributed
5 // except according to those terms.
6
7 use std::{
8 cmp::{max, min},
9 time::{Duration, Instant},
10 };
11
12 use neqo_common::qtrace;
13
14 use crate::recovery::RecoveryToken;
15
16 #[derive(Debug, Clone)]
17 /// There's a little bit of different behavior for resetting idle timeout. See
18 /// -transport 10.2 ("Idle Timeout").
19 enum IdleTimeoutState {
20 Init,
21 PacketReceived(Instant),
22 AckElicitingPacketSent(Instant),
23 }
24
25 #[derive(Debug, Clone)]
26 /// There's a little bit of different behavior for resetting idle timeout. See
27 /// -transport 10.1 ("Idle Timeout").
28 ///
29 /// <https://datatracker.ietf.org/doc/html/rfc9000#section-10.1>
30 pub struct IdleTimeout {
31 timeout: Duration,
32 state: IdleTimeoutState,
33 keep_alive_outstanding: bool,
34 }
35
36 impl IdleTimeout {
37 pub const fn new(timeout: Duration) -> Self {
38 Self {
39 timeout,
40 state: IdleTimeoutState::Init,
41 keep_alive_outstanding: false,
42 }
43 }
44 pub fn set_peer_timeout(&mut self, peer_timeout: Duration) {
45 self.timeout = min(self.timeout, peer_timeout);
46 }
47
48 const fn start(&self, now: Instant) -> Instant {
49 match self.state {
50 IdleTimeoutState::Init => now,
51 IdleTimeoutState::PacketReceived(t) | IdleTimeoutState::AckElicitingPacketSent(t) => t,
52 }
53 }
54
55 pub fn expiry(&self, now: Instant, pto: Duration) -> Instant {
56 let delay = max(self.timeout, pto * 3);
57 let t = self.start(now) + delay;
58 qtrace!("IdleTimeout::expiry@{now:?} pto={pto:?} => {t:?}");
59 t
60 }
61
62 pub fn on_packet_sent(&mut self, now: Instant) {
63 // Only reset idle timeout if we've received a packet since the last
64 // time we reset the timeout here.
65 match self.state {
66 // > An endpoint also restarts its idle timer when sending an
67 // > ack-eliciting packet if no other ack-eliciting packets have
68 // > been sent since last receiving and processing a packet.
69 //
70 // <https://datatracker.ietf.org/doc/html/rfc9000#section-10.1>
71 //
72 // Conversely, given that a packet has been sent since last
73 // receival, don't reset idle timer.
74 IdleTimeoutState::AckElicitingPacketSent(_) => {}
75 IdleTimeoutState::Init | IdleTimeoutState::PacketReceived(_) => {
76 self.state = IdleTimeoutState::AckElicitingPacketSent(now);
77 }
78 }
79 }
80
81 pub fn on_packet_received(&mut self, now: Instant) {
82 // Only update if this doesn't rewind the idle timeout.
83 // We sometimes process packets after caching them, which uses
84 // the time the packet was received. That could be in the past.
85 let update = match self.state {
86 IdleTimeoutState::Init => true,
87 IdleTimeoutState::AckElicitingPacketSent(t) | IdleTimeoutState::PacketReceived(t) => {
88 t <= now
89 }
90 };
91 if update {
92 self.state = IdleTimeoutState::PacketReceived(now);
93 }
94 }
95
96 pub fn expired(&self, now: Instant, pto: Duration) -> bool {
97 now >= self.expiry(now, pto)
98 }
99
100 fn keep_alive_timeout(&self, now: Instant, pto: Duration) -> Instant {
101 // For a keep-alive timer, wait for half the timeout interval, but be sure
102 // not to wait too little or we will send many unnecessary probes.
103 self.start(now) + max(self.timeout / 2, pto)
104 }
105
106 pub fn next_keep_alive(&self, now: Instant, pto: Duration) -> Option<Instant> {
107 if self.keep_alive_outstanding {
108 return None;
109 }
110
111 let timeout = self.keep_alive_timeout(now, pto);
112 // Timer is in the past, i.e. we should have sent a keep alive,
113 // but we were unable to, e.g. due to CC.
114 if timeout <= now {
115 return None;
116 }
117
118 Some(timeout)
119 }
120
121 pub fn send_keep_alive(
122 &mut self,
123 now: Instant,
124 pto: Duration,
125 tokens: &mut Vec<RecoveryToken>,
126 ) -> bool {
127 if !self.keep_alive_outstanding && now >= self.keep_alive_timeout(now, pto) {
128 self.keep_alive_outstanding = true;
129 tokens.push(RecoveryToken::KeepAlive);
130 true
131 } else {
132 false
133 }
134 }
135
136 pub fn lost_keep_alive(&mut self) {
137 self.keep_alive_outstanding = false;
138 }
139
140 pub fn ack_keep_alive(&mut self) {
141 self.keep_alive_outstanding = false;
142 }
143 }