From: Matt Joiner Date: Sat, 9 Oct 2021 08:00:58 +0000 (+1100) Subject: Extract pendingRequests X-Git-Tag: v1.34.0^2~55 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=9aff9f35929ff67872cc5f76cc386de45c3758e8;p=btrtrc.git Extract pendingRequests --- diff --git a/peerconn.go b/peerconn.go index dc4ce8d1..f0efb19d 100644 --- a/peerconn.go +++ b/peerconn.go @@ -588,7 +588,7 @@ func (cn *Peer) request(r RequestIndex) (more bool, err error) { 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 @@ func (c *Peer) deleteRequest(r RequestIndex) bool { 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 00000000..4afefe65 --- /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 f4813516..f7b4f7c4 100644 --- a/requesting.go +++ b/requesting.go @@ -140,7 +140,7 @@ func (p peerRequests) Less(i, j int) bool { 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 b2596be3..d2187a05 100644 --- a/torrent.go +++ b/torrent.go @@ -145,7 +145,7 @@ type Torrent struct { 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 @@ func (t *Torrent) onSetInfo() { 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 @@ func (t *Torrent) numActivePeers() (num int) { } 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) {