From a7ec34884ab1de9b1a07e7ed2b9ed4c835c003b2 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 11 Jul 2025 00:13:20 +1000 Subject: [PATCH] Set default webseed download rate burst --- config.go | 18 +++++++----------- rate.go | 18 ++++++++++++++++++ torrent.go | 1 + 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 rate.go diff --git a/config.go b/config.go index 55b72ade..05da2bf4 100644 --- a/config.go +++ b/config.go @@ -260,15 +260,11 @@ type HeaderObfuscationPolicy struct { } func (cfg *ClientConfig) setRateLimiterBursts() { - // Create a helper for rate limiters to avoid mistakes? What if the limit is greater than what - // can be represented by int? - if cfg.UploadRateLimiter.Limit() != rate.Inf && cfg.UploadRateLimiter.Burst() == 0 { - // What about chunk size? - cfg.UploadRateLimiter.SetBurst(cfg.MaxAllocPeerRequestDataPerConn) - } - if cfg.DownloadRateLimiter.Limit() != rate.Inf && cfg.DownloadRateLimiter.Burst() == 0 { - // 64 KiB used to be a rough default buffer for sockets on Windows. I'm sure it's bigger - // these days. What about the read buffer size mentioned elsewhere? - cfg.DownloadRateLimiter.SetBurst(min(int(cfg.DownloadRateLimiter.Limit()), 1<<16)) - } + // What about chunk size? + setRateLimiterBurstIfZero(cfg.UploadRateLimiter, cfg.MaxAllocPeerRequestDataPerConn) + setRateLimiterBurstIfZero( + cfg.DownloadRateLimiter, + min( + int(cfg.DownloadRateLimiter.Limit()), + defaultDownloadRateLimiterBurst)) } diff --git a/rate.go b/rate.go new file mode 100644 index 00000000..b711cc91 --- /dev/null +++ b/rate.go @@ -0,0 +1,18 @@ +package torrent + +import ( + "golang.org/x/time/rate" +) + +// 64 KiB used to be a rough default buffer for sockets on Windows. I'm sure it's bigger +// these days. What about the read buffer size mentioned elsewhere? Note this is also used for +// webseeding since that shares the download rate limiter by default. +const defaultDownloadRateLimiterBurst = 1 << 16 + +// Sets rate limiter burst if it's set to zero which is used to request the default by our API. +func setRateLimiterBurstIfZero(l *rate.Limiter, def int) { + if l.Burst() == 0 && l.Limit() != rate.Inf { + // What if the limit is greater than what can be represented by int? + l.SetBurst(def) + } +} diff --git a/torrent.go b/torrent.go index 6ba0858a..02556bfc 100644 --- a/torrent.go +++ b/torrent.go @@ -3057,6 +3057,7 @@ func (t *Torrent) addWebSeed(url string, opts ...AddWebSeedsOpt) bool { for _, opt := range opts { opt(&ws.client) } + setRateLimiterBurstIfZero(ws.client.ResponseBodyRateLimiter, defaultDownloadRateLimiterBurst) ws.client.ResponseBodyWrapper = func(r io.Reader) io.Reader { return &rateLimitedReader{ l: ws.client.ResponseBodyRateLimiter, -- 2.51.0