diff --git a/utils/protoproxy.go b/utils/protoproxy.go index 5b16c091..e1c5adc7 100644 --- a/utils/protoproxy.go +++ b/utils/protoproxy.go @@ -52,7 +52,7 @@ func NewProtoProxy[T proto.Message](refreshInterval time.Duration, updateFn func refreshInterval: refreshInterval, queueUpdate: make(chan struct{}, 1), } - p.performUpdate(true) + p.performUpdate(true, time.Now()) if refreshInterval > 0 { go p.worker() } @@ -105,7 +105,7 @@ func (p *ProtoProxy[T]) Stop() { } } -func (p *ProtoProxy[T]) performUpdate(skipNotify bool) bool { +func (p *ProtoProxy[T]) performUpdate(skipNotify bool, refreshTime time.Time) bool { // set dirty back *before* calling updateFn because otherwise it could // wipe out another thread setting dirty to true while updateFn is executing p.lock.Lock() @@ -126,6 +126,9 @@ func (p *ProtoProxy[T]) performUpdate(skipNotify bool) bool { return false } p.message = msg + // only updating refreshedAt if we have notified, so it shouldn't push + // out the next notification out by another interval + p.refreshedAt = refreshTime p.lock.Unlock() if !skipNotify { @@ -151,20 +154,10 @@ func (p *ProtoProxy[T]) worker() { shouldUpdate := p.dirty && time.Since(p.refreshedAt) >= p.refreshInterval p.lock.RUnlock() if shouldUpdate { - if p.performUpdate(false) { - // only updating refreshedAt if we have notified, so it shouldn't push - // out the next notification out by another interval - p.lock.Lock() - p.refreshedAt = now - p.lock.Unlock() - } + p.performUpdate(false, now) } case <-p.queueUpdate: - if p.performUpdate(false) { - p.lock.Lock() - p.refreshedAt = time.Now() - p.lock.Unlock() - } + p.performUpdate(false, time.Now()) } } }