11 "github.com/RoaringBitmap/roaring"
12 "github.com/anacrolix/log"
13 "github.com/anacrolix/torrent/metainfo"
14 pp "github.com/anacrolix/torrent/peer_protocol"
15 "github.com/anacrolix/torrent/webseed"
18 type webseedPeer struct {
19 // First field for stats alignment.
22 activeRequests map[Request]webseed.Request
23 requesterCond sync.Cond
24 // Number of requester routines.
28 var _ peerImpl = (*webseedPeer)(nil)
30 func (me *webseedPeer) connStatusString() string {
34 func (ws *webseedPeer) String() string {
35 return fmt.Sprintf("webseed peer for %q", ws.client.Url)
38 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
39 ws.client.SetInfo(info)
40 // There should be probably be a callback in Client instead, so it can remove pieces at its whim
42 ws.client.Pieces.Iterate(func(x uint32) bool {
43 ws.peer.t.incPieceAvailability(pieceIndex(x))
48 func (ws *webseedPeer) writeInterested(interested bool) bool {
52 func (ws *webseedPeer) _cancel(r RequestIndex) bool {
53 if active, ok := ws.activeRequests[ws.peer.t.requestIndexToRequest(r)]; ok {
55 // The requester is running and will handle the result.
58 // There should be no requester handling this, so no further events will occur.
62 func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
63 return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
66 func (ws *webseedPeer) _request(r Request) bool {
67 ws.requesterCond.Signal()
71 func (ws *webseedPeer) doRequest(r Request) error {
72 webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
73 ws.activeRequests[r] = webseedRequest
75 ws.requesterCond.L.Unlock()
76 defer ws.requesterCond.L.Lock()
77 return ws.requestResultHandler(r, webseedRequest)
79 delete(ws.activeRequests, r)
83 func (ws *webseedPeer) requester(i int) {
84 ws.requesterCond.L.Lock()
85 defer ws.requesterCond.L.Unlock()
87 for !ws.peer.closed.IsSet() {
89 ws.peer.requestState.Requests.Iterate(func(x RequestIndex) bool {
90 r := ws.peer.t.requestIndexToRequest(x)
91 if _, ok := ws.activeRequests[r]; ok {
94 err := ws.doRequest(r)
95 ws.requesterCond.L.Unlock()
96 if err != nil && !errors.Is(err, context.Canceled) {
97 log.Printf("requester %v: error doing webseed request %v: %v", i, r, err)
100 if errors.Is(err, webseed.ErrTooFast) {
101 time.Sleep(time.Duration(rand.Int63n(int64(10 * time.Second))))
103 ws.requesterCond.L.Lock()
109 ws.requesterCond.Wait()
113 func (ws *webseedPeer) connectionFlags() string {
117 // Maybe this should drop all existing connections, or something like that.
118 func (ws *webseedPeer) drop() {}
120 func (cn *webseedPeer) ban() {
124 func (ws *webseedPeer) handleUpdateRequests() {
125 // Because this is synchronous, webseed peers seem to get first dibs on newly prioritized
129 defer ws.peer.t.cl.unlock()
130 ws.peer.maybeUpdateActualRequestState()
134 func (ws *webseedPeer) onClose() {
135 ws.peer.logger.Levelf(log.Debug, "closing")
136 // Just deleting them means we would have to manually cancel active requests.
137 ws.peer.cancelAllRequests()
138 ws.peer.t.iterPeers(func(p *Peer) {
139 if p.isLowOnRequests() {
140 p.updateRequests("webseedPeer.onClose")
143 ws.requesterCond.Broadcast()
146 func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) error {
147 result := <-webseedRequest.Result
148 close(webseedRequest.Result) // one-shot
149 // We do this here rather than inside receiveChunk, since we want to count errors too. I'm not
150 // sure if we can divine which errors indicate cancellation on our end without hitting the
152 if len(result.Bytes) != 0 || result.Err == nil {
153 // Increment ChunksRead and friends
154 ws.peer.doChunkReadStats(int64(len(result.Bytes)))
156 ws.peer.readBytes(int64(len(result.Bytes)))
158 defer ws.peer.t.cl.unlock()
159 if ws.peer.t.closed.IsSet() {
165 case errors.Is(err, context.Canceled):
166 case errors.Is(err, webseed.ErrTooFast):
167 case ws.peer.closed.IsSet():
169 ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
170 // // Here lies my attempt to extract something concrete from Go's error system. RIP.
171 // cfg := spew.NewDefaultConfig()
172 // cfg.DisableMethods = true
173 // cfg.Dump(result.Err)
174 log.Printf("closing %v", ws)
177 if !ws.peer.remoteRejectedRequest(ws.peer.t.requestIndexFromRequest(r)) {
178 panic("invalid reject")
182 err = ws.peer.receiveChunk(&pp.Message{
194 func (me *webseedPeer) peerPieces() *roaring.Bitmap {
195 return &me.client.Pieces
198 func (cn *webseedPeer) peerHasAllPieces() (all, known bool) {
199 if !cn.peer.t.haveInfo() {
202 return cn.client.Pieces.GetCardinality() == uint64(cn.peer.t.numPieces()), true