blob: de37c231afdb8a28a8b639da371dc5eab7d8ce35 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package cache
import (
"context"
"errors"
"time"
log "github.com/sirupsen/logrus"
"gitlab.com/gitlab-org/gitlab-pages/internal/source/gitlab/api"
)
// Retriever is an utility type that performs an HTTP request with backoff in
// case of errors
type Retriever struct {
client api.Client
retrievalTimeout time.Duration
maxRetrievalInterval time.Duration
maxRetrievalRetries int
}
// NewRetriever creates a Retriever with a client
func NewRetriever(client api.Client, retrievalTimeout, maxRetrievalInterval time.Duration, maxRetrievalRetries int) *Retriever {
return &Retriever{
client: client,
retrievalTimeout: retrievalTimeout,
maxRetrievalInterval: maxRetrievalInterval,
maxRetrievalRetries: maxRetrievalRetries,
}
}
// Retrieve retrieves a lookup response from external source with timeout and
// backoff. It has its own context with timeout.
func (r *Retriever) Retrieve(domain string) (lookup api.Lookup) {
ctx, cancel := context.WithTimeout(context.Background(), r.retrievalTimeout)
defer cancel()
select {
case <-ctx.Done():
log.Debug("retrieval context done")
lookup = api.Lookup{Error: errors.New("retrieval context done")}
case lookup = <-r.resolveWithBackoff(ctx, domain):
log.Debug("retrieval response sent")
}
return lookup
}
func (r *Retriever) resolveWithBackoff(ctx context.Context, domain string) <-chan api.Lookup {
response := make(chan api.Lookup)
go func() {
var lookup api.Lookup
for i := 1; i <= r.maxRetrievalRetries; i++ {
lookup = r.client.GetLookup(ctx, domain)
if lookup.Error != nil {
time.Sleep(r.maxRetrievalInterval)
} else {
break
}
}
response <- lookup
close(response)
}()
return response
}
|