-
Notifications
You must be signed in to change notification settings - Fork 3
/
health.go
47 lines (41 loc) · 1.02 KB
/
health.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
package main
import (
"fmt"
"log"
"net"
"sync"
"golang.org/x/net/context"
"google.golang.org/grpc"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
)
var (
mut sync.Mutex
counter int = 0
)
type healthServer struct{}
func (hs *healthServer) Check(context.Context, *healthpb.HealthCheckRequest) (*healthpb.HealthCheckResponse, error) {
mut.Lock()
defer mut.Unlock()
counter++
log.Println("counter=", counter)
switch counter % 4 {
case 0:
return &healthpb.HealthCheckResponse{Status: healthpb.HealthCheckResponse_SERVING}, nil
case 1:
return &healthpb.HealthCheckResponse{Status: healthpb.HealthCheckResponse_NOT_SERVING}, nil
case 2:
return &healthpb.HealthCheckResponse{Status: healthpb.HealthCheckResponse_UNKNOWN}, nil
default:
return nil, fmt.Errorf("it is 3")
}
}
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
healthpb.RegisterHealthServer(s, &healthServer{})
log.Println("staring server")
s.Serve(lis)
}