fork of https://github.com/sourcegraph/zoekt
1package main
2
3import (
4 "github.com/sourcegraph/log"
5 "time"
6)
7
8type backoff struct {
9 // maxBackoff is the longest duration we will backoff indexing operations of the given repo.
10 maxBackoff time.Duration
11 // backoffDuration is used to determine the duration of backoff. consecutiveFailures * backoffDuration calculates the
12 // duration set on failed indexing attempt.
13 backoffDuration time.Duration
14 // consecutiveFailures is the count of preceding consecutive failures.
15 consecutiveFailures int
16 // backOffUntil is the earliest time when we allow the item to be pushed to the heap. Until then the item will not be enqueued
17 // and indexing will not be attempted.
18 backoffUntil time.Time
19}
20
21func (b *backoff) Allow(now time.Time) bool {
22 return b.backoffUntil.Before(now)
23}
24
25func (b *backoff) Reset() {
26 b.consecutiveFailures = 0
27 b.backoffUntil = time.Unix(0, 0)
28}
29
30func (b *backoff) Fail(now time.Time, logger log.Logger, opts IndexOptions) {
31 backoffDuration := time.Duration(b.consecutiveFailures+1) * b.backoffDuration
32
33 if backoffDuration > b.maxBackoff {
34 backoffDuration = b.maxBackoff
35 } else {
36 b.consecutiveFailures++
37 }
38 b.backoffUntil = now.Add(backoffDuration)
39
40 logger.Debug("Backoff subsequent attempts to index repository",
41 log.String("repo", opts.Name),
42 log.Uint32("id", opts.RepoID),
43 log.Duration("backoff_duration", b.backoffDuration),
44 log.Time("backoff_until", b.backoffUntil),
45 )
46}