golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/pacer.go (about)

     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  //go:build go1.21
     6  
     7  package quic
     8  
     9  import (
    10  	"time"
    11  )
    12  
    13  // A pacerState controls the rate at which packets are sent using a leaky-bucket rate limiter.
    14  //
    15  // The pacer limits the maximum size of a burst of packets.
    16  // When a burst exceeds this limit, it spreads subsequent packets
    17  // over time.
    18  //
    19  // The bucket is initialized to the maximum burst size (ten packets by default),
    20  // and fills at the rate:
    21  //
    22  //	1.25 * congestion_window / smoothed_rtt
    23  //
    24  // A sender can send one congestion window of packets per RTT,
    25  // since the congestion window consumed by each packet is returned
    26  // one round-trip later by the responding ack.
    27  // The pacer permits sending at slightly faster than this rate to
    28  // avoid underutilizing the congestion window.
    29  //
    30  // The pacer permits the bucket to become negative, and permits
    31  // sending when non-negative. This biases slightly in favor of
    32  // sending packets over limiting them, and permits bursts one
    33  // packet greater than the configured maximum, but permits the pacer
    34  // to be ignorant of the maximum packet size.
    35  //
    36  // https://www.rfc-editor.org/rfc/rfc9002.html#section-7.7
    37  type pacerState struct {
    38  	bucket           int // measured in bytes
    39  	maxBucket        int
    40  	timerGranularity time.Duration
    41  	lastUpdate       time.Time
    42  	nextSend         time.Time
    43  }
    44  
    45  func (p *pacerState) init(now time.Time, maxBurst int, timerGranularity time.Duration) {
    46  	// Bucket is limited to maximum burst size, which is the initial congestion window.
    47  	// https://www.rfc-editor.org/rfc/rfc9002#section-7.7-2
    48  	p.maxBucket = maxBurst
    49  	p.bucket = p.maxBucket
    50  	p.timerGranularity = timerGranularity
    51  	p.lastUpdate = now
    52  	p.nextSend = now
    53  }
    54  
    55  // pacerBytesForInterval returns the number of bytes permitted over an interval.
    56  //
    57  //	rate  = 1.25 * congestion_window / smoothed_rtt
    58  //	bytes = interval * rate
    59  //
    60  // https://www.rfc-editor.org/rfc/rfc9002#section-7.7-6
    61  func pacerBytesForInterval(interval time.Duration, congestionWindow int, rtt time.Duration) int {
    62  	bytes := (int64(interval) * int64(congestionWindow)) / int64(rtt)
    63  	bytes = (bytes * 5) / 4 // bytes *= 1.25
    64  	return int(bytes)
    65  }
    66  
    67  // pacerIntervalForBytes returns the amount of time required for a number of bytes.
    68  //
    69  //	time_per_byte = (smoothed_rtt / congestion_window) / 1.25
    70  //	interval      = time_per_byte * bytes
    71  //
    72  // https://www.rfc-editor.org/rfc/rfc9002#section-7.7-8
    73  func pacerIntervalForBytes(bytes int, congestionWindow int, rtt time.Duration) time.Duration {
    74  	interval := (int64(rtt) * int64(bytes)) / int64(congestionWindow)
    75  	interval = (interval * 4) / 5 // interval /= 1.25
    76  	return time.Duration(interval)
    77  }
    78  
    79  // advance is called when time passes.
    80  func (p *pacerState) advance(now time.Time, congestionWindow int, rtt time.Duration) {
    81  	elapsed := now.Sub(p.lastUpdate)
    82  	if elapsed < 0 {
    83  		// Time has gone backward?
    84  		elapsed = 0
    85  		p.nextSend = now // allow a packet through to get back on track
    86  		if p.bucket < 0 {
    87  			p.bucket = 0
    88  		}
    89  	}
    90  	p.lastUpdate = now
    91  	if rtt == 0 {
    92  		// Avoid divide by zero in the implausible case that we measure no RTT.
    93  		p.bucket = p.maxBucket
    94  		return
    95  	}
    96  	// Refill the bucket.
    97  	delta := pacerBytesForInterval(elapsed, congestionWindow, rtt)
    98  	p.bucket = min(p.bucket+delta, p.maxBucket)
    99  }
   100  
   101  // packetSent is called to record transmission of a packet.
   102  func (p *pacerState) packetSent(now time.Time, size, congestionWindow int, rtt time.Duration) {
   103  	p.bucket -= size
   104  	if p.bucket < -congestionWindow {
   105  		// Never allow the bucket to fall more than one congestion window in arrears.
   106  		// We can only fall this far behind if the sender is sending unpaced packets,
   107  		// the congestion window has been exceeded, or the RTT is less than the
   108  		// timer granularity.
   109  		//
   110  		// Limiting the minimum bucket size limits the maximum pacer delay
   111  		// to RTT/1.25.
   112  		p.bucket = -congestionWindow
   113  	}
   114  	if p.bucket >= 0 {
   115  		p.nextSend = now
   116  		return
   117  	}
   118  	// Next send occurs when the bucket has refilled to 0.
   119  	delay := pacerIntervalForBytes(-p.bucket, congestionWindow, rtt)
   120  	p.nextSend = now.Add(delay)
   121  }
   122  
   123  // canSend reports whether a packet can be sent now.
   124  // If it returns false, next is the time when the next packet can be sent.
   125  func (p *pacerState) canSend(now time.Time) (canSend bool, next time.Time) {
   126  	// If the next send time is within the timer granularity, send immediately.
   127  	if p.nextSend.After(now.Add(p.timerGranularity)) {
   128  		return false, p.nextSend
   129  	}
   130  	return true, time.Time{}
   131  }