Skip to content

Commit 5b2f56b

Browse files
committed
bwlimit: add burst option
1 parent 5486081 commit 5b2f56b

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

forward/bwlimit.go

Lines changed: 2 additions & 2 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

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)