From e36e12fec21b16f3177417ce3c4d34f3f79aeede Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 5 Aug 2025 19:54:51 +1000 Subject: [PATCH] Add TORRENT_MAX_ACTIVE_PIECE_HASHERS --- client.go | 4 +++- env.go | 18 ++++++++++++++++++ webseed-requesting.go | 9 +-------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 4c3c6dc2..f67f2a89 100644 --- a/client.go +++ b/client.go @@ -1962,8 +1962,10 @@ func (cl *Client) checkConfig() error { return nil } +var maxActivePieceHashers = initIntFromEnv("TORRENT_MAX_ACTIVE_PIECE_HASHERS", runtime.NumCPU(), 0) + func (cl *Client) maxActivePieceHashers() int { - return runtime.NumCPU() + return maxActivePieceHashers } func (cl *Client) belowMaxActivePieceHashers() bool { diff --git a/env.go b/env.go index 10cbafc7..c8759d6e 100644 --- a/env.go +++ b/env.go @@ -1 +1,19 @@ package torrent + +import ( + "os" + "strconv" + + "github.com/anacrolix/missinggo/v2/panicif" + "golang.org/x/exp/constraints" +) + +func initIntFromEnv[T constraints.Integer](key string, defaultValue T, bitSize int) T { + s := os.Getenv(key) + if s == "" { + return defaultValue + } + i64, err := strconv.ParseInt(s, 10, bitSize) + panicif.Err(err) + return T(i64) +} diff --git a/webseed-requesting.go b/webseed-requesting.go index a4cc17d5..698eea79 100644 --- a/webseed-requesting.go +++ b/webseed-requesting.go @@ -9,7 +9,6 @@ import ( "maps" "os" "runtime/pprof" - "strconv" "strings" "sync" "time" @@ -24,13 +23,7 @@ import ( "github.com/anacrolix/torrent/webseed" ) -var webseedHostRequestConcurrency int - -func init() { - i64, err := strconv.ParseInt(cmp.Or(os.Getenv("TORRENT_WEBSEED_HOST_REQUEST_CONCURRENCY"), "10"), 10, 0) - panicif.Err(err) - webseedHostRequestConcurrency = int(i64) -} +var webseedHostRequestConcurrency = initIntFromEnv("TORRENT_WEBSEED_HOST_REQUEST_CONCURRENCY", 10, 0) type ( webseedHostKey string -- 2.51.0