-
Notifications
You must be signed in to change notification settings - Fork 1
/
blondie.go
175 lines (147 loc) · 3.72 KB
/
blondie.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package blondie
import (
"fmt"
"net"
"net/http"
"sync"
"time"
)
type Options struct {
PollInterval time.Duration
ExitCodeOnOk int
ExitCodeOnFail int
QuietMode bool
OutputWriter func(string)
}
func DefaultOptions() *Options {
return &Options{
PollInterval: time.Millisecond * 250,
ExitCodeOnFail: 1,
ExitCodeOnOk: 0,
QuietMode: false,
OutputWriter: func(msg string) { fmt.Println(msg) },
}
}
type netCheck struct {
host string
port int
timeout time.Duration
}
type tcpCheck struct {
netCheck
dial func(string, string, time.Duration) (net.Conn, error)
}
func NewTcpCheck(host string, port int, timeout time.Duration) DepCheck {
return &tcpCheck{
netCheck: netCheck{
host: host,
port: port,
timeout: timeout,
},
dial: net.DialTimeout,
}
}
type httpCheck struct {
netCheck
path string
successCodes []int
secure bool
get func(string) (*http.Response, error)
}
// NewHttpCheck creates a new DepCheck for a HTTP endpoint. Path may be an empty string and successCodes may be an empty slice in which case any response code will be considered a successful response
func newHttpCheck(host string, port int, timeout time.Duration, path string, successCodes []int, secure bool) DepCheck {
client := http.Client{Timeout: timeout}
return &httpCheck{
netCheck: netCheck{
host: host,
port: port,
timeout: timeout,
},
successCodes: successCodes,
path: path,
secure: secure,
get: client.Get,
}
}
func NewHttpCheck(host string, port int, timeout time.Duration, path string, successCodes []int) DepCheck {
return newHttpCheck(host, port, timeout, path, successCodes, false)
}
func NewHttpsCheck(host string, port int, timeout time.Duration, path string, successCodes []int) DepCheck {
return newHttpCheck(host, port, timeout, path, successCodes, true)
}
type DepCheck interface {
Try() bool
Timeout() time.Duration
Address() string
}
func (check *netCheck) Timeout() time.Duration {
return check.timeout
}
func (check *httpCheck) Try() bool {
protocol := "http"
if check.secure {
protocol = "https"
}
endpoint := fmt.Sprintf("%s://%s:%v/%s", protocol, check.host, check.port, check.path)
resp, err := check.get(endpoint)
if err == nil {
if len(check.successCodes) == 0 {
return true
}
for _, successCode := range check.successCodes {
if successCode == resp.StatusCode {
return true
}
}
}
return false
}
func (check *tcpCheck) Try() bool {
address := fmt.Sprintf("%s:%v", check.host, check.port)
conn, err := check.dial("tcp", address, check.timeout)
if conn != nil {
conn.Close()
}
return err == nil
}
func (check *httpCheck) Address() string {
protocol := "http"
if check.secure {
protocol = "https"
}
return fmt.Sprintf("%s://%s:%v/%s", protocol, check.host, check.port, check.path)
}
func (check *tcpCheck) Address() string {
return fmt.Sprintf("tcp://%s:%v", check.host, check.port)
}
func (options Options) Write(message string) {
if !options.QuietMode {
options.OutputWriter(message)
}
}
func WaitForDeps(deps []DepCheck, opts *Options) bool {
var waitGroup = &sync.WaitGroup{}
success := true
waitGroup.Add(len(deps))
for _, target := range deps {
opts.Write(fmt.Sprintf("Trying to connect: %s", target.Address()))
go func(target DepCheck) {
start := time.Now()
for true {
if target.Try() {
opts.Write(fmt.Sprintf("Connection OK : %s", target.Address()))
waitGroup.Done()
break
} else if time.Now().Sub(start) > target.Timeout() {
opts.Write(fmt.Sprintf("Timed out : %s", target.Address()))
success = false
waitGroup.Done()
break
}
time.Sleep(opts.PollInterval)
}
}(target)
}
waitGroup.Wait()
return success
}