/
service_request.go
155 lines (126 loc) · 4.29 KB
/
service_request.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package deployment_service_checker
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
)
// RequestResult represents a HTTP request result.
type RequestResult struct {
Response *http.Response
Err error
}
// makeRequestToDeploymentCheckService attempts to make an HTTP request to the service
// hostname and returns a boolean value. Returns a chan of errors.
func makeRequestToDeploymentCheckService(ctx context.Context, client *kubernetes.Clientset) error {
// service ip
hostname := getServiceClusterIP(ctx, client)
requestChan := make(chan error)
go func() {
log.Infoln("Looking for a response from the endpoint.")
// Init a timeout for request backoffs. Assume that this should not take more than 3m.
backoffTimeout := time.Minute * 3
timeoutChan := time.After(backoffTimeout)
log.Debugln("Setting timeout for backoff loop to:", backoffTimeout)
defer close(requestChan)
if len(hostname) == 0 {
err := fmt.Errorf("given blank hostname for service load balancer endpoint -- skipping HTTP call")
requestChan <- err
return
}
// Prepend the hostname with a HTTP protocol.
if !strings.HasPrefix(hostname, "http://") {
hostname = "http://" + hostname
}
// Try to make requests to the hostname endpoint and wait for a result.
select {
case result := <-getRequestBackoff(hostname):
if &result == nil {
requestChan <- errors.New("got a nil request result from the backoff process")
return
}
if result.Err != nil {
requestChan <- result.Err
return
}
if result.Response == nil {
err := fmt.Errorf("could not get a response from the given address: %s", hostname)
requestChan <- err
return
}
if result.Response.StatusCode != http.StatusOK {
requestChan <- result.Err
return
}
log.Infoln("Got a result from", http.MethodGet, "request backoff:", result.Response.Status)
requestChan <- nil
case <-timeoutChan:
err := fmt.Errorf("backoff loop for a 200 response took too long and timed out")
requestChan <- err
case <-ctx.Done():
err := fmt.Errorf("context expired while waiting for a status: %v form hostname: %s", http.StatusOK, hostname)
requestChan <- err
}
}()
return <-requestChan
}
// getRequestBackoff returns a channel that reports the result of a GET backoff loop from the hostname endpoint.
func getRequestBackoff(hostname string) chan RequestResult {
requestResultChan := make(chan RequestResult)
// Make requests to the hostname endpoint in the background.
go func() {
defer close(requestResultChan)
requestResult := RequestResult{}
// Backoff time = attempts * 5 seconds.
retrySleep := func(t int) {
log.Infoln("Retrying in", t*5, "seconds.")
time.Sleep(time.Duration(t) * 5 * time.Second)
}
// Backoff loop here for HTTP GET request.
attempts := 1
maxRetries := 10
log.Infoln("Beginning backoff loop for HTTP", http.MethodGet, "request.")
err := errors.New("") // Set err to something that is not nil to start the following loop.
for err != nil { // Loop on http.Get() errors.
if attempts > maxRetries {
log.Infoln("Could not successfully make an HTTP request after", attempts, "attempts.")
requestResult.Err = err
requestResultChan <- requestResult
return
}
log.Debugln("Making", http.MethodGet, "to", hostname)
var resp *http.Response
resp, err = http.Get(hostname)
if err == nil && resp.StatusCode == http.StatusOK {
log.Infoln("Successfully made an HTTP request on attempt:", attempts)
log.Infoln("Got a", resp.StatusCode, "with a", http.MethodGet, "to", hostname)
closeErr := resp.Body.Close()
if closeErr != nil {
log.Debugln("Failed to close response body:", closeErr.Error())
}
requestResult.Response = resp
requestResultChan <- requestResult
return
}
if err != nil && !strings.Contains(err.Error(), "no such host") {
log.Debugln("An error occurred making a", http.MethodGet, "request:", err)
}
if resp != nil {
log.Debugln("Got a", resp.StatusCode)
closeErr := resp.Body.Close()
if closeErr != nil {
log.Debugln("Failed to close response body:", closeErr.Error())
}
}
retrySleep(attempts)
attempts++
}
requestResultChan <- requestResult
return
}()
return requestResultChan
}