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