-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalive_guard.go
46 lines (38 loc) · 1016 Bytes
/
alive_guard.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
package simple_k8s
import (
"sync"
"time"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
type AliveGuard struct {
mutex sync.Mutex
healthCheckServer *health.Server
timeThreshold time.Duration
serviceName string
failed bool
}
func NewAliveGuard(healtServer *health.Server, threshold time.Duration, serviceName string) *AliveGuard {
return &AliveGuard{
healthCheckServer: healtServer,
timeThreshold: threshold,
serviceName: serviceName,
failed: false,
}
}
func (g *AliveGuard) CheckTimeDuration(startTime time.Time) {
if g.failed {
return
}
if time.Since(startTime) > g.timeThreshold {
g.mutex.Lock()
defer g.mutex.Unlock()
if !g.failed {
g.failed = true
g.healthCheckServer.SetServingStatus(g.serviceName, healthpb.HealthCheckResponse_NOT_SERVING)
}
}
}
func GetAppAliveGuard(threshold time.Duration) *AliveGuard {
return NewAliveGuard(healthCheckServer, threshold, "")
}