-
Notifications
You must be signed in to change notification settings - Fork 79
/
httputil.go
60 lines (54 loc) · 1.13 KB
/
httputil.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
package httputil
import (
"io/ioutil"
"net/http"
"time"
"go.uber.org/zap"
)
// CheckGet retries until HTTP response returns the expected output.
func CheckGet(lg *zap.Logger, u, exp string, retries int, interval time.Duration, stopc chan struct{}) bool {
for retries > 0 {
select {
case <-stopc:
return false
default:
}
resp, err := http.Get(u)
if err != nil {
lg.Warn(
"HTTP Get failed",
zap.String("endpoint", u),
zap.Error(err),
)
retries--
time.Sleep(interval)
continue
}
d, err := ioutil.ReadAll(resp.Body)
if err != nil {
lg.Warn(
"failed to read from HTTP Response",
zap.String("endpoint", u),
zap.Error(err),
)
retries--
time.Sleep(interval)
continue
}
resp.Body.Close()
if exp != "" && string(d) != exp {
lg.Warn(
"unexpected data from HTTP Response",
zap.String("endpoint", u),
zap.Int("expected-bytes", len(exp)),
zap.Int("response-bytes", len(d)),
)
retries--
time.Sleep(interval)
continue
}
lg.Info("HTTP Get success", zap.String("endpoint", u), zap.Int("response-size", len(d)))
return true
}
return false
}