Skip to content

Commit

Permalink
remove time from the interval predictor
Browse files Browse the repository at this point in the history
It makes it harder to test.
  • Loading branch information
Stebalien committed Jun 28, 2024
1 parent 332ab28 commit 70f08c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
17 changes: 3 additions & 14 deletions certexchange/polling/predictor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func newPredictor(minInterval, defaultInterval, maxInterval time.Duration) *pred
type predictor struct {
minInterval, maxInterval time.Duration

next time.Time
interval time.Duration
wasIncreasing bool
exploreDistance time.Duration
Expand Down Expand Up @@ -88,21 +87,11 @@ func (p *predictor) update(progress uint64) time.Duration {
}

// Apply either the backoff or predicted the interval.
nextInterval := p.interval
if p.backoff > 0 {
nextInterval = p.backoff
p.backoff = min(2*p.backoff, maxBackoffMultiplier*p.maxInterval)
p.next = p.next.Add(p.backoff)
} else {
p.next = p.next.Add(p.interval)
}
return nextInterval

// Polling takes time. If we run behind, predict that we should poll again immediately. We
// enforce a minimum interval above so this shouldn't be too often.
now := time.Now()
prediction := p.next.Sub(now)
if prediction < 0 {
p.next = now
return 0
}

return prediction
}
19 changes: 10 additions & 9 deletions certexchange/polling/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *Subscriber) run(ctx context.Context, discoveredPeers <-chan peer.ID, po
select {
case p := <-discoveredPeers:
s.peerTracker.peerSeen(p)
case <-timer.C:
case pollTime := <-timer.C:
// First, see if we made progress locally. If we have, update
// interval prediction based on that local progress. If our interval
// was accurate, we'll keep predicting the same interval and we'll
Expand All @@ -159,16 +159,17 @@ func (s *Subscriber) run(ctx context.Context, discoveredPeers <-chan peer.ID, po
if err != nil {
return err
}
if progress > 0 {
timer.Reset(predictor.update(progress))
break
// Otherwise, poll the network.
if progress == 0 {
progress, err = s.poll(ctx, poller)
if err != nil {
return err
}
}

progress, err = s.poll(ctx, poller)
if err != nil {
return err
}
timer.Reset(predictor.update(progress))
nextInterval := predictor.update(progress)
nextPollTime := pollTime.Add(nextInterval)
timer.Reset(max(time.Until(nextPollTime), 0))
case <-ctx.Done():
return ctx.Err()
}
Expand Down

0 comments on commit 70f08c1

Please sign in to comment.