From 9defe70f571c732e997514a7d2726d649ab8076c Mon Sep 17 00:00:00 2001 From: Peter Kieltyka Date: Mon, 18 Mar 2024 10:57:11 -0400 Subject: [PATCH] fix race --- ethmonitor/ethmonitor.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ethmonitor/ethmonitor.go b/ethmonitor/ethmonitor.go index c6dda550..a081427b 100644 --- a/ethmonitor/ethmonitor.go +++ b/ethmonitor/ethmonitor.go @@ -114,7 +114,7 @@ type Monitor struct { chainID *big.Int nextBlockNumber *big.Int nextBlockNumberMu sync.Mutex - pollInterval time.Duration + pollInterval atomic.Int64 cache cachestore.Store[[]byte] @@ -322,7 +322,7 @@ func (m *Monitor) listenNewHead() <-chan uint64 { case <-m.ctx.Done(): close(nextBlock) return - case <-time.After(m.pollInterval): + case <-time.After(time.Duration(m.pollInterval.Load())): nextBlock <- 0 } } @@ -415,9 +415,9 @@ func (m *Monitor) monitor() error { // if we hit a miss between calls, then we reset the pollInterval, otherwise // we speed up the polling interval if miss { - m.pollInterval = m.options.PollingInterval + m.pollInterval.Store(int64(m.options.PollingInterval)) } else { - m.pollInterval = clampDuration(minLoopInterval, m.pollInterval/4) + m.pollInterval.Store(int64(clampDuration(minLoopInterval, time.Duration(m.pollInterval.Load())/4))) } // build deterministic set of add/remove events which construct the canonical chain