]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Extract pendingRequests
authorMatt Joiner <anacrolix@gmail.com>
Sat, 9 Oct 2021 08:00:58 +0000 (19:00 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 19 Oct 2021 03:08:56 +0000 (14:08 +1100)
peerconn.go
pending-requests.go [new file with mode: 0644]
requesting.go
torrent.go

index dc4ce8d13714d4add7bb2461d8fa11a3e93d39ea..f0efb19dd00aee36ded2712bab52bec1aa98fd7f 100644 (file)
@@ -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 (file)
index 0000000..4afefe6
--- /dev/null
@@ -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]
+}
index f481351673a1daedf8c8830fe61c08bdf7640016..f7b4f7c4e3ff4c3af91e7a38d371c186008141a5 100644 (file)
@@ -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--
                }
index b2596be3a47af8b79a3053e93d189e7a7ec8dd2e..d2187a054f4633b209d5f17702722a0e17ebdb6e 100644 (file)
@@ -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) {