This repository has been archived by the owner on Mar 17, 2024. It is now read-only.
/
brutal_manual.go
108 lines (86 loc) · 2.68 KB
/
brutal_manual.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package quic
import (
"time"
"github.com/lucas-clemente/quic-go/congestion"
)
var TheCustomRate = 0.75
type BrutalSender_M struct {
rttStats congestion.RTTStatsProvider
bps congestion.ByteCount
maxDatagramSize congestion.ByteCount
pacer *pacer
}
func NewBrutalSender_M(bps congestion.ByteCount) *BrutalSender_M {
bs := &BrutalSender_M{
bps: bps,
maxDatagramSize: initMaxDatagramSize,
}
bs.pacer = newPacer(func() congestion.ByteCount {
return congestion.ByteCount(float64(bs.bps) / bs.getAckRate())
})
return bs
}
func (b *BrutalSender_M) SetRTTStatsProvider(rttStats congestion.RTTStatsProvider) {
b.rttStats = rttStats
}
func (b *BrutalSender_M) TimeUntilSend(bytesInFlight congestion.ByteCount) time.Time {
return b.pacer.TimeUntilSend()
}
func (b *BrutalSender_M) HasPacingBudget() bool {
return b.pacer.Budget(time.Now()) >= b.maxDatagramSize
}
func (b *BrutalSender_M) CanSend(bytesInFlight congestion.ByteCount) bool {
return bytesInFlight < b.GetCongestionWindow()
}
func (b *BrutalSender_M) GetCongestionWindow() congestion.ByteCount {
rtt := maxDuration(b.rttStats.LatestRTT(), b.rttStats.SmoothedRTT())
if rtt <= 0 {
return 10240
}
return congestion.ByteCount(float64(b.bps) * rtt.Seconds() * 1.5 / b.getAckRate())
}
func (b *BrutalSender_M) OnPacketSent(sentTime time.Time, bytesInFlight congestion.ByteCount,
packetNumber congestion.PacketNumber, bytes congestion.ByteCount, isRetransmittable bool) {
b.pacer.SentPacket(sentTime, bytes)
}
func (b *BrutalSender_M) OnPacketAcked(number congestion.PacketNumber, ackedBytes congestion.ByteCount, priorInFlight congestion.ByteCount, eventTime time.Time) {
}
func (b *BrutalSender_M) OnPacketLost(number congestion.PacketNumber, lostBytes congestion.ByteCount, priorInFlight congestion.ByteCount) {
}
func (b *BrutalSender_M) SetMaxDatagramSize(size congestion.ByteCount) {
b.maxDatagramSize = size
b.pacer.SetMaxDatagramSize(size)
}
func rateOk(r float64) int {
if r < 0.2 {
return -1
}
if r > 1.5 {
return 1
}
return 0
}
//原来最小值是0.75, 最大值是1,越小的话发包越疯狂.
// 我们改成最小值0.2, 最快可以 7.5倍发包
// 最大值改成 1.5, 这样最慢可以1倍速正常发包
func (b *BrutalSender_M) getAckRate() float64 {
r := rateOk(TheCustomRate)
switch r {
case 0:
return TheCustomRate
case -1:
return 0.2
case 1:
return 1.5
default:
panic("rateOk returned value not 0,-1,1")
}
}
func (b *BrutalSender_M) InSlowStart() bool {
return false
}
func (b *BrutalSender_M) InRecovery() bool {
return false
}
func (b *BrutalSender_M) MaybeExitSlowStart() {}
func (b *BrutalSender_M) OnRetransmissionTimeout(packetsRetransmitted bool) {}