"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)
}
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()))