]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Update request reason rejigging
authorMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2025 02:44:55 +0000 (12:44 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2025 02:44:55 +0000 (12:44 +1000)
requesting.go

index 591b407ae640b870c02f93040d1c8594b8bd0b62..278720593eda861a420bde38573ca0052ef979b4 100644 (file)
@@ -10,11 +10,12 @@ import (
        "unsafe"
 
        "github.com/RoaringBitmap/roaring"
-       g "github.com/anacrolix/generics"
-       "github.com/anacrolix/generics/heap"
        "github.com/anacrolix/log"
        "github.com/anacrolix/multiless"
 
+       g "github.com/anacrolix/generics"
+       "github.com/anacrolix/generics/heap"
+
        "github.com/anacrolix/torrent/metainfo"
        requestStrategy "github.com/anacrolix/torrent/request-strategy"
        typedRoaring "github.com/anacrolix/torrent/typed-roaring"
@@ -244,14 +245,21 @@ func (p *Peer) getDesiredRequestState() (desired desiredRequestState) {
        return
 }
 
+// Update requests if there's a reason assigned.
 func (p *Peer) maybeUpdateActualRequestState() {
-       if p.closed.IsSet() {
+       if p.needRequestUpdate == "" {
                return
        }
-       if p.needRequestUpdate == "" {
+       p.updateRequestsWithReason(p.needRequestUpdate)
+}
+
+// Updates requests right now with the given reason. Clobbers any deferred reason if there was one.
+// Does all the necessary checks and includes profiler tags to assign the overhead.
+func (p *Peer) updateRequestsWithReason(reason updateRequestReason) {
+       if p.closed.IsSet() {
                return
        }
-       if p.needRequestUpdate == peerUpdateRequestsTimerReason {
+       if reason == peerUpdateRequestsTimerReason {
                since := time.Since(p.lastRequestUpdate)
                if since < updateRequestsTimerDuration {
                        panic(since)
@@ -262,11 +270,13 @@ func (p *Peer) maybeUpdateActualRequestState() {
        }
        pprof.Do(
                context.Background(),
-               pprof.Labels("update request", string(p.needRequestUpdate)),
+               pprof.Labels("update request", string(reason)),
                func(_ context.Context) {
                        p.updateRequests()
                },
        )
+       // Is there any chance we should leave this to run again, and have the caller clear it if they
+       // called with this reason?
        p.needRequestUpdate = ""
        p.lastRequestUpdate = time.Now()
        if enableUpdateRequestsTimer {