From: Matt Joiner Date: Wed, 2 Jul 2025 05:09:16 +0000 (+1000) Subject: Stop reading webseed response if enough chunks are no longer wanted X-Git-Tag: v1.59.0~52 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=c3a00b6b3780afa743fd3927da751614786f45ca;p=btrtrc.git Stop reading webseed response if enough chunks are no longer wanted --- diff --git a/webseed-peer.go b/webseed-peer.go index b453deec..c2543c5b 100644 --- a/webseed-peer.go +++ b/webseed-peer.go @@ -136,6 +136,9 @@ func (ws *webseedPeer) spawnRequest(begin, end RequestIndex) { func (ws *webseedPeer) runRequest(webseedRequest *webseedRequest) { locker := ws.locker err := ws.readChunks(webseedRequest) + if webseed.PrintDebug && webseedRequest.next < webseedRequest.end { + fmt.Printf("webseed peer stopped reading chunks early\n") + } // Ensure the body reader and response are closed. webseedRequest.Close() if err != nil { @@ -285,7 +288,7 @@ func (ws *webseedPeer) readChunks(wr *webseedRequest) (err error) { msg := pp.Message{ Type: pp.Piece, } - for ws.keepReading(wr) { + for { reqSpec := t.requestIndexToRequest(wr.next) chunkLen := reqSpec.Length.Int() buf = buf[:chunkLen] @@ -301,16 +304,22 @@ func (ws *webseedPeer) readChunks(wr *webseedRequest) (err error) { msg.Piece = buf msg.Index = reqSpec.Index msg.Begin = reqSpec.Begin + ws.peer.locker().Lock() - err = ws.peer.receiveChunk(&msg) + // Ensure the request is pointing to the next chunk before receiving the current one. If + // webseed requests are triggered, we want to ensure our existing request is up to date. wr.next++ + err = ws.peer.receiveChunk(&msg) + stop := err != nil || !ws.keepReading(wr) ws.peer.locker().Unlock() + if err != nil { err = fmt.Errorf("processing chunk: %w", err) + } + if stop { return } } - return } func (me *webseedPeer) peerPieces() *roaring.Bitmap {