blob: c0be68e0dcffe3a93ab9e7e829dca9684cd74ab0 (
plain)
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
|
package health
import (
"context"
"net/http"
"sync"
rrhttp "github.com/spiral/roadrunner/service/http"
)
// ID declares the public service name
const ID = "health"
// Service to serve an endpoint for checking the health of the worker pool
type Service struct {
cfg *Config
mu sync.Mutex
http *http.Server
httpService *rrhttp.Service
}
// Init health service
func (s *Service) Init(cfg *Config, r *rrhttp.Service) (bool, error) {
// Ensure the httpService is set
if r == nil {
return false, nil
}
s.cfg = cfg
s.httpService = r
return true, nil
}
// Serve the health endpoint
func (s *Service) Serve() error {
// Configure and start the http server
s.mu.Lock()
s.http = &http.Server{Addr: s.cfg.Address, Handler: s}
s.mu.Unlock()
return s.http.ListenAndServe()
}
// Stop the health endpoint
func (s *Service) Stop() {
s.mu.Lock()
defer s.mu.Unlock()
if s.http != nil {
// gracefully stop the server
go s.http.Shutdown(context.Background())
}
}
// ServeHTTP returns the health of the pool of workers
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
status := http.StatusOK
if !s.isHealthy() {
status = http.StatusInternalServerError
}
w.WriteHeader(status)
}
// isHealthy checks the server, pool and ensures at least one worker is active
func (s *Service) isHealthy() bool {
httpService := s.httpService
if httpService == nil {
return false
}
server := httpService.Server()
if server == nil {
return false
}
pool := server.Pool()
if pool == nil {
return false
}
// Ensure at least one worker is active
for _, w := range pool.Workers() {
if w.State().IsActive() {
return true
}
}
return false
}
|