mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-19 08:20:05 +08:00
Some checks failed
Test / test (1.20, macos-13) (push) Has been cancelled
Test / test (1.20, macos-latest) (push) Has been cancelled
Test / test (1.20, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.20, ubuntu-latest) (push) Has been cancelled
Test / test (1.20, windows-latest) (push) Has been cancelled
Test / test (1.21, macos-13) (push) Has been cancelled
Test / test (1.21, macos-latest) (push) Has been cancelled
Test / test (1.21, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.21, ubuntu-latest) (push) Has been cancelled
Test / test (1.21, windows-latest) (push) Has been cancelled
Test / test (1.22, macos-13) (push) Has been cancelled
Test / test (1.22, macos-latest) (push) Has been cancelled
Test / test (1.22, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.22, ubuntu-latest) (push) Has been cancelled
Test / test (1.22, windows-latest) (push) Has been cancelled
Test / test (1.23, macos-13) (push) Has been cancelled
Test / test (1.23, macos-latest) (push) Has been cancelled
Test / test (1.23, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.23, ubuntu-latest) (push) Has been cancelled
Test / test (1.23, windows-latest) (push) Has been cancelled
Test / test (1.24, macos-13) (push) Has been cancelled
Test / test (1.24, macos-latest) (push) Has been cancelled
Test / test (1.24, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.24, ubuntu-latest) (push) Has been cancelled
Test / test (1.24, windows-latest) (push) Has been cancelled
Test / test (1.25, macos-13) (push) Has been cancelled
Test / test (1.25, macos-latest) (push) Has been cancelled
Test / test (1.25, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.25, ubuntu-latest) (push) Has been cancelled
Test / test (1.25, windows-latest) (push) Has been cancelled
Trigger CMFA Update / trigger-CMFA-update (push) Has been cancelled
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package congestion
|
|
|
|
import (
|
|
"math"
|
|
"time"
|
|
|
|
"github.com/metacubex/quic-go/congestion"
|
|
"github.com/metacubex/quic-go/monotime"
|
|
)
|
|
|
|
const (
|
|
maxBurstPackets = 10
|
|
)
|
|
|
|
// Pacer implements a token bucket pacing algorithm.
|
|
type Pacer struct {
|
|
budgetAtLastSent congestion.ByteCount
|
|
maxDatagramSize congestion.ByteCount
|
|
lastSentTime monotime.Time
|
|
getBandwidth func() congestion.ByteCount // in bytes/s
|
|
}
|
|
|
|
func NewPacer(getBandwidth func() congestion.ByteCount) *Pacer {
|
|
p := &Pacer{
|
|
budgetAtLastSent: maxBurstPackets * congestion.InitialPacketSize,
|
|
maxDatagramSize: congestion.InitialPacketSize,
|
|
getBandwidth: getBandwidth,
|
|
}
|
|
return p
|
|
}
|
|
|
|
func (p *Pacer) SentPacket(sendTime monotime.Time, size congestion.ByteCount) {
|
|
budget := p.Budget(sendTime)
|
|
if size > budget {
|
|
p.budgetAtLastSent = 0
|
|
} else {
|
|
p.budgetAtLastSent = budget - size
|
|
}
|
|
p.lastSentTime = sendTime
|
|
}
|
|
|
|
func (p *Pacer) Budget(now monotime.Time) congestion.ByteCount {
|
|
if p.lastSentTime.IsZero() {
|
|
return p.maxBurstSize()
|
|
}
|
|
budget := p.budgetAtLastSent + (p.getBandwidth()*congestion.ByteCount(now.Sub(p.lastSentTime).Nanoseconds()))/1e9
|
|
if budget < 0 { // protect against overflows
|
|
budget = congestion.ByteCount(1<<62 - 1)
|
|
}
|
|
return Min(p.maxBurstSize(), budget)
|
|
}
|
|
|
|
func (p *Pacer) maxBurstSize() congestion.ByteCount {
|
|
return Max(
|
|
congestion.ByteCount((congestion.MinPacingDelay+time.Millisecond).Nanoseconds())*p.getBandwidth()/1e9,
|
|
maxBurstPackets*p.maxDatagramSize,
|
|
)
|
|
}
|
|
|
|
// TimeUntilSend returns when the next packet should be sent.
|
|
// It returns the zero value of monotime.Time if a packet can be sent immediately.
|
|
func (p *Pacer) TimeUntilSend() monotime.Time {
|
|
if p.budgetAtLastSent >= p.maxDatagramSize {
|
|
return monotime.Time(0)
|
|
}
|
|
return p.lastSentTime.Add(Max(
|
|
congestion.MinPacingDelay,
|
|
time.Duration(math.Ceil(float64(p.maxDatagramSize-p.budgetAtLastSent)*1e9/
|
|
float64(p.getBandwidth())))*time.Nanosecond,
|
|
))
|
|
}
|
|
|
|
func (p *Pacer) SetMaxDatagramSize(s congestion.ByteCount) {
|
|
p.maxDatagramSize = s
|
|
}
|