From: Matt Joiner Date: Thu, 31 Jul 2025 01:54:39 +0000 (+1000) Subject: Truncate webseed requests to response body cache boundaries X-Git-Tag: v1.59.0~2^2~79 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=50c5d486d4940c49467d232a14b5b634bce4ca41;p=btrtrc.git Truncate webseed requests to response body cache boundaries --- diff --git a/webseed-requesting.go b/webseed-requesting.go index ee1f9396..2376e685 100644 --- a/webseed-requesting.go +++ b/webseed-requesting.go @@ -205,6 +205,7 @@ func (cl *Client) updateWebseedRequests() { panicif.LessThan(last, begin) // Hello C++ my old friend. end := last + 1 + truncateEndToCacheBoundary(begin, &end, t.chunkSize) if webseed.PrintDebug && end != fileEnd { debugLogger.Debug( "shortened webseed request", @@ -218,6 +219,16 @@ func (cl *Client) updateWebseedRequests() { } } +// Limit a webseed request end request index so that the required response body size fits within +// cache limits for a WebSeed provider. +func truncateEndToCacheBoundary(start RequestIndex, end *RequestIndex, chunkSize pp.Integer) { + // Cloudflare caches up to 512 MB responses by default. + const cacheResponseBodyLimit = 256 << 20 + chunksPerAlignedResponse := RequestIndex(cacheResponseBodyLimit / chunkSize) + startIndex := start / chunksPerAlignedResponse + *end = min(*end, (startIndex+1)*chunksPerAlignedResponse) +} + func (cl *Client) dumpCurrentWebseedRequests() { if webseed.PrintDebug { fmt.Println("current webseed requests:") diff --git a/webseed/request.go b/webseed/request.go index ce8e0cd5..ff679ace 100644 --- a/webseed/request.go +++ b/webseed/request.go @@ -75,7 +75,7 @@ func newRequest( return nil, err } // We avoid Range requests if we can. We check the Content-Length elsewhere so that early - // detection is not lost. + // detection is not lost. TODO: Try disabling this for CloudFlare? if offset != 0 || length != fileInfo.Length { req.Header.Set("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1)) }