-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
57 lines (46 loc) · 1.08 KB
/
grpc.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
package middleware
import (
"context"
"fmt"
"github.com/kayx-org/freja/component"
"github.com/kayx-org/freja/healthcheck"
"net"
)
type grpcMiddleware struct {
service *component.GRPCServer
listener *net.Listener
status healthcheck.ServiceStatus
}
func NewGRPCMiddleware(service *component.GRPCServer) *grpcMiddleware {
return &grpcMiddleware{
service: service,
status: healthcheck.UP,
}
}
func (m *grpcMiddleware) Init() error {
lis, err := net.Listen("tcp", m.service.ListenAddress())
if err != nil {
return fmt.Errorf("unable to bind listener: %w", err)
}
m.listener = &lis
return nil
}
func (m *grpcMiddleware) Run(context.Context) error {
defer func() {
m.status = healthcheck.DOWN
}()
if err := m.service.Server().Serve(*m.listener); err != nil {
return fmt.Errorf("unabel to serve: %w", err)
}
return nil
}
func (m *grpcMiddleware) Stop(context.Context) error {
m.service.Server().GracefulStop()
return nil
}
func (m *grpcMiddleware) Name() string {
return "grpc"
}
func (m *grpcMiddleware) Status() healthcheck.ServiceStatus {
return m.status
}