-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
187 lines (153 loc) · 4.67 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package grpc
import (
"context"
"net"
"github.com/alexfalkowski/go-service/security"
"github.com/alexfalkowski/go-service/server"
"github.com/alexfalkowski/go-service/time"
"github.com/alexfalkowski/go-service/transport/grpc/meta"
szap "github.com/alexfalkowski/go-service/transport/grpc/telemetry/logger/zap"
"github.com/alexfalkowski/go-service/transport/grpc/telemetry/metrics"
"github.com/alexfalkowski/go-service/transport/grpc/telemetry/tracer"
tm "github.com/alexfalkowski/go-service/transport/meta"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"go.opentelemetry.io/otel/metric"
"go.uber.org/fx"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/reflection"
)
// ServerParams for gRPC.
type ServerParams struct {
fx.In
Shutdowner fx.Shutdowner
Config *Config
Logger *zap.Logger
Tracer tracer.Tracer
Meter metric.Meter
Unary []grpc.UnaryServerInterceptor
Stream []grpc.StreamServerInterceptor
}
// UnaryServerInterceptor for gRPC.
func UnaryServerInterceptor() []grpc.UnaryServerInterceptor {
return nil
}
// StreamServerInterceptor for gRPC.
func StreamServerInterceptor() []grpc.StreamServerInterceptor {
return nil
}
// Server for gRPC.
type Server struct {
Server *grpc.Server
sh fx.Shutdowner
config *Config
logger *zap.Logger
list net.Listener
}
// NewServer for gRPC.
func NewServer(params ServerParams) (*Server, error) {
metrics, err := metrics.NewServer(params.Meter)
if err != nil {
return nil, err
}
l, err := listener(params.Config)
if err != nil {
return nil, err
}
opts := []grpc.ServerOption{
grpc.KeepaliveEnforcementPolicy(keepalive.EnforcementPolicy{
MinTime: time.Timeout,
PermitWithoutStream: true,
}),
grpc.KeepaliveParams(keepalive.ServerParameters{
MaxConnectionIdle: time.Timeout,
MaxConnectionAge: time.Timeout,
MaxConnectionAgeGrace: time.Timeout,
Time: time.Timeout,
Timeout: time.Timeout,
}),
unaryServerOption(params, metrics, params.Unary...),
streamServerOption(params, metrics, params.Stream...),
}
opt, err := creds(params.Config)
if err != nil {
return nil, err
}
if opt != nil {
opts = append(opts, opt)
}
s := grpc.NewServer(opts...)
reflection.Register(s)
server := &Server{
Server: s,
sh: params.Shutdowner,
config: params.Config,
logger: params.Logger,
list: l,
}
return server, nil
}
// Start the server.
func (s *Server) Start() {
if s.list == nil {
return
}
go s.start()
}
func (s *Server) start() {
s.logger.Info("starting server", zap.Stringer("addr", s.list.Addr()), zap.String(tm.ServiceKey, "grpc"))
if err := s.Server.Serve(s.list); err != nil {
fields := []zapcore.Field{zap.Stringer("addr", s.list.Addr()), zap.Error(err), zap.String(tm.ServiceKey, "grpc")}
if err := s.sh.Shutdown(); err != nil {
fields = append(fields, zap.NamedError("shutdown_error", err))
}
s.logger.Error("could not start server", fields...)
}
}
// Stop the server.
func (s *Server) Stop(_ context.Context) {
if s.list == nil {
return
}
s.logger.Info("stopping server", zap.String(tm.ServiceKey, "grpc"))
s.Server.GracefulStop()
}
func listener(cfg *Config) (net.Listener, error) {
if !IsEnabled(cfg) {
return nil, nil
}
return server.Listener(cfg.Port)
}
func unaryServerOption(params ServerParams, m *metrics.Server, interceptors ...grpc.UnaryServerInterceptor) grpc.ServerOption {
defaultInterceptors := []grpc.UnaryServerInterceptor{
meta.UnaryServerInterceptor(UserAgent(params.Config)),
tracer.UnaryServerInterceptor(params.Tracer),
szap.UnaryServerInterceptor(params.Logger),
m.UnaryInterceptor(),
}
defaultInterceptors = append(defaultInterceptors, interceptors...)
return grpc.UnaryInterceptor(middleware.ChainUnaryServer(defaultInterceptors...))
}
func streamServerOption(params ServerParams, m *metrics.Server, interceptors ...grpc.StreamServerInterceptor) grpc.ServerOption {
defaultInterceptors := []grpc.StreamServerInterceptor{
meta.StreamServerInterceptor(UserAgent(params.Config)),
tracer.StreamServerInterceptor(params.Tracer),
szap.StreamServerInterceptor(params.Logger),
m.StreamInterceptor(),
}
defaultInterceptors = append(defaultInterceptors, interceptors...)
return grpc.StreamInterceptor(middleware.ChainStreamServer(defaultInterceptors...))
}
func creds(cfg *Config) (grpc.ServerOption, error) {
if !IsEnabled(cfg) || !security.IsEnabled(cfg.Security) {
return nil, nil
}
conf, err := security.NewTLSConfig(cfg.Security)
if err != nil {
return nil, err
}
return grpc.Creds(credentials.NewTLS(conf)), nil
}