]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Set default webseed download rate burst
authorMatt Joiner <anacrolix@gmail.com>
Thu, 10 Jul 2025 14:13:20 +0000 (00:13 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 10 Jul 2025 14:13:20 +0000 (00:13 +1000)
config.go
rate.go [new file with mode: 0644]
torrent.go

index 55b72ade5d612b97133c207efe1d906fd515e727..05da2bf4d9a652ac9073742194445d78aebf7bf7 100644 (file)
--- 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 (file)
index 0000000..b711cc9
--- /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)
+       }
+}
index 6ba0858a403451036c8afa9d96d433a510e65f0d..02556bfc4df4c97719168aa3c1950b8daba6a2ac 100644 (file)
@@ -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,