-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_names.go
157 lines (132 loc) · 3.52 KB
/
health_names.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
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/url"
"time"
)
const ipKey uint8 = 0
//noOfChecks, is the no for checks for one health check
const noOfChecks = 10
//instanceHealthChecker, is health checker for instance whether it is backend or load balancer
type instanceHealthChecker interface {
performHealthCheck()
//healthCheckPerformer, Returns function which is used for health checking for each instance
healthCheckPerformer() func(context.Context, backend) <-chan error
//getTTI, returns Timeout,Thresold,Interval,
getTTI() (time.Duration, float32, time.Duration)
}
//basicHealthChecker contains to attribute to hold about a health check
type basicHealthChecker struct {
port uint
timeout time.Duration
interval time.Duration
threshold float32
}
//getTTI, returns Timeout,Thresold and Interval
func (b basicHealthChecker) getTTI() (time.Duration, float32, time.Duration) {
return b.timeout, b.threshold, b.interval
}
//generalHealthChecker, is the general health checker for instances
type generalHealthChecker struct {
basicHealthChecker
}
func (g generalHealthChecker) isHealthy() bool {
return true
}
//performHealthCheck, perfroms health of
func (g generalHealthChecker) performHealthCheck() {
}
//tcphealthchecker, is the health checker via tcp
type tcphealthchecker struct {
tcporudphealthcheck
}
//httphealthchecker, is the health checker via http
type httphealthchecker struct {
basicHealthChecker
//path, is the path of http endpoint
path string
//method, is the http method for request
method string
//niceStatus, the status code which states about healthy response
niceStatus int
}
//udphealthchecker, is the health checker via udp
type udphealthchecker struct {
tcporudphealthcheck
}
//tcporudphealthcheck, is the health checker for tcp or udp protocol
type tcporudphealthcheck struct {
basicHealthChecker
//protocol, specifies the protocol for health check 0 for tcp and 1 for udp
protocol uint
}
//Errors
var (
timeOutErr = errors.New("Request Timeout")
couldntEstablishConnection = errors.New("Couldn't establish connection")
notHealthyStatus = errors.New("Not a healthy Status")
)
func (t tcporudphealthcheck) healthCheckPerformer() func(context.Context, backend) <-chan error {
return func(con context.Context, b backend) <-chan error {
c := make(chan error)
go func() {
var pp string
if t.protocol == 1 {
pp = "udp"
} else {
pp = "tcp"
}
conn, err := net.Dial(pp, fmt.Sprint(b.IP.String(), b.Port))
if err != nil {
c <- err
return
}
defer conn.Close() //Closing the connection
var bb []byte = []byte("PING")
_, err = conn.Write(bb)
if err != nil {
c <- err
return
}
c <- nil
}()
return c
}
}
func (t tcporudphealthcheck) performHealthCheck() {
}
func (t httphealthchecker) healthCheckPerformer() func(context.Context, backend) <-chan error {
return func(con context.Context, b backend) <-chan error {
c := make(chan error)
go func() {
r := new(http.Request)
if t.method == "" {
t.method = "get"
}
r.Method = t.method
r = r.WithContext(con)
r.URL, _ = url.Parse(fmt.Sprint("http://", b.IP.String(), ":", b.Port, t.path))
cli := http.DefaultClient
//set timeout for request
resp, err := cli.Do(r)
if err != nil {
c <- err
return
//handle error
}
if resp.StatusCode != t.niceStatus {
//handle error
c <- notHealthyStatus
return
}
c <- nil //No error
}()
return c
}
}
func (t httphealthchecker) performHealthCheck() {
}