/
retryFetcher.go
76 lines (62 loc) · 2.09 KB
/
retryFetcher.go
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
71
72
73
74
75
76
package tracker
import (
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/zapproject/pythia/util"
)
// Client utilized for all HTTP requests
var client http.Client
func init() {
client = http.Client{}
}
var retryFetchLog = util.NewLogger("tracker", "FetchWithRetries")
//FetchRequest holds info for a request
// TODO: add mock fetch
type FetchRequest struct {
queryURL string
timeout time.Duration
}
func fetchWithRetries(req *FetchRequest) ([]byte, error) {
return _recFetch(req, clck.Now().Add(req.timeout))
}
func _recFetch(req *FetchRequest, expiration time.Time) ([]byte, error) {
retryFetchLog.Debug("Fetch request will expire at: %v (timeout: %v)", expiration, req.timeout)
now := clck.Now()
client.Timeout = expiration.Sub(now)
r, err := client.Get(req.queryURL)
if err != nil {
//log local non-timeout errors for now
retryFetchLog.Warn("Problem fetching data from: %s. %v", req.queryURL, err)
now := clck.Now()
if now.After(expiration) {
retryFetchLog.Error("Timeout expired, not retrying query and passing error up")
return nil, err
}
//FIXME: should this be configured as fetch error sleep duration?
time.Sleep(500 * time.Millisecond)
//try again
retryFetchLog.Warn("Trying fetch again...")
return _recFetch(req, expiration)
}
data, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
return nil, fmt.Errorf("failed to read response body: %v", err)
}
if r.StatusCode < 200 || r.StatusCode > 299 {
retryFetchLog.Warn("Response from fetching %s. Response code: %d, payload: %s", req.queryURL, r.StatusCode, data)
//log local non-timeout errors for now
// this is a duplicated error that is unlikely to be triggered since expiration is updated above
now := clck.Now()
if now.After(expiration) {
return nil, fmt.Errorf("giving up fetch request after request timeout: %d", r.StatusCode)
}
//FIXME: should this be configured as fetch error sleep duration?
time.Sleep(500 * time.Millisecond)
//try again
return _recFetch(req, expiration)
}
return data, nil
}