Repository navigation
Expand file tree
/
Copy pathrequest.go
More file actions
70 lines (67 loc) · 1.55 KB
/
Copy pathrequest.go
File metadata and controls
70 lines (67 loc) · 1.55 KB
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 main
import (
"context"
"log"
"net/http"
"time"
)
func (opt *Opt) runWaitFor(ctx context.Context, client *http.Client) (string, int) {
consecutive := opt.Consecutive - 1
requestNum := 0
for ctx.Err() == nil {
requestNum++
okMsg, errReq := opt.Request(ctx, client)
interval := opt.Interim
if errReq == nil && consecutive <= 0 {
log.Printf("request[%d]: %s", requestNum, okMsg)
return okMsg, OK
} else if errReq == nil {
consecutive--
log.Printf("request[%d]: %s", requestNum, okMsg)
} else {
interval = opt.WaitForInterval
consecutive = opt.Consecutive - 1
log.Printf("request[%d]: %s", requestNum, errReq.Error())
}
timer := time.NewTimer(interval)
select {
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
}
}
return "Give up waiting for success", UNKNOWN
}
func (opt *Opt) runRequest(ctx context.Context, client *http.Client) (string, int) {
consecutive := opt.Consecutive - 1
requestNum := 0
var rErr *RequestError
for ctx.Err() == nil {
var okMsg string
requestNum++
okMsg, rErr = opt.Request(ctx, client)
if rErr == nil && consecutive <= 0 {
log.Printf("request[%d]: %s", requestNum, okMsg)
return okMsg, OK
} else if rErr == nil {
consecutive--
log.Printf("request[%d]: %s", requestNum, okMsg)
} else {
break
}
timer := time.NewTimer(opt.Interim)
select {
case <-ctx.Done():
if !timer.Stop() {
<-timer.C
}
case <-timer.C:
}
}
if rErr == nil {
return "HTTP UNKNOWN - timeout", UNKNOWN
}
return rErr.Error(), rErr.Code()
}