Skip to content

Commit 5034486

Browse files
authored
Merge pull request #121 from SenseUnit/int64_rl
RL burst option
2 parents 70858d3 + 5b2f56b commit 5034486

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

forward/bwlimit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ type BWLimit struct {
1717
u []rate.Limiter
1818
}
1919

20-
func NewBWLimit(bytesPerSecond float64, buckets uint, separate bool) *BWLimit {
20+
func NewBWLimit(bytesPerSecond float64, burst int64, buckets uint, separate bool) *BWLimit {
2121
if buckets == 0 {
2222
buckets = 1
2323
}
24-
lim := *(rate.NewLimiter(rate.Limit(bytesPerSecond), copyChunkSize))
24+
lim := *(rate.NewLimiter(rate.Limit(bytesPerSecond), max(copyChunkSize, burst)))
2525
d := make([]rate.Limiter, buckets)
2626
for i := range d {
2727
d[i] = lim
@@ -66,7 +66,7 @@ func (l *BWLimit) copy(ctx context.Context, rl *rate.Limiter, dst io.Writer, src
6666
if n < copyChunkSize {
6767
r.CancelAt(t)
6868
if n > 0 {
69-
rl.ReserveN(t, int(n))
69+
rl.ReserveN(t, n)
7070
}
7171
}
7272
if err != nil {

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ require (
3030
github.com/hashicorp/errwrap v1.1.0 // indirect
3131
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
3232
github.com/pires/go-proxyproto v0.8.0
33-
golang.org/x/sync v0.13.0 // indirect
33+
golang.org/x/sync v0.13.0
3434
golang.org/x/sys v0.32.0 // indirect
3535
golang.org/x/term v0.31.0 // indirect
3636
golang.org/x/text v0.24.0 // indirect
3737
)
38+
39+
replace golang.org/x/time => github.com/Snawoot/xtime v0.0.0-20250501122004-d1ce456948bb

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ github.com/GehirnInc/crypt v0.0.0-20230320061759-8cc1b52080c5 h1:IEjq88XO4PuBDcv
22
github.com/GehirnInc/crypt v0.0.0-20230320061759-8cc1b52080c5/go.mod h1:exZ0C/1emQJAw5tHOaUDyY1ycttqBAPcxuzf7QbY6ec=
33
github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0=
44
github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ=
5+
github.com/Snawoot/xtime v0.0.0-20250501122004-d1ce456948bb h1:PleTDwc/EQenzLsvIal2BgvIXr2D214M88RFac3WkeI=
6+
github.com/Snawoot/xtime v0.0.0-20250501122004-d1ce456948bb/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
57
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
68
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
79
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
@@ -64,8 +66,6 @@ golang.org/x/term v0.31.0 h1:erwDkOK1Msy6offm1mOgvspSkslFnIGsFnxOKoufg3o=
6466
golang.org/x/term v0.31.0/go.mod h1:R4BeIy7D95HzImkxGkTW1UQTtP54tio2RyHz7PwK0aw=
6567
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
6668
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
67-
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
68-
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
6969
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
7070
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
7171
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ type CLIArgs struct {
206206
minTLSVersion TLSVersionArg
207207
maxTLSVersion TLSVersionArg
208208
bwLimit uint64
209+
bwBurst int64
209210
bwBuckets uint
210211
bwSeparate bool
211212
dnsCacheTTL time.Duration
@@ -300,6 +301,7 @@ func parse_args() CLIArgs {
300301
flag.Var(&args.minTLSVersion, "min-tls-version", "minimum TLS version accepted by server")
301302
flag.Var(&args.maxTLSVersion, "max-tls-version", "maximum TLS version accepted by server")
302303
flag.Uint64Var(&args.bwLimit, "bw-limit", 0, "per-user bandwidth limit in bytes per second")
304+
flag.Int64Var(&args.bwBurst, "bw-limit-burst", 0, "allowed burst size for bandwidth limit, how many \"tokens\" can fit into leaky bucket")
303305
flag.UintVar(&args.bwBuckets, "bw-limit-buckets", 1024*1024, "number of buckets of bandwidth limit")
304306
flag.BoolVar(&args.bwSeparate, "bw-limit-separate", false, "separate upload and download bandwidth limits")
305307
flag.DurationVar(&args.dnsCacheTTL, "dns-cache-ttl", 0, "enable DNS cache with specified fixed TTL")
@@ -467,6 +469,7 @@ func run() int {
467469
if args.bwLimit != 0 {
468470
forwarder = forward.NewBWLimit(
469471
float64(args.bwLimit),
472+
args.bwBurst,
470473
args.bwBuckets,
471474
args.bwSeparate,
472475
).PairConnections

0 commit comments

Comments
 (0)