]> Sergey Matveev's repositories - btrtrc.git/blob - webseed-peer.go
Handle 503 returns from webseed peer endpoints
[btrtrc.git] / webseed-peer.go
1 package torrent
2
3 import (
4         "context"
5         "errors"
6         "fmt"
7         "math/rand"
8         "sync"
9         "time"
10
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"
16 )
17
18 type webseedPeer struct {
19         client         webseed.Client
20         activeRequests map[Request]webseed.Request
21         requesterCond  sync.Cond
22         peer           Peer
23         // Number of requester routines.
24         maxRequests int
25 }
26
27 var _ peerImpl = (*webseedPeer)(nil)
28
29 func (me *webseedPeer) connStatusString() string {
30         return me.client.Url
31 }
32
33 func (ws *webseedPeer) String() string {
34         return fmt.Sprintf("webseed peer for %q", ws.client.Url)
35 }
36
37 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
38         ws.client.SetInfo(info)
39         // There should be probably be a callback in Client instead, so it can remove pieces at its whim
40         // too.
41         ws.client.Pieces.Iterate(func(x uint32) bool {
42                 ws.peer.t.incPieceAvailability(pieceIndex(x))
43                 return true
44         })
45 }
46
47 func (ws *webseedPeer) writeInterested(interested bool) bool {
48         return true
49 }
50
51 func (ws *webseedPeer) _cancel(r RequestIndex) bool {
52         active, ok := ws.activeRequests[ws.peer.t.requestIndexToRequest(r)]
53         if ok {
54                 active.Cancel()
55         }
56         if !ws.peer.deleteRequest(r) {
57                 panic("cancelled webseed request should exist")
58         }
59         if ws.peer.isLowOnRequests() {
60                 ws.peer.updateRequests("webseedPeer._cancel")
61         }
62         return true
63 }
64
65 func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
66         return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
67 }
68
69 func (ws *webseedPeer) _request(r Request) bool {
70         ws.requesterCond.Signal()
71         return true
72 }
73
74 func (ws *webseedPeer) doRequest(r Request) error {
75         webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
76         ws.activeRequests[r] = webseedRequest
77         err := func() error {
78                 ws.requesterCond.L.Unlock()
79                 defer ws.requesterCond.L.Lock()
80                 return ws.requestResultHandler(r, webseedRequest)
81         }()
82         delete(ws.activeRequests, r)
83         return err
84 }
85
86 func (ws *webseedPeer) requester(i int) {
87         ws.requesterCond.L.Lock()
88         defer ws.requesterCond.L.Unlock()
89 start:
90         for !ws.peer.closed.IsSet() {
91                 restart := false
92                 ws.peer.actualRequestState.Requests.Iterate(func(x uint32) bool {
93                         r := ws.peer.t.requestIndexToRequest(x)
94                         if _, ok := ws.activeRequests[r]; ok {
95                                 return true
96                         }
97                         err := ws.doRequest(r)
98                         ws.requesterCond.L.Unlock()
99                         if err != nil {
100                                 log.Printf("requester %v: error doing webseed request %v: %v", i, r, err)
101                         }
102                         restart = true
103                         if errors.Is(err, webseed.ErrTooFast) {
104                                 time.Sleep(time.Duration(rand.Int63n(int64(10 * time.Second))))
105                         }
106                         ws.requesterCond.L.Lock()
107                         return false
108                 })
109                 if restart {
110                         goto start
111                 }
112                 ws.requesterCond.Wait()
113         }
114 }
115
116 func (ws *webseedPeer) connectionFlags() string {
117         return "WS"
118 }
119
120 // TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could
121 // return bool if this is even possible, and if it isn't, skip to the next drop candidate.
122 func (ws *webseedPeer) drop() {}
123
124 func (ws *webseedPeer) handleUpdateRequests() {
125         // Because this is synchronous, webseed peers seem to get first dibs on newly prioritized
126         // pieces.
127         ws.peer.maybeUpdateActualRequestState()
128 }
129
130 func (ws *webseedPeer) onClose() {
131         ws.peer.logger.WithLevel(log.Debug).Print("closing")
132         ws.peer.deleteAllRequests()
133         for _, r := range ws.activeRequests {
134                 r.Cancel()
135         }
136         ws.requesterCond.Broadcast()
137 }
138
139 func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) error {
140         result := <-webseedRequest.Result
141         close(webseedRequest.Result) // one-shot
142         // We do this here rather than inside receiveChunk, since we want to count errors too. I'm not
143         // sure if we can divine which errors indicate cancellation on our end without hitting the
144         // network though.
145         if len(result.Bytes) != 0 || result.Err == nil {
146                 // Increment ChunksRead and friends
147                 ws.peer.doChunkReadStats(int64(len(result.Bytes)))
148         }
149         ws.peer.readBytes(int64(len(result.Bytes)))
150         ws.peer.t.cl.lock()
151         defer ws.peer.t.cl.unlock()
152         if ws.peer.t.closed.IsSet() {
153                 return nil
154         }
155         err := result.Err
156         if err != nil {
157                 switch {
158                 case errors.Is(err, context.Canceled):
159                 case errors.Is(err, webseed.ErrTooFast):
160                 case ws.peer.closed.IsSet():
161                 default:
162                         ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
163                         // // Here lies my attempt to extract something concrete from Go's error system. RIP.
164                         // cfg := spew.NewDefaultConfig()
165                         // cfg.DisableMethods = true
166                         // cfg.Dump(result.Err)
167                         log.Printf("closing %v", ws)
168                         ws.peer.close()
169                 }
170                 ws.peer.remoteRejectedRequest(ws.peer.t.requestIndexFromRequest(r))
171                 return err
172         }
173         err = ws.peer.receiveChunk(&pp.Message{
174                 Type:  pp.Piece,
175                 Index: r.Index,
176                 Begin: r.Begin,
177                 Piece: result.Bytes,
178         })
179         if err != nil {
180                 panic(err)
181         }
182         return err
183 }
184
185 func (me *webseedPeer) isLowOnRequests() bool {
186         return me.peer.actualRequestState.Requests.GetCardinality() < uint64(me.maxRequests)
187 }
188
189 func (me *webseedPeer) peerPieces() *roaring.Bitmap {
190         return &me.client.Pieces
191 }
192
193 func (cn *webseedPeer) peerHasAllPieces() (all, known bool) {
194         if !cn.peer.t.haveInfo() {
195                 return true, false
196         }
197         return cn.client.Pieces.GetCardinality() == uint64(cn.peer.t.numPieces()), true
198 }