]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Truncate webseed requests to response body cache boundaries
authorMatt Joiner <anacrolix@gmail.com>
Thu, 31 Jul 2025 01:54:39 +0000 (11:54 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 31 Jul 2025 01:54:39 +0000 (11:54 +1000)
webseed-requesting.go
webseed/request.go

index ee1f93960eda6ce2d282864b4895d9851d5e651b..2376e685490497457ebaf8512f3d651d1425e8c2 100644 (file)
@@ -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:")
index ce8e0cd58894c3616d60fc7c752e06588fb082f1..ff679aceb4f3b17a177619a3b5898f427e1c3ea4 100644 (file)
@@ -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))
        }