-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path07-desired-request-sender.go
61 lines (42 loc) · 1006 Bytes
/
07-desired-request-sender.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
package main
import (
"io/ioutil"
"net/http"
"strings"
"time"
)
func desiredRequestSender(context *context) {
context.log("07-desired-request-sender")
desiredRequest := context.SpecTriplet.DesiredRequest
body := ioutil.NopCloser(strings.NewReader(desiredRequest.Body()))
request, err := http.NewRequest(
desiredRequest.Method(),
desiredRequest.URL(),
body,
)
if errorHandler(context, err) {
return
}
for _, headerLine := range context.SpecTriplet.DesiredRequest.HeaderLines {
parts := strings.Split(headerLine.Text, ":")
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
request.Header.Add(key, value)
}
context.SpecTriplet.StartedAt = time.Now()
attempt := 0
for {
context.HTTPResponse, err = context.HTTPClient.Do(request)
attempt++
if err == nil {
break
}
if attempt >= context.MaxHTTPAttempts {
context.Err = err
return
}
time.Sleep(context.HTTPRetryDelay)
continue
}
actualResponseReceiver(context)
}