-
Notifications
You must be signed in to change notification settings - Fork 25
/
health.go
61 lines (51 loc) · 1.79 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package health
import (
"context"
"go.uber.org/fx"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"github.com/fluxninja/aperture/pkg/config"
"github.com/fluxninja/aperture/pkg/log"
grpcclient "github.com/fluxninja/aperture/pkg/net/grpc"
)
// Module is a module that provides grpc health server for checking services status.
func Module() fx.Option {
return fx.Options(
grpcclient.ClientConstructor{Name: "health-grpc-client", ConfigKey: "health.client.grpc"}.Annotate(),
fx.Provide(provideHealthServer),
fx.Provide(fx.Annotate(
provideHealthClient,
fx.ParamTags(config.NameTag("health-grpc-client")),
)),
fx.Invoke(RegisterHealthServer),
)
}
// provideHealthServer creates instance of health server.
func provideHealthServer(lifecycle fx.Lifecycle) *health.Server {
server := health.NewServer()
lifecycle.Append(fx.Hook{
OnStop: func(context.Context) error {
server.Shutdown()
return nil
},
})
return server
}
// provideHealthClient creates instance of client to health server.
func provideHealthClient(GRPClientConnectionBuilder grpcclient.ClientConnectionBuilder) (grpc_health_v1.HealthClient, error) {
// Setup connection to health service
connWrapper := GRPClientConnectionBuilder.Build()
conn, err := connWrapper.Dial(context.Background(), "localhost:80")
if err != nil {
log.Warn().Err(err).Msg("Could not connect to health grpc server")
return nil, err
}
healthClient := grpc_health_v1.NewHealthClient(conn)
return healthClient, nil
}
// RegisterHealthServer registers health server to grpc_health_v1 api and sets default statuses.
func RegisterHealthServer(srv *grpc.Server, healthsrv *health.Server) {
// It registers empty name server implicitly
grpc_health_v1.RegisterHealthServer(srv, healthsrv)
}