Skip to content

Commit

Permalink
More precise proto proxy timing. (#859)
Browse files Browse the repository at this point in the history
* More precise proto proxy timing.

It is possible that update function takes some time. Due to that the
next cycle could see less than refresh interval since refresh and skip a
cycle.

That could end up adding an extra cycle.

* clean up

* clean up
  • Loading branch information
boks1971 authored Oct 16, 2024
1 parent d16f740 commit b7c4dc5
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions utils/protoproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func (p *ProtoProxy[T]) Stop() {
}
}

func (p *ProtoProxy[T]) performUpdate(skipNotify bool) {
func (p *ProtoProxy[T]) performUpdate(skipNotify bool, refreshTime time.Time) {
// 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()
Expand All @@ -128,7 +128,7 @@ func (p *ProtoProxy[T]) performUpdate(skipNotify bool) {
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 = time.Now()
p.refreshedAt = refreshTime
p.lock.Unlock()

if !skipNotify {
Expand All @@ -148,15 +148,15 @@ func (p *ProtoProxy[T]) worker() {
select {
case <-p.fuse.Watch():
return
case <-ticker.C:
case now := <-ticker.C:
p.lock.RLock()
shouldUpdate := p.dirty && time.Since(p.refreshedAt) > p.refreshInterval
shouldUpdate := p.dirty && time.Since(p.refreshedAt) >= p.refreshInterval
p.lock.RUnlock()
if shouldUpdate {
p.performUpdate(false)
p.performUpdate(false, now)
}
case <-p.queueUpdate:
p.performUpdate(false)
p.performUpdate(false, time.Now())
}
}
}

0 comments on commit b7c4dc5

Please sign in to comment.