github.com/anacrolix/torrent@v1.61.0/webseed-request.go (about)

     1  package torrent
     2  
     3  import (
     4  	"fmt"
     5  	"log/slog"
     6  	"sync/atomic"
     7  
     8  	"github.com/anacrolix/torrent/webseed"
     9  )
    10  
    11  // A wrapper around webseed.Request with extra state for webseedPeer.
    12  type webseedRequest struct {
    13  	// Fingers out.
    14  	request webseed.Request
    15  	logger  *slog.Logger
    16  	// First assigned in the range.
    17  	begin RequestIndex
    18  	// The next to be read.
    19  	next RequestIndex
    20  	// One greater than the end of the range.
    21  	end       RequestIndex
    22  	cancelled atomic.Bool
    23  }
    24  
    25  func (me *webseedRequest) Close() {
    26  	me.request.Close()
    27  }
    28  
    29  // Record that it was exceptionally cancelled. Require that Torrent be passed so we can ensure
    30  // announce dispatcher is updated.
    31  func (me *webseedRequest) Cancel(cause string, t *Torrent) bool {
    32  	me.request.Cancel(stringError(cause))
    33  	if !me.cancelled.Swap(true) {
    34  		if webseed.PrintDebug {
    35  			me.logger.Debug("webseed request cancelled", "cause", cause)
    36  		}
    37  		t.deferUpdateRegularTrackerAnnouncing()
    38  		return true
    39  	}
    40  	return false
    41  }
    42  
    43  func (me *webseedRequest) String() string {
    44  	s := fmt.Sprintf("%v of [%v-%v)", me.next, me.begin, me.end)
    45  	if me.cancelled.Load() {
    46  		s += " (cancelled)"
    47  	}
    48  	return s
    49  }