]> Sergey Matveev's repositories - btrtrc.git/blob - webseed-peer.go
Fix closing of webseed peers
[btrtrc.git] / webseed-peer.go
1 package torrent
2
3 import (
4         "context"
5         "errors"
6         "fmt"
7         "strings"
8         "sync"
9
10         "github.com/anacrolix/torrent/common"
11         "github.com/anacrolix/torrent/metainfo"
12         pp "github.com/anacrolix/torrent/peer_protocol"
13         "github.com/anacrolix/torrent/segments"
14         "github.com/anacrolix/torrent/webseed"
15 )
16
17 type webseedPeer struct {
18         client         webseed.Client
19         activeRequests map[Request]webseed.Request
20         requesterCond  sync.Cond
21         peer           Peer
22 }
23
24 var _ peerImpl = (*webseedPeer)(nil)
25
26 func (me *webseedPeer) connStatusString() string {
27         return me.client.Url
28 }
29
30 func (ws *webseedPeer) String() string {
31         return fmt.Sprintf("webseed peer for %q", ws.client.Url)
32 }
33
34 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
35         ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
36         ws.client.Info = info
37 }
38
39 func (ws *webseedPeer) _postCancel(r Request) {
40         ws.cancel(r)
41 }
42
43 func (ws *webseedPeer) writeInterested(interested bool) bool {
44         return true
45 }
46
47 func (ws *webseedPeer) cancel(r Request) bool {
48         active, ok := ws.activeRequests[r]
49         if !ok {
50                 return false
51         }
52         active.Cancel()
53         return true
54 }
55
56 func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
57         return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
58 }
59
60 func (ws *webseedPeer) request(r Request) bool {
61         ws.requesterCond.Signal()
62         return true
63 }
64
65 func (ws *webseedPeer) doRequest(r Request) {
66         webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
67         ws.activeRequests[r] = webseedRequest
68         func() {
69                 ws.requesterCond.L.Unlock()
70                 defer ws.requesterCond.L.Lock()
71                 ws.requestResultHandler(r, webseedRequest)
72         }()
73         delete(ws.activeRequests, r)
74 }
75
76 func (ws *webseedPeer) requester() {
77         ws.requesterCond.L.Lock()
78         defer ws.requesterCond.L.Unlock()
79 start:
80         for !ws.peer.closed.IsSet() {
81                 for r := range ws.peer.requests {
82                         if _, ok := ws.activeRequests[r]; ok {
83                                 continue
84                         }
85                         ws.doRequest(r)
86                         goto start
87                 }
88                 ws.requesterCond.Wait()
89         }
90 }
91
92 func (ws *webseedPeer) connectionFlags() string {
93         return "WS"
94 }
95
96 // TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could
97 // return bool if this is even possible, and if it isn't, skip to the next drop candidate.
98 func (ws *webseedPeer) drop() {}
99
100 func (ws *webseedPeer) updateRequests() {
101         ws.peer.doRequestState()
102 }
103
104 func (ws *webseedPeer) onClose() {
105         ws.peer.logger.Print("closing")
106         for _, r := range ws.activeRequests {
107                 r.Cancel()
108         }
109         ws.requesterCond.Broadcast()
110 }
111
112 func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) {
113         result := <-webseedRequest.Result
114         ws.peer.t.cl.lock()
115         defer ws.peer.t.cl.unlock()
116         if result.Err != nil {
117                 if !errors.Is(result.Err, context.Canceled) {
118                         ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
119                 }
120                 // Always close for now. We need to filter out temporary errors, but this is a nightmare in
121                 // Go. Currently a bad webseed URL can starve out the good ones due to the chunk selection
122                 // algorithm.
123                 const closeOnAllErrors = false
124                 if closeOnAllErrors || strings.Contains(result.Err.Error(), "unsupported protocol scheme") {
125                         ws.peer.close()
126                 } else {
127                         ws.peer.remoteRejectedRequest(r)
128                 }
129         } else {
130                 err := ws.peer.receiveChunk(&pp.Message{
131                         Type:  pp.Piece,
132                         Index: r.Index,
133                         Begin: r.Begin,
134                         Piece: result.Bytes,
135                 })
136                 if err != nil {
137                         panic(err)
138                 }
139         }
140 }