Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
boks1971 committed Oct 16, 2024
1 parent 687816a commit d30dc8f
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 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) 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()
Expand All @@ -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 {
Expand All @@ -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())
}
}
}

0 comments on commit d30dc8f

Please sign in to comment.