From a6cb3d81c01f87d86ab9a45eec90a38d86bb29e5 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 21 May 2025 12:44:55 +1000 Subject: [PATCH] Update request reason rejigging --- requesting.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/requesting.go b/requesting.go index 591b407a..27872059 100644 --- a/requesting.go +++ b/requesting.go @@ -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 { -- 2.51.0