From 9fc46fff2fc3d11a61a6249260af9eeac948cadb Mon Sep 17 00:00:00 2001 From: Cassandra Heart <7929478+CassOnMars@users.noreply.github.com> Date: Tue, 20 Feb 2024 17:12:29 -0600 Subject: [PATCH] fix: memory leak accumulation in pending frame processing (#59) --- .../ceremony_data_clock_consensus_engine.go | 2 +- node/consensus/time/data_time_reel.go | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/node/consensus/ceremony/ceremony_data_clock_consensus_engine.go b/node/consensus/ceremony/ceremony_data_clock_consensus_engine.go index cc10ab3..395739d 100644 --- a/node/consensus/ceremony/ceremony_data_clock_consensus_engine.go +++ b/node/consensus/ceremony/ceremony_data_clock_consensus_engine.go @@ -30,7 +30,7 @@ import ( ) const PEER_INFO_TTL = 60 * 60 * 1000 -const UNCOOPERATIVE_PEER_INFO_TTL = 60 * 60 * 1000 +const UNCOOPERATIVE_PEER_INFO_TTL = 5 * 60 * 1000 type InclusionMap = map[curves.PairingPoint]*protobufs.InclusionCommitment type PolynomialMap = map[curves.PairingPoint][]curves.PairingScalar diff --git a/node/consensus/time/data_time_reel.go b/node/consensus/time/data_time_reel.go index 782660c..e291ca2 100644 --- a/node/consensus/time/data_time_reel.go +++ b/node/consensus/time/data_time_reel.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/hex" "math/big" + "sort" "sync" lru "github.com/hashicorp/golang-lru/v2" @@ -462,7 +463,23 @@ func (d *DataTimeReel) processPending( frame *protobufs.ClockFrame, ) { d.logger.Debug("process pending") - for f, nextPending := range d.pending { + frameNumbers := []uint64{} + for f := range d.pending { + frameNumbers = append(frameNumbers, f) + } + sort.Slice(frameNumbers, func(i, j int) bool { + return frameNumbers[i] < frameNumbers[j] + }) + limit := 8 + for _, f := range frameNumbers { + if f < d.head.FrameNumber { + delete(d.pending, f) + } + if limit <= 0 { + return + } + + nextPending := d.pending[f] d.logger.Debug( "checking frame set", zap.Uint64("pending_frame_number", f), @@ -498,7 +515,9 @@ func (d *DataTimeReel) processPending( go func() { d.frames <- nextFrame }() + limit-- } + if len(d.pending[f]) == 0 { d.logger.Debug("last next processing, clear list") delete(d.pending, f)