]> Sergey Matveev's repositories - btrtrc.git/blob - requesting.go
Increase request update interval if there's no signalling
[btrtrc.git] / requesting.go
1 package torrent
2
3 import (
4         "time"
5         "unsafe"
6
7         "github.com/anacrolix/missinggo/v2/bitmap"
8
9         "github.com/anacrolix/chansync"
10         request_strategy "github.com/anacrolix/torrent/request-strategy"
11         "github.com/anacrolix/torrent/types"
12 )
13
14 func (cl *Client) requester() {
15         for {
16                 update := func() chansync.Signaled {
17                         cl.lock()
18                         defer cl.unlock()
19                         cl.doRequests()
20                         return cl.updateRequests.Signaled()
21                 }()
22                 select {
23                 case <-cl.closed.Done():
24                         return
25                 case <-time.After(100 * time.Millisecond):
26                 }
27                 select {
28                 case <-cl.closed.Done():
29                         return
30                 case <-update:
31                 case <-time.After(time.Second):
32                 }
33         }
34 }
35
36 func (cl *Client) tickleRequester() {
37         cl.updateRequests.Broadcast()
38 }
39
40 func (cl *Client) doRequests() {
41         ts := make([]request_strategy.Torrent, 0, len(cl.torrents))
42         for _, t := range cl.torrents {
43                 rst := request_strategy.Torrent{
44                         StableId: uintptr(unsafe.Pointer(t)),
45                 }
46                 if t.storage != nil {
47                         rst.Capacity = t.storage.Capacity
48                 }
49                 for i := range t.pieces {
50                         p := &t.pieces[i]
51                         rst.Pieces = append(rst.Pieces, request_strategy.Piece{
52                                 Request:          !t.ignorePieceForRequests(i),
53                                 Priority:         p.purePriority(),
54                                 Partial:          t.piecePartiallyDownloaded(i),
55                                 Availability:     p.availability,
56                                 Length:           int64(p.length()),
57                                 NumPendingChunks: int(t.pieceNumPendingChunks(i)),
58                                 IterPendingChunks: func(f func(types.ChunkSpec)) {
59                                         p.iterUndirtiedChunks(func(cs ChunkSpec) bool {
60                                                 f(cs)
61                                                 return true
62                                         })
63                                 },
64                         })
65                 }
66                 t.iterPeers(func(p *Peer) {
67                         if p.closed.IsSet() {
68                                 return
69                         }
70                         if p.piecesReceivedSinceLastRequestUpdate > p.maxPiecesReceivedBetweenRequestUpdates {
71                                 p.maxPiecesReceivedBetweenRequestUpdates = p.piecesReceivedSinceLastRequestUpdate
72                         }
73                         p.piecesReceivedSinceLastRequestUpdate = 0
74                         rst.Peers = append(rst.Peers, request_strategy.Peer{
75                                 HasPiece:    p.peerHasPiece,
76                                 MaxRequests: p.nominalMaxRequests(),
77                                 HasExistingRequest: func(r request_strategy.Request) bool {
78                                         _, ok := p.actualRequestState.Requests[r]
79                                         return ok
80                                 },
81                                 Choking: p.peerChoking,
82                                 PieceAllowedFast: func(i pieceIndex) bool {
83                                         return p.peerAllowedFast.Contains(bitmap.BitIndex(i))
84                                 },
85                                 DownloadRate: p.downloadRate(),
86                                 Age:          time.Since(p.completedHandshake),
87                                 Id:           (*peerId)(p),
88                         })
89                 })
90                 ts = append(ts, rst)
91         }
92         nextPeerStates := request_strategy.Run(request_strategy.Input{
93                 Torrents:           ts,
94                 MaxUnverifiedBytes: cl.config.MaxUnverifiedBytes,
95         })
96         for p, state := range nextPeerStates {
97                 setPeerNextRequestState(p, state)
98         }
99 }
100
101 type peerId Peer
102
103 func (p *peerId) Uintptr() uintptr {
104         return uintptr(unsafe.Pointer(p))
105 }
106
107 func setPeerNextRequestState(_p request_strategy.PeerId, rp request_strategy.PeerNextRequestState) {
108         p := (*Peer)(_p.(*peerId))
109         p.nextRequestState = rp
110         p.onNextRequestStateChanged()
111 }
112
113 func (p *Peer) applyNextRequestState() bool {
114         next := p.nextRequestState
115         current := p.actualRequestState
116         if !p.setInterested(next.Interested) {
117                 return false
118         }
119         for req := range current.Requests {
120                 if _, ok := next.Requests[req]; !ok {
121                         if !p.cancel(req) {
122                                 return false
123                         }
124                 }
125         }
126         for req := range next.Requests {
127                 more, err := p.request(req)
128                 if err != nil {
129                         panic(err)
130                 } else {
131                         //log.Print(req)
132                 }
133                 if !more {
134                         return false
135                 }
136         }
137         return true
138 }