-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhealth_check.go
More file actions
72 lines (65 loc) · 1.81 KB
/
Copy pathhealth_check.go
File metadata and controls
72 lines (65 loc) · 1.81 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
71
72
package http_lb
import (
"fmt"
"go.uber.org/zap"
"net/http"
"time"
)
var _ HealthChecker = (*HealthCheck)(nil)
var _ GracefulShutdown = (*HealthCheck)(nil)
func NewHealthCheck(endPoint string, interval time.Duration, serverPool ServerPool,
expectedStatusCode int, client *http.Client, logger *zap.Logger) *HealthCheck {
return &HealthCheck{
endPoint: endPoint,
interval: interval,
serverPool: serverPool,
expectedStatusCode: expectedStatusCode,
client: client,
logger: logger,
shutdownCh: make(chan struct{}, 1),
}
}
type HealthCheck struct {
endPoint string
interval time.Duration
client *http.Client
serverPool ServerPool
expectedStatusCode int
logger *zap.Logger
shutdownCh chan struct{}
}
func (h *HealthCheck) Run() {
go func() {
ticker := time.NewTicker(h.interval)
for range ticker.C {
select {
case <-h.shutdownCh:
return
default:
for _, server := range h.serverPool.Servers() {
go h.check(server)
}
}
}
}()
}
func (h *HealthCheck) Shutdown() error {
h.shutdownCh <- struct{}{}
return nil
}
func (h *HealthCheck) check(server Server) {
status := Unhealthy
resp, err := h.client.Get(fmt.Sprintf("%s%s", server.Address, h.endPoint))
if err == nil && resp.StatusCode == h.expectedStatusCode {
status = Healthy
}
err = h.serverPool.SetServerStatus(server.Address, status)
if err != nil {
h.logger.Error("unexpected error for setting the server status", zap.Error(err), zap.String("server", server.Address))
}
if server.Status != status {
h.logger.Warn(fmt.Sprintf("server went %s", status), zap.String("server", server.Address))
return
}
h.logger.Warn(fmt.Sprintf("server is still %s", status), zap.String("server", server.Address))
}