From 50c5d486d4940c49467d232a14b5b634bce4ca41 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 31 Jul 2025 11:54:39 +1000 Subject: [PATCH] Truncate webseed requests to response body cache boundaries --- webseed-requesting.go | 11 +++++++++++ webseed/request.go | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) 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)) } -- 2.51.0