-
Notifications
You must be signed in to change notification settings - Fork 14
/
grpc.go
166 lines (140 loc) · 5.14 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package core
import (
"context"
"errors"
"fmt"
"net"
"github.com/anz-bank/sysl-go/config"
"github.com/anz-bank/sysl-go/handlerinitialiser"
"github.com/anz-bank/sysl-go/log"
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/reflection"
)
// Deprecated: prefer GrpcServerManager.
type GrpcManager interface {
Interceptors() []grpc.UnaryServerInterceptor
EnabledGrpcHandlers() []handlerinitialiser.GrpcHandlerInitialiser
GrpcAdminServerConfig() *config.CommonServerConfig
GrpcPublicServerConfig() *config.GRPCServerConfig
}
type GrpcServerManager struct {
GrpcServerOptions []grpc.ServerOption
EnabledGrpcHandlers []handlerinitialiser.GrpcHandlerInitialiser
GrpcPublicServerConfig *config.GRPCServerConfig
}
func DefaultGrpcServerOptions(ctx context.Context, grpcPublicServerConfig *config.GRPCServerConfig) ([]grpc.ServerOption, error) {
opts, err := config.ExtractGrpcServerOptions(ctx, grpcPublicServerConfig)
if err != nil {
return nil, err
}
logger := log.GetLogger(ctx)
// Inject the logger into the ctx so we can log when we're serving rpc calls.
opts = append(opts, grpc.ChainUnaryInterceptor(makeLoggerInterceptor(logger)))
opts = append(opts, grpc.ChainUnaryInterceptor(TraceidLogInterceptor))
return opts, nil
}
func newGrpcServerManagerFromGrpcManager(ctx context.Context, hl GrpcManager) (*GrpcServerManager, error) {
opts, err := extractGrpcServerOptionsFromGrpcManager(ctx, hl)
if err != nil {
return nil, err
}
return &GrpcServerManager{
GrpcServerOptions: opts,
EnabledGrpcHandlers: hl.EnabledGrpcHandlers(),
GrpcPublicServerConfig: hl.GrpcPublicServerConfig(),
}, nil
}
func extractGrpcServerOptionsFromGrpcManager(ctx context.Context, hl GrpcManager) ([]grpc.ServerOption, error) {
opts, err := config.ExtractGrpcServerOptions(ctx, hl.GrpcPublicServerConfig())
if err != nil {
return nil, err
}
opts = append(opts, grpc.ChainUnaryInterceptor(hl.Interceptors()...))
opts = append(opts, grpc.ChainUnaryInterceptor(makeLoggerInterceptor(log.GetLogger(ctx))))
opts = append(opts, grpc.ChainUnaryInterceptor(TraceidLogInterceptor)) // seems wrong to have this last in chain, but that was old behaviour.
return opts, nil
}
func configurePublicGrpcServerListener(ctx context.Context, m GrpcServerManager, hooks *Hooks) StoppableServer {
server := grpc.NewServer(m.GrpcServerOptions...)
cfg := config.GetDefaultConfig(ctx)
if cfg != nil && cfg.GenCode.Upstream.GRPC.EnableReflection {
reflection.Register(server)
}
// Not sure if it is possible to register multiple servers
for _, h := range m.EnabledGrpcHandlers {
h.RegisterServer(ctx, server)
}
if (hooks == nil) || (hooks.ShouldSetGrpcGlobalLogger == nil) || hooks.ShouldSetGrpcGlobalLogger() {
setLogger(ctx)
}
prepareGrpcServerListenerFn := prepareGrpcServerListener
if hooks != nil && hooks.StoppableGrpcServerBuilder != nil {
prepareGrpcServerListenerFn = hooks.StoppableGrpcServerBuilder
}
return prepareGrpcServerListenerFn(ctx, server, *m.GrpcPublicServerConfig, "gRPC Public server")
}
func setLogger(ctx context.Context) {
logger := log.GetLogger(ctx)
grpclog.SetLoggerV2(
grpclog.NewLoggerV2(
&logWriterInfo{logger: logger},
&logWriterInfo{logger: logger},
&logWriterError{logger: logger}))
}
type grpcServer struct {
ctx context.Context
cfg config.GRPCServerConfig
server *grpc.Server
name string
}
func (s grpcServer) Start() error {
if s.cfg.TLS != nil {
log.Infof(s.ctx, "TLS configuration present. Preparing to serve gRPC/HTTPS for address: %s:%d", s.cfg.HostName, s.cfg.Port)
} else {
log.Infof(s.ctx, "TLS configuration NOT present. Preparing to serve gRPC/HTTP for address: %s:%d", s.cfg.HostName, s.cfg.Port)
}
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.cfg.HostName, s.cfg.Port))
if err != nil {
return err
}
return s.server.Serve(lis)
}
func (s grpcServer) GracefulStop() error {
s.server.GracefulStop()
return nil
}
func (s grpcServer) Stop() error {
s.server.Stop()
return nil
}
func (s grpcServer) GetName() string {
return s.name
}
type logWriterInfo struct {
logger log.Logger
}
func (lw *logWriterInfo) Write(p []byte) (n int, err error) {
lw.logger.Info(string(p))
return len(p), nil
}
type logWriterError struct {
logger log.Logger
}
func (lw *logWriterError) Write(p []byte) (n int, err error) {
lw.logger.Error(errors.New(string(p)), "gRPC error")
return len(p), nil
}
func prepareGrpcServerListener(ctx context.Context, server *grpc.Server, commonConfig config.GRPCServerConfig, name string) StoppableServer {
log.Infof(ctx, "configured gRPC listener for address: %s:%d", commonConfig.HostName, commonConfig.Port)
return grpcServer{ctx: ctx, cfg: commonConfig, server: server, name: name}
}
func makeLoggerInterceptor(logger log.Logger) grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx = log.PutLogger(ctx, logger)
return handler(ctx, req)
}
}
func TraceidLogInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
return handler(log.WithStr(ctx, "traceid", "traceid"), req)
}