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