]> Sergey Matveev's repositories - btrtrc.git/blob - requesting.go
Optimize requesting peerId Uintptr allocation
[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{
88                                         Peer: p,
89                                         ptr:  uintptr(unsafe.Pointer(p)),
90                                 },
91                         })
92                 })
93                 ts = append(ts, rst)
94         }
95         nextPeerStates := request_strategy.Run(request_strategy.Input{
96                 Torrents:           ts,
97                 MaxUnverifiedBytes: cl.config.MaxUnverifiedBytes,
98         })
99         for p, state := range nextPeerStates {
100                 setPeerNextRequestState(p, state)
101         }
102 }
103
104 type peerId struct {
105         *Peer
106         ptr uintptr
107 }
108
109 func (p peerId) Uintptr() uintptr {
110         return p.ptr
111 }
112
113 func setPeerNextRequestState(_p request_strategy.PeerId, rp request_strategy.PeerNextRequestState) {
114         p := _p.(peerId).Peer
115         p.nextRequestState = rp
116         p.onNextRequestStateChanged()
117 }
118
119 func (p *Peer) applyNextRequestState() bool {
120         next := p.nextRequestState
121         current := p.actualRequestState
122         if !p.setInterested(next.Interested) {
123                 return false
124         }
125         for req := range current.Requests {
126                 if _, ok := next.Requests[req]; !ok {
127                         if !p.cancel(req) {
128                                 return false
129                         }
130                 }
131         }
132         for req := range next.Requests {
133                 more, err := p.request(req)
134                 if err != nil {
135                         panic(err)
136                 } /* else {
137                         log.Print(req)
138                 } */
139                 if !more {
140                         return false
141                 }
142         }
143         return true
144 }