-
Notifications
You must be signed in to change notification settings - Fork 7
/
readiness.go
60 lines (46 loc) · 965 Bytes
/
readiness.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
package main
import (
"net"
"net/http"
)
type ReadinessCreator = func(addr string, readinessConf *ReadinessConf) Readiness
type Readiness interface {
// check if the backend is ready
IsReady() bool
}
type NullReadiness struct {
}
func NewNullReadiness() *NullReadiness {
return &NullReadiness{}
}
func (n *NullReadiness) IsReady() bool {
return true
}
type TcpReadiness struct {
addr string
}
func NewTcpReadiness(addr string) *TcpReadiness {
return &TcpReadiness{addr: addr}
}
func (t *TcpReadiness) IsReady() bool {
conn, err := net.Dial("tcp", t.addr)
if err != nil {
return false
}
defer conn.Close()
return true
}
type HttpReadiness struct {
url string
}
func NewHttpReadiness(url string) *HttpReadiness {
return &HttpReadiness{url: url}
}
func (h *HttpReadiness) IsReady() bool {
resp, err := http.Get(h.url)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode >= 200 && resp.StatusCode < 400
}