-
Notifications
You must be signed in to change notification settings - Fork 10
/
grpc.go
70 lines (58 loc) · 2.06 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
58
59
60
61
62
63
64
65
66
67
68
69
70
package utils
import (
"context"
"fmt"
"net"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
const (
maxStreams = 100000
streamKeepAliveDuration = 60 * time.Second
)
// NewGrpc creates a new gRPC server
func NewGrpc(serverType string, port int, certPem, keyPem, rootCertPem []byte) (*grpc.Server, net.Listener, error) {
log.Info().Msgf("Setting up %s gRPC server...", serverType)
addr := fmt.Sprintf(":%d", port)
lis, err := net.Listen("tcp", addr)
if err != nil {
log.Error().Err(err).Msgf("Error starting %s gRPC server on %s", serverType, addr)
return nil, nil, err
}
log.Debug().Msgf("Parameters for %s gRPC server: MaxConcurrentStreams=%d; KeepAlive=%+v", serverType, maxStreams, streamKeepAliveDuration)
grpcOptions := []grpc.ServerOption{
grpc.MaxConcurrentStreams(maxStreams),
grpc.KeepaliveParams(keepalive.ServerParameters{
Time: streamKeepAliveDuration,
}),
}
mutualTLS, err := setupMutualTLS(false, serverType, certPem, keyPem, rootCertPem)
if err != nil {
log.Error().Err(err).Msg("Error setting up mutual tls for GRPC server")
return nil, nil, err
}
grpcOptions = append(grpcOptions, mutualTLS)
return grpc.NewServer(grpcOptions...), lis, nil
}
// GrpcServe starts the gRPC server passed.
func GrpcServe(ctx context.Context, grpcServer *grpc.Server, lis net.Listener, cancel context.CancelFunc, serverType string, errorCh chan interface{}) {
go func() {
if err := grpcServer.Serve(lis); err != nil {
log.Error().Err(err).Msgf("[grpc][%s] Error serving gRPC request", serverType)
if errorCh != nil {
errorCh <- err
}
}
cancel()
}()
log.Info().Msgf("[grpc][%s] Started server on: %s", serverType, lis.Addr())
<-ctx.Done()
log.Info().Msgf("[grpc][%s] stopping %s server", serverType, serverType)
if grpcServer != nil {
log.Info().Msgf("[grpc][%s] Gracefully stopping %s gRPC server", serverType, serverType)
grpcServer.GracefulStop()
log.Info().Msgf("[grpc][%s] gRPC Server stopped", serverType)
}
log.Info().Msgf("[grpc][%s] exiting %s gRPC server", serverType, serverType)
}