Source file src/vendor/golang.org/x/net/quic/rtt.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package quic
     6  
     7  import (
     8  	"time"
     9  )
    10  
    11  type rttState struct {
    12  	minRTT          time.Duration
    13  	latestRTT       time.Duration
    14  	smoothedRTT     time.Duration
    15  	rttvar          time.Duration // RTT variation
    16  	firstSampleTime time.Time     // time of first RTT sample
    17  }
    18  
    19  func (r *rttState) init() {
    20  	r.minRTT = -1 // -1 indicates the first sample has not been taken yet
    21  
    22  	// "[...] the initial RTT SHOULD be set to 333 milliseconds."
    23  	// https://www.rfc-editor.org/rfc/rfc9002.html#section-6.2.2-1
    24  	const initialRTT = 333 * time.Millisecond
    25  
    26  	// https://www.rfc-editor.org/rfc/rfc9002.html#section-5.3-12
    27  	r.smoothedRTT = initialRTT
    28  	r.rttvar = initialRTT / 2
    29  }
    30  
    31  func (r *rttState) establishPersistentCongestion() {
    32  	// "Endpoints SHOULD set the min_rtt to the newest RTT sample
    33  	// after persistent congestion is established."
    34  	// https://www.rfc-editor.org/rfc/rfc9002#section-5.2-5
    35  	r.minRTT = r.latestRTT
    36  }
    37  
    38  // updateSample is called when we generate a new RTT sample.
    39  // https://www.rfc-editor.org/rfc/rfc9002.html#section-5
    40  func (r *rttState) updateSample(now time.Time, handshakeConfirmed bool, spaceID numberSpace, latestRTT, ackDelay, maxAckDelay time.Duration) {
    41  	r.latestRTT = latestRTT
    42  
    43  	if r.minRTT < 0 {
    44  		// First RTT sample.
    45  		// "min_rtt MUST be set to the latest_rtt on the first RTT sample."
    46  		// https://www.rfc-editor.org/rfc/rfc9002.html#section-5.2-2
    47  		r.minRTT = latestRTT
    48  		// https://www.rfc-editor.org/rfc/rfc9002.html#section-5.3-14
    49  		r.smoothedRTT = latestRTT
    50  		r.rttvar = latestRTT / 2
    51  		r.firstSampleTime = now
    52  		return
    53  	}
    54  
    55  	// "min_rtt MUST be set to the lesser of min_rtt and latest_rtt [...]
    56  	// on all other samples."
    57  	// https://www.rfc-editor.org/rfc/rfc9002.html#section-5.2-2
    58  	r.minRTT = min(r.minRTT, latestRTT)
    59  
    60  	// https://www.rfc-editor.org/rfc/rfc9002.html#section-5.3-16
    61  	if handshakeConfirmed {
    62  		ackDelay = min(ackDelay, maxAckDelay)
    63  	}
    64  	adjustedRTT := latestRTT - ackDelay
    65  	if adjustedRTT < r.minRTT {
    66  		adjustedRTT = latestRTT
    67  	}
    68  	rttvarSample := abs(r.smoothedRTT - adjustedRTT)
    69  	r.rttvar = (3*r.rttvar + rttvarSample) / 4
    70  	r.smoothedRTT = ((7 * r.smoothedRTT) + adjustedRTT) / 8
    71  }
    72  

View as plain text