-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
107 lines (88 loc) · 2.64 KB
/
server.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package go_grpc_helloworld
import (
"context"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
pb "google.golang.org/grpc/examples/helloworld/helloworld"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
"github.com/alexferl/go_grpc_helloworld/methods"
)
var (
grpcServer *grpc.Server
grpcHealthServer *grpc.Server
system = "" // empty string represents the health of the system
)
func Start() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
defer signal.Stop(sig)
g, ctx := errgroup.WithContext(ctx)
// gRPC Health Server
healthServer := health.NewServer()
g.Go(func() error {
addr := fmt.Sprintf("%s:%s", viper.GetString("health-bind-address"), viper.GetString("health-bind-port"))
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal().Msgf("health server failed to listen: '%v'", err)
}
grpcHealthServer = grpc.NewServer()
healthpb.RegisterHealthServer(grpcHealthServer, healthServer)
if viper.GetString("env-name") == "local" {
reflection.Register(grpcHealthServer)
}
log.Info().Msgf("gRPC health server serving at: %s", addr)
return grpcHealthServer.Serve(lis)
})
// gRPC server
g.Go(func() error {
addr := fmt.Sprintf("%s:%d", viper.GetString("bind-address"), viper.GetInt("bind-port"))
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Fatal().Msgf("server failed to listen: '%v'", err)
}
grpcServer = grpc.NewServer(
grpc.KeepaliveParams(keepalive.ServerParameters{MaxConnectionAge: 2 * time.Minute}),
)
pb.RegisterGreeterServer(grpcServer, &methods.Server{})
if viper.GetString("env-name") == "local" {
reflection.Register(grpcServer)
}
log.Info().Msgf("gRPC server serving at %s", addr)
healthServer.SetServingStatus(system, healthpb.HealthCheckResponse_SERVING)
return grpcServer.Serve(lis)
})
select {
case <-sig:
break
case <-ctx.Done():
break
}
cancel()
healthServer.SetServingStatus(system, healthpb.HealthCheckResponse_NOT_SERVING)
timeout := time.Duration(viper.GetInt64("graceful-timeout")) * time.Second
_, shutdownCancel := context.WithTimeout(context.Background(), timeout)
defer shutdownCancel()
if grpcServer != nil {
grpcServer.GracefulStop()
}
if grpcHealthServer != nil {
grpcHealthServer.GracefulStop()
}
err := g.Wait()
if err != nil {
log.Fatal().Msgf("wait returned error: '%v'", err)
}
}