cn.validReceiveChunks = make(map[RequestIndex]int)
}
cn.validReceiveChunks[r]++
- cn.t.pendingRequests[r]++
+ cn.t.pendingRequests.Inc(r)
cn.updateExpectingChunks()
ppReq := cn.t.requestIndexToRequest(r)
for _, f := range cn.callbacks.SentRequest {
f(PeerRequestEvent{c, c.t.requestIndexToRequest(r)})
}
c.updateExpectingChunks()
- pr := c.t.pendingRequests
- pr[r]--
- n := pr[r]
- if n == 0 {
- delete(pr, r)
- }
- if n < 0 {
- panic(n)
- }
+ c.t.pendingRequests.Dec(r)
return true
}
--- /dev/null
+package torrent
+
+type pendingRequests struct {
+ m map[RequestIndex]int
+}
+
+func (p pendingRequests) Dec(r RequestIndex) {
+ p.m[r]--
+ n := p.m[r]
+ if n == 0 {
+ delete(p.m, r)
+ }
+ if n < 0 {
+ panic(n)
+ }
+}
+
+func (p pendingRequests) Inc(r RequestIndex) {
+ p.m[r]++
+}
+
+func (p *pendingRequests) Init() {
+ p.m = make(map[RequestIndex]int)
+}
+
+func (p *pendingRequests) AssertEmpty() {
+ if len(p.m) != 0 {
+ panic(p.m)
+ }
+}
+
+func (p pendingRequests) Get(r RequestIndex) int {
+ return p.m[r]
+}
leftCurrent := p.peer.actualRequestState.Requests.Contains(leftRequest)
rightCurrent := p.peer.actualRequestState.Requests.Contains(rightRequest)
pending := func(index RequestIndex, current bool) int {
- ret := t.pendingRequests[index]
+ ret := t.pendingRequests.Get(index)
if current {
ret--
}
connPieceInclinationPool sync.Pool
// Count of each request across active connections.
- pendingRequests map[RequestIndex]int
+ pendingRequests pendingRequests
// Chunks we've written to since the corresponding piece was last checked.
dirtyChunks roaring.Bitmap
t.cl.event.Broadcast()
close(t.gotMetainfoC)
t.updateWantPeersEvent()
- t.pendingRequests = make(map[RequestIndex]int)
+ t.pendingRequests.Init()
t.tryCreateMorePieceHashers()
t.iterPeers(func(p *Peer) {
p.onGotInfo(t.info)
}
func (t *Torrent) assertNoPendingRequests() {
- if len(t.pendingRequests) != 0 {
- panic(t.pendingRequests)
- }
- //if len(t.lastRequested) != 0 {
- // panic(t.lastRequested)
- //}
+ t.pendingRequests.AssertEmpty()
}
func (t *Torrent) dropConnection(c *PeerConn) {