From 64a11e2464d300efa5eebed9e4f9ee407a49d016 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 5 Aug 2025 22:40:18 +1000 Subject: [PATCH] Expose TORRENT_WEBSEED_REQUEST_CHUNK_SIZE --- env.go | 12 ++++++++++-- webseed-requesting.go | 5 +++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/env.go b/env.go index c8759d6e..996e1a8f 100644 --- a/env.go +++ b/env.go @@ -8,12 +8,20 @@ import ( "golang.org/x/exp/constraints" ) -func initIntFromEnv[T constraints.Integer](key string, defaultValue T, bitSize int) T { +func initIntFromEnv[T constraints.Signed](key string, defaultValue T, bitSize int) T { + return strconvFromEnv(key, defaultValue, bitSize, strconv.ParseInt) +} + +func initUIntFromEnv[T constraints.Unsigned](key string, defaultValue T, bitSize int) T { + return strconvFromEnv(key, defaultValue, bitSize, strconv.ParseUint) +} + +func strconvFromEnv[T, U constraints.Integer](key string, defaultValue T, bitSize int, conv func(s string, base, bitSize int) (U, error)) T { s := os.Getenv(key) if s == "" { return defaultValue } - i64, err := strconv.ParseInt(s, 10, bitSize) + i64, err := conv(s, 10, bitSize) panicif.Err(err) return T(i64) } diff --git a/webseed-requesting.go b/webseed-requesting.go index d41d909c..d290d525 100644 --- a/webseed-requesting.go +++ b/webseed-requesting.go @@ -233,8 +233,9 @@ func (t *Torrent) getWebseedRequestEnd(begin RequestIndex, debugLogger *slog.Log return end } -// Cloudflare caches up to 512 MB responses by default. This is also an alignment. -var webseedRequestChunkSize uint64 = 256 << 20 +// Cloudflare caches up to 512 MB responses by default. This is also an alignment. Making this +// smaller will allow requests to complete a smaller set of files faster. +var webseedRequestChunkSize = initUIntFromEnv[uint64]("TORRENT_WEBSEED_REQUEST_CHUNK_SIZE", 64<<20, 64) func (t *Torrent) endRequestForAlignedWebseedResponse(start RequestIndex) RequestIndex { end := min(t.maxEndRequest(), nextMultiple(start, t.chunksPerAlignedWebseedResponse())) -- 2.51.0