peerconn.go | 12 ++---------- pending-requests.go | 34 ++++++++++++++++++++++++++++++++++ requesting.go | 2 +- torrent.go | 11 +++-------- diff --git a/peerconn.go b/peerconn.go index dc4ce8d13714d4add7bb2461d8fa11a3e93d39ea..f0efb19dd00aee36ded2712bab52bec1aa98fd7f 100644 --- a/peerconn.go +++ b/peerconn.go @@ -588,7 +588,7 @@ if cn.validReceiveChunks == nil { 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 { @@ -1431,15 +1431,7 @@ for _, f := range c.callbacks.DeletedRequest { 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 } diff --git a/pending-requests.go b/pending-requests.go new file mode 100644 index 0000000000000000000000000000000000000000..4afefe65027c1d087e1c8f3d26de13b4da73a92e --- /dev/null +++ b/pending-requests.go @@ -0,0 +1,34 @@ +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] +} diff --git a/requesting.go b/requesting.go index f481351673a1daedf8c8830fe61c08bdf7640016..f7b4f7c4e3ff4c3af91e7a38d371c186008141a5 100644 --- a/requesting.go +++ b/requesting.go @@ -140,7 +140,7 @@ rightPieceIndex := rightRequest / t.chunksPerRegularPiece() 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-- } diff --git a/torrent.go b/torrent.go index b2596be3a47af8b79a3053e93d189e7a7ec8dd2e..d2187a054f4633b209d5f17702722a0e17ebdb6e 100644 --- a/torrent.go +++ b/torrent.go @@ -145,7 +145,7 @@ // different pieces. 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 @@ -445,7 +445,7 @@ } 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) @@ -1429,12 +1429,7 @@ return } 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) {