torrent/webseed-peer.go

219 lines
5.7 KiB
Go
Raw Normal View History

2020-05-30 07:52:27 +00:00
package torrent
import (
2021-02-09 08:21:54 +00:00
"context"
"errors"
"fmt"
"math/rand"
"sync"
"time"
2020-06-01 08:25:45 +00:00
"github.com/RoaringBitmap/roaring"
2021-10-20 23:28:57 +00:00
"github.com/anacrolix/log"
2022-11-15 12:22:10 +00:00
"github.com/anacrolix/torrent/metainfo"
2020-06-01 08:41:21 +00:00
pp "github.com/anacrolix/torrent/peer_protocol"
2020-06-01 08:25:45 +00:00
"github.com/anacrolix/torrent/webseed"
2020-05-30 07:52:27 +00:00
)
const (
webseedPeerUnhandledErrorSleep = 5 * time.Second
webseedPeerCloseOnUnhandledError = false
)
type webseedPeer struct {
2022-01-31 01:53:44 +00:00
// First field for stats alignment.
peer Peer
client webseed.Client
activeRequests map[Request]webseed.Request
requesterCond sync.Cond
lastUnhandledErr time.Time
2020-06-01 08:25:45 +00:00
}
var _ peerImpl = (*webseedPeer)(nil)
2020-06-01 08:25:45 +00:00
func (me *webseedPeer) peerImplStatusLines() []string {
return []string{
me.client.Url,
fmt.Sprintf("last unhandled error: %v", eventAgeString(me.lastUnhandledErr)),
}
}
func (ws *webseedPeer) String() string {
return fmt.Sprintf("webseed peer for %q", ws.client.Url)
}
func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
ws.client.SetInfo(info)
// There should be probably be a callback in Client instead, so it can remove pieces at its whim
// too.
ws.client.Pieces.Iterate(func(x uint32) bool {
ws.peer.t.incPieceAvailability(pieceIndex(x))
return true
})
}
func (ws *webseedPeer) writeInterested(interested bool) bool {
2020-05-30 07:52:27 +00:00
return true
}
func (ws *webseedPeer) _cancel(r RequestIndex) bool {
if active, ok := ws.activeRequests[ws.peer.t.requestIndexToRequest(r)]; ok {
2021-05-09 13:38:38 +00:00
active.Cancel()
// The requester is running and will handle the result.
return true
}
// There should be no requester handling this, so no further events will occur.
return false
2020-05-30 07:52:27 +00:00
}
func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
}
func (ws *webseedPeer) _request(r Request) bool {
ws.requesterCond.Signal()
return true
2020-05-30 07:52:27 +00:00
}
func (ws *webseedPeer) doRequest(r Request) error {
webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
ws.activeRequests[r] = webseedRequest
err := func() error {
2021-02-09 08:21:54 +00:00
ws.requesterCond.L.Unlock()
defer ws.requesterCond.L.Lock()
return ws.requestResultHandler(r, webseedRequest)
2021-02-09 08:21:54 +00:00
}()
delete(ws.activeRequests, r)
return err
}
func (ws *webseedPeer) requester(i int) {
ws.requesterCond.L.Lock()
defer ws.requesterCond.L.Unlock()
start:
for !ws.peer.closed.IsSet() {
// Restart is set if we don't need to wait for the requestCond before trying again.
2021-09-19 05:16:37 +00:00
restart := false
2022-05-09 01:34:08 +00:00
ws.peer.requestState.Requests.Iterate(func(x RequestIndex) bool {
2021-09-19 05:16:37 +00:00
r := ws.peer.t.requestIndexToRequest(x)
if _, ok := ws.activeRequests[r]; ok {
2021-09-19 05:16:37 +00:00
return true
}
err := ws.doRequest(r)
ws.requesterCond.L.Unlock()
if err != nil && !errors.Is(err, context.Canceled) {
log.Printf("requester %v: error doing webseed request %v: %v", i, r, err)
}
2021-09-19 05:16:37 +00:00
restart = true
if errors.Is(err, webseed.ErrTooFast) {
time.Sleep(time.Duration(rand.Int63n(int64(10 * time.Second))))
}
time.Sleep(time.Until(ws.lastUnhandledErr.Add(webseedPeerUnhandledErrorSleep)))
ws.requesterCond.L.Lock()
2021-09-19 05:16:37 +00:00
return false
})
if restart {
goto start
}
ws.requesterCond.Wait()
}
}
func (ws *webseedPeer) connectionFlags() string {
2020-05-30 07:52:27 +00:00
return "WS"
}
2022-03-11 02:33:34 +00:00
// Maybe this should drop all existing connections, or something like that.
func (ws *webseedPeer) drop() {}
2020-05-30 07:52:27 +00:00
2022-03-11 02:33:34 +00:00
func (cn *webseedPeer) ban() {
cn.peer.close()
}
2021-10-20 23:28:57 +00:00
func (ws *webseedPeer) handleUpdateRequests() {
// Because this is synchronous, webseed peers seem to get first dibs on newly prioritized
// pieces.
go func() {
ws.peer.t.cl.lock()
defer ws.peer.t.cl.unlock()
ws.peer.maybeUpdateActualRequestState()
}()
2020-05-30 07:52:27 +00:00
}
2020-05-31 03:09:56 +00:00
func (ws *webseedPeer) onClose() {
ws.peer.logger.Levelf(log.Debug, "closing")
// Just deleting them means we would have to manually cancel active requests.
ws.peer.cancelAllRequests()
ws.peer.t.iterPeers(func(p *Peer) {
if p.isLowOnRequests() {
p.updateRequests("webseedPeer.onClose")
}
})
ws.requesterCond.Broadcast()
}
2020-06-01 08:41:21 +00:00
func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) error {
2020-06-02 06:41:49 +00:00
result := <-webseedRequest.Result
close(webseedRequest.Result) // one-shot
// We do this here rather than inside receiveChunk, since we want to count errors too. I'm not
// sure if we can divine which errors indicate cancellation on our end without hitting the
// network though.
if len(result.Bytes) != 0 || result.Err == nil {
// Increment ChunksRead and friends
ws.peer.doChunkReadStats(int64(len(result.Bytes)))
}
ws.peer.readBytes(int64(len(result.Bytes)))
ws.peer.t.cl.lock()
2020-06-02 06:41:49 +00:00
defer ws.peer.t.cl.unlock()
if ws.peer.t.closed.IsSet() {
return nil
}
err := result.Err
if err != nil {
switch {
case errors.Is(err, context.Canceled):
case errors.Is(err, webseed.ErrTooFast):
case ws.peer.closed.IsSet():
default:
2021-02-09 08:21:54 +00:00
ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
// // Here lies my attempt to extract something concrete from Go's error system. RIP.
// cfg := spew.NewDefaultConfig()
// cfg.DisableMethods = true
// cfg.Dump(result.Err)
if webseedPeerCloseOnUnhandledError {
log.Printf("closing %v", ws)
ws.peer.close()
} else {
ws.lastUnhandledErr = time.Now()
}
}
if !ws.peer.remoteRejectedRequest(ws.peer.t.requestIndexFromRequest(r)) {
panic("invalid reject")
}
return err
}
err = ws.peer.receiveChunk(&pp.Message{
Type: pp.Piece,
Index: r.Index,
Begin: r.Begin,
Piece: result.Bytes,
})
if err != nil {
panic(err)
2020-06-01 08:41:21 +00:00
}
return err
2020-06-01 08:41:21 +00:00
}
func (me *webseedPeer) peerPieces() *roaring.Bitmap {
return &me.client.Pieces
}
func (cn *webseedPeer) peerHasAllPieces() (all, known bool) {
if !cn.peer.t.haveInfo() {
return true, false
}
return cn.client.Pieces.GetCardinality() == uint64(cn.peer.t.numPieces()), true
}