-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
141 lines (116 loc) · 3.18 KB
/
main.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
package main
import (
"flag"
"fmt"
"net/url"
"os"
"strings"
"github.com/golang/glog"
"github.com/karlkfi/probe/http"
"github.com/karlkfi/probe/tcp"
"time"
)
func main() {
flagSet := flag.CommandLine
c := parseFlags(flagSet)
defer glog.Flush()
glog.V(1).Info("Executing: ", strings.Join(os.Args, " "))
// non-flag args
args := flagSet.Args()
if len(args) == 0 {
fmt.Fprint(os.Stderr, "Error: No address specified\n")
flagSet.Usage()
os.Exit(2)
}
if len(args) > 1 {
fmt.Fprint(os.Stderr, "Error: Too many arguments specified\n")
flagSet.Usage()
os.Exit(2)
}
addrArg := args[0]
addrURL, err := url.ParseRequestURI(addrArg)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Invalid address %q - Expected valid URL\n", addrArg)
os.Exit(2)
}
if *c.maxAttempts == 0 {
fmt.Fprintf(os.Stderr, "Error: Invalid max-attempts %q - Expected an int > 0 or exactly -1 (unlimited)\n", addrArg)
os.Exit(2)
}
if *c.retryDelay < 0 {
fmt.Fprintf(os.Stderr, "Error: Invalid retry-delay %q - Expected a duration >= 0\n", addrArg)
os.Exit(2)
}
if *c.timeout == 0 {
fmt.Fprintf(os.Stderr, "Error: Invalid timeout %q - Expected an duration > 0 or exactly -1 (unlimited)\n", addrArg)
os.Exit(2)
}
if *c.attemptTimeout == 0 {
fmt.Fprintf(os.Stderr, "Error: Invalid attempt-timeout %q - Expected an duration > 0 or exactly -1 (unlimited)\n", addrArg)
os.Exit(2)
}
var prober Prober
var address string
switch addrURL.Scheme {
case SchemeTCP:
dialer := tcp.NewDialer()
if *c.attemptTimeout > 0 {
dialer.Timeout = *c.attemptTimeout
}
prober = tcp.NewProber(dialer)
address = addrURL.Host
case SchemeHTTP:
fallthrough
case SchemeHTTPS:
client := http.NewInsecureClient()
if *c.attemptTimeout > 0 {
client.Timeout = *c.attemptTimeout
}
prober = http.NewProber(client)
address = addrURL.String()
default:
fmt.Fprint(os.Stderr, "Error: No probable address scheme specified - Expected \"tcp\", \"http\", or \"https\"\n")
flagSet.Usage()
os.Exit(2)
}
var exitTimer *time.Timer
if *c.timeout >= 0 {
exitTimer = time.NewTimer(*c.timeout)
go func() {
_, ok := <-exitTimer.C
if ok {
// channel still open means timeout occurred
fmt.Fprintf(os.Stderr, "Error: Timed out after %v\n", *c.timeout)
// main goroutine will be killed by timeout exit.
// http client & dialer don't seem to be interruptable, or we might do that instead.
os.Exit(1)
}
// otherwise timer was stopped
}()
}
err = makeAttempts(prober, address, *c.maxAttempts, *c.retryDelay)
if exitTimer != nil {
exitTimer.Stop()
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
func makeAttempts(prober Prober, address string, maxAttempts int, retryDelay time.Duration) error {
probeErr := prober.Probe(address)
attemptsMade := 1
glog.V(2).Infof("Attempt %d Failed: %v", attemptsMade, probeErr)
for probeErr != nil && (maxAttempts < 0 || maxAttempts > attemptsMade) {
glog.V(3).Infof("Sleeping %s", retryDelay)
time.Sleep(retryDelay)
probeErr = prober.Probe(address)
attemptsMade++
glog.V(2).Infof("Attempt %d Failed: %v", attemptsMade, probeErr)
}
if probeErr != nil {
return probeErr
}
return nil
}