|
| 1 | +// Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// Ported from: |
| 6 | +// https://github.com/google/quiche/blob/main/quiche/quic/core/quic_bandwidth.h |
| 7 | + |
| 8 | +package congestion_bbr1 |
| 9 | + |
| 10 | +import ( |
| 11 | + "math" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/sagernet/quic-go/congestion" |
| 15 | +) |
| 16 | + |
| 17 | +// Bandwidth represents a bandwidth, stored in bits per second resolution. |
| 18 | +type Bandwidth int64 |
| 19 | + |
| 20 | +const ( |
| 21 | + // BitsPerSecond is 1 bit per second. |
| 22 | + BitsPerSecond Bandwidth = 1 |
| 23 | + // BytesPerSecond is 8 bits per second. |
| 24 | + BytesPerSecond = 8 * BitsPerSecond |
| 25 | + // KBitsPerSecond is 1000 bits per second. |
| 26 | + KBitsPerSecond = 1000 * BitsPerSecond |
| 27 | + // KBytesPerSecond is 8000 bits per second. |
| 28 | + KBytesPerSecond = 8000 * BitsPerSecond |
| 29 | +) |
| 30 | + |
| 31 | +const ( |
| 32 | + infiniteBandwidth = Bandwidth(math.MaxInt64) |
| 33 | +) |
| 34 | + |
| 35 | +// BandwidthFromBytesAndTimeDelta creates a new Bandwidth based on the bytes per the elapsed delta. |
| 36 | +func BandwidthFromBytesAndTimeDelta(bytes congestion.ByteCount, delta time.Duration) Bandwidth { |
| 37 | + if bytes == 0 { |
| 38 | + return 0 |
| 39 | + } |
| 40 | + if delta <= 0 { |
| 41 | + return infiniteBandwidth |
| 42 | + } |
| 43 | + // 1 bit is 1000000 micro bits. |
| 44 | + numMicroBits := int64(bytes) * 8 * int64(time.Second/time.Microsecond) |
| 45 | + deltaMicros := delta.Microseconds() |
| 46 | + if numMicroBits < deltaMicros { |
| 47 | + return 1 |
| 48 | + } |
| 49 | + return Bandwidth(numMicroBits / deltaMicros) |
| 50 | +} |
| 51 | + |
| 52 | +// Zero returns a zero bandwidth. |
| 53 | +func (b Bandwidth) Zero() Bandwidth { |
| 54 | + return 0 |
| 55 | +} |
| 56 | + |
| 57 | +// IsZero returns true if the bandwidth is zero. |
| 58 | +func (b Bandwidth) IsZero() bool { |
| 59 | + return b == 0 |
| 60 | +} |
| 61 | + |
| 62 | +// IsInfinite returns true if the bandwidth is infinite. |
| 63 | +func (b Bandwidth) IsInfinite() bool { |
| 64 | + return b == infiniteBandwidth |
| 65 | +} |
| 66 | + |
| 67 | +// ToBitsPerSecond returns the bandwidth in bits per second. |
| 68 | +func (b Bandwidth) ToBitsPerSecond() int64 { |
| 69 | + return int64(b) |
| 70 | +} |
| 71 | + |
| 72 | +// ToBytesPerSecond returns the bandwidth in bytes per second. |
| 73 | +func (b Bandwidth) ToBytesPerSecond() int64 { |
| 74 | + return int64(b) / 8 |
| 75 | +} |
| 76 | + |
| 77 | +// ToKBitsPerSecond returns the bandwidth in kilo bits per second. |
| 78 | +func (b Bandwidth) ToKBitsPerSecond() int64 { |
| 79 | + return int64(b) / 1000 |
| 80 | +} |
| 81 | + |
| 82 | +// ToKBytesPerSecond returns the bandwidth in kilo bytes per second. |
| 83 | +func (b Bandwidth) ToKBytesPerSecond() int64 { |
| 84 | + return int64(b) / 8000 |
| 85 | +} |
| 86 | + |
| 87 | +// ToBytesPerPeriod returns the number of bytes that can be transmitted in the given time period. |
| 88 | +func (b Bandwidth) ToBytesPerPeriod(timePeriod time.Duration) congestion.ByteCount { |
| 89 | + return congestion.ByteCount(int64(b) * timePeriod.Microseconds() / 8 / int64(time.Second/time.Microsecond)) |
| 90 | +} |
| 91 | + |
| 92 | +// TransferTime returns the time it takes to transfer the given number of bytes. |
| 93 | +func (b Bandwidth) TransferTime(bytes congestion.ByteCount) time.Duration { |
| 94 | + if b == 0 { |
| 95 | + return 0 |
| 96 | + } |
| 97 | + return time.Duration(int64(bytes) * 8 * int64(time.Second/time.Microsecond) / int64(b) * int64(time.Microsecond)) |
| 98 | +} |
| 99 | + |
| 100 | +// Infinite returns an infinite bandwidth value. |
| 101 | +func InfiniteBandwidth() Bandwidth { |
| 102 | + return infiniteBandwidth |
| 103 | +} |
0 commit comments