@@ -50,21 +50,25 @@ pub struct RttEstimator {
5050}
5151
5252impl Default for RttEstimator {
53+ /// Creates a new RTT Estimator with default initial values
5354 fn default ( ) -> Self {
54- RttEstimator :: new ( Duration :: ZERO )
55+ RttEstimator :: new ( DEFAULT_INITIAL_RTT )
5556 }
5657}
5758
5859impl RttEstimator {
59- /// Creates a new RTT Estimator with default initial values using the given `max_ack_delay`.
60+ /// Creates a new RTT Estimator with the given `initial_rtt`
61+ ///
62+ /// `on_max_ack_delay` must be called when the `max_ack_delay` transport
63+ /// parameter is received to initialize the `max_ack_delay` value.
6064 #[ inline]
61- pub fn new ( max_ack_delay : Duration ) -> Self {
62- Self :: new_with_initial ( max_ack_delay , DEFAULT_INITIAL_RTT )
65+ pub fn new ( initial_rtt : Duration ) -> Self {
66+ Self :: new_with_max_ack_delay ( Duration :: ZERO , initial_rtt )
6367 }
6468
6569 /// Creates a new RTT Estimator with the provided initial values using the given `max_ack_delay`.
6670 #[ inline]
67- pub fn new_with_initial ( max_ack_delay : Duration , initial_rtt : Duration ) -> Self {
71+ fn new_with_max_ack_delay ( max_ack_delay : Duration , initial_rtt : Duration ) -> Self {
6872 debug_assert ! ( initial_rtt >= MIN_RTT ) ;
6973 let initial_rtt = initial_rtt. max ( MIN_RTT ) ;
7074
@@ -91,6 +95,11 @@ impl RttEstimator {
9195 }
9296 }
9397
98+ /// Creates a new RTT Estimator with the `max_ack_delay` from the current instance
99+ pub fn for_new_path ( & self , initial_rtt : Duration ) -> Self {
100+ Self :: new_with_max_ack_delay ( self . max_ack_delay , initial_rtt)
101+ }
102+
94103 /// Gets the latest round trip time sample
95104 #[ inline]
96105 pub fn latest_rtt ( & self ) -> Duration {
@@ -385,7 +394,8 @@ mod test {
385394 /// Test the initial values before any RTT samples
386395 #[ test]
387396 fn initial_rtt_across_spaces ( ) {
388- let rtt_estimator = RttEstimator :: new ( Duration :: from_millis ( 10 ) ) ;
397+ let rtt_estimator =
398+ RttEstimator :: new_with_max_ack_delay ( Duration :: from_millis ( 10 ) , DEFAULT_INITIAL_RTT ) ;
389399 assert_eq ! ( rtt_estimator. min_rtt, DEFAULT_INITIAL_RTT ) ;
390400 assert_eq ! ( rtt_estimator. latest_rtt( ) , DEFAULT_INITIAL_RTT ) ;
391401 assert_eq ! ( rtt_estimator. smoothed_rtt( ) , DEFAULT_INITIAL_RTT ) ;
@@ -407,7 +417,7 @@ mod test {
407417 /// Test a zero RTT value is treated as 1 µs
408418 #[ test]
409419 fn zero_rtt_sample ( ) {
410- let mut rtt_estimator = RttEstimator :: new ( Duration :: from_millis ( 10 ) ) ;
420+ let mut rtt_estimator = RttEstimator :: new ( DEFAULT_INITIAL_RTT ) ;
411421 let now = NoopClock . get_time ( ) ;
412422 rtt_estimator. update_rtt (
413423 Duration :: from_millis ( 10 ) ,
@@ -425,17 +435,26 @@ mod test {
425435 ) ;
426436 }
427437
438+ #[ test]
439+ fn for_new_path ( ) {
440+ let mut rtt_estimator = RttEstimator :: default ( ) ;
441+ let max_ack_delay = Duration :: from_millis ( 10 ) ;
442+ rtt_estimator. on_max_ack_delay ( max_ack_delay. try_into ( ) . unwrap ( ) ) ;
443+ let new_path_rtt_estimator = rtt_estimator. for_new_path ( DEFAULT_INITIAL_RTT ) ;
444+ assert_eq ! ( max_ack_delay, new_path_rtt_estimator. max_ack_delay)
445+ }
446+
428447 //= https://www.rfc-editor.org/rfc/rfc9002#section-5.3
429448 //= type=test
430449 //# * MUST use the lesser of the acknowledgement delay and the peer's
431450 //# max_ack_delay after the handshake is confirmed;
432451 #[ test]
433452 fn max_ack_delay ( ) {
434453 let mut rtt_estimator = RttEstimator :: default ( ) ;
435- assert_eq ! ( Duration :: ZERO , rtt_estimator. max_ack_delay( ) ) ;
454+ assert_eq ! ( Duration :: ZERO , rtt_estimator. max_ack_delay) ;
436455
437456 rtt_estimator. on_max_ack_delay ( MaxAckDelay :: new ( VarInt :: from_u8 ( 10 ) ) . unwrap ( ) ) ;
438- assert_eq ! ( Duration :: from_millis( 10 ) , rtt_estimator. max_ack_delay( ) ) ;
457+ assert_eq ! ( Duration :: from_millis( 10 ) , rtt_estimator. max_ack_delay) ;
439458
440459 let now = NoopClock . get_time ( ) ;
441460 rtt_estimator. update_rtt (
@@ -495,7 +514,8 @@ mod test {
495514 /// Test several rounds of RTT updates
496515 #[ test]
497516 fn update_rtt ( ) {
498- let mut rtt_estimator = RttEstimator :: new ( Duration :: from_millis ( 10 ) ) ;
517+ let mut rtt_estimator =
518+ RttEstimator :: new_with_max_ack_delay ( Duration :: from_millis ( 10 ) , DEFAULT_INITIAL_RTT ) ;
499519 let now = NoopClock . get_time ( ) ;
500520 let rtt_sample = Duration :: from_millis ( 500 ) ;
501521 assert ! ( rtt_estimator. first_rtt_sample. is_none( ) ) ;
@@ -574,7 +594,8 @@ mod test {
574594 //# the resulting value is smaller than the min_rtt.
575595 #[ test]
576596 fn must_not_subtract_acknowledgement_delay_if_result_smaller_than_min_rtt ( ) {
577- let mut rtt_estimator = RttEstimator :: new ( Duration :: from_millis ( 200 ) ) ;
597+ let mut rtt_estimator =
598+ RttEstimator :: new_with_max_ack_delay ( Duration :: from_millis ( 200 ) , DEFAULT_INITIAL_RTT ) ;
578599 let now = NoopClock . get_time ( ) ;
579600
580601 rtt_estimator. min_rtt = Duration :: from_millis ( 500 ) ;
@@ -606,7 +627,8 @@ mod test {
606627 //# the min_rtt.
607628 #[ test]
608629 fn prior_to_handshake_ignore_if_less_than_min_rtt ( ) {
609- let mut rtt_estimator = RttEstimator :: new ( Duration :: from_millis ( 200 ) ) ;
630+ let mut rtt_estimator =
631+ RttEstimator :: new_with_max_ack_delay ( Duration :: from_millis ( 200 ) , DEFAULT_INITIAL_RTT ) ;
610632 let now = NoopClock . get_time ( ) ;
611633 let smoothed_rtt = Duration :: from_millis ( 700 ) ;
612634
@@ -634,7 +656,8 @@ mod test {
634656 // of [QUIC-TRANSPORT]);
635657 #[ test]
636658 fn initial_space ( ) {
637- let mut rtt_estimator = RttEstimator :: new ( Duration :: from_millis ( 10 ) ) ;
659+ let mut rtt_estimator =
660+ RttEstimator :: new_with_max_ack_delay ( Duration :: from_millis ( 10 ) , DEFAULT_INITIAL_RTT ) ;
638661 let now = NoopClock . get_time ( ) ;
639662 let rtt_sample = Duration :: from_millis ( 500 ) ;
640663 rtt_estimator. update_rtt (
@@ -671,7 +694,8 @@ mod test {
671694 #[ test]
672695 fn persistent_congestion_duration ( ) {
673696 let max_ack_delay = Duration :: from_millis ( 10 ) ;
674- let mut rtt_estimator = RttEstimator :: new ( max_ack_delay) ;
697+ let mut rtt_estimator =
698+ RttEstimator :: new_with_max_ack_delay ( max_ack_delay, DEFAULT_INITIAL_RTT ) ;
675699
676700 rtt_estimator. smoothed_rtt = Duration :: from_millis ( 100 ) ;
677701 rtt_estimator. rttvar = Duration :: from_millis ( 50 ) ;
@@ -703,7 +727,8 @@ mod test {
703727
704728 #[ test]
705729 fn set_min_rtt_to_latest_sample_after_persistent_congestion ( ) {
706- let mut rtt_estimator = RttEstimator :: new ( Duration :: from_millis ( 10 ) ) ;
730+ let mut rtt_estimator =
731+ RttEstimator :: new_with_max_ack_delay ( Duration :: from_millis ( 10 ) , DEFAULT_INITIAL_RTT ) ;
707732 let now = NoopClock . get_time ( ) ;
708733 let mut rtt_sample = Duration :: from_millis ( 500 ) ;
709734 rtt_estimator. update_rtt (
@@ -743,9 +768,8 @@ mod test {
743768 #[ test]
744769 fn pto_must_be_at_least_k_granularity ( ) {
745770 let space = PacketNumberSpace :: Handshake ;
746- let max_ack_delay = Duration :: from_millis ( 0 ) ;
747771 let now = NoopClock . get_time ( ) ;
748- let mut rtt_estimator = RttEstimator :: new ( max_ack_delay ) ;
772+ let mut rtt_estimator = RttEstimator :: new ( DEFAULT_INITIAL_RTT ) ;
749773
750774 // Update RTT with the smallest possible sample
751775 rtt_estimator. update_rtt (
@@ -795,7 +819,8 @@ mod test {
795819 //# RTT multiplier, is 9/8.
796820 #[ test]
797821 fn time_threshold_multiplier_equals_nine_eighths ( ) {
798- let mut rtt_estimator = RttEstimator :: new ( Duration :: from_millis ( 10 ) ) ;
822+ let mut rtt_estimator =
823+ RttEstimator :: new_with_max_ack_delay ( Duration :: from_millis ( 10 ) , DEFAULT_INITIAL_RTT ) ;
799824 rtt_estimator. update_rtt (
800825 Duration :: from_millis ( 10 ) ,
801826 Duration :: from_secs ( 1 ) ,
0 commit comments