Repository navigation
Expand file tree
/
Copy pathgrpc.go
More file actions
executable file
·249 lines (200 loc) · 6.45 KB
/
Copy pathgrpc.go
File metadata and controls
executable file
·249 lines (200 loc) · 6.45 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package servers
import (
"context"
"errors"
"fmt"
"github.com/bool64/ctxd"
"github.com/bool64/zapctxd"
grpcLogging "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
)
// ErrGRPCStart is returned when an error occurs the GRPC server start.
var ErrGRPCStart = errors.New("start grpc server")
// GRPCRegisterService is an interface for a grpc service that provides registration.
type GRPCRegisterService interface {
RegisterService(s grpc.ServiceRegistrar)
}
// GRPCRegisterServiceFunc is the function to register service to a grpc.
type GRPCRegisterServiceFunc func(s grpc.ServiceRegistrar)
// WithRegisterService registers a service.
// Apply to GRPC server instances.
func WithRegisterService(r GRPCRegisterService) Option {
return func(srv any) {
s, ok := srv.(*GRPC)
if !ok {
// skip does not apply to this instance.
return
}
s.options.registerServices = append(s.options.registerServices, r.RegisterService)
}
}
// WithReflection sets service to implement reflection. Mainly used in dev.
// Apply to GRPC server instances.
func WithReflection() Option {
return func(srv any) {
s, ok := srv.(*GRPC)
if !ok {
// skip does not apply to this instance.
return
}
s.options.reflection = true
}
}
// WithChainUnaryInterceptor sets the server interceptors for unary.
// Apply to GRPC server instances.
func WithChainUnaryInterceptor(interceptors ...grpc.UnaryServerInterceptor) Option {
return WithServerOption(grpc.ChainUnaryInterceptor(interceptors...))
}
// WithChainStreamInterceptor sets the server interceptors for stream.
// Apply to GRPC server instances.
func WithChainStreamInterceptor(interceptors ...grpc.StreamServerInterceptor) Option {
return WithServerOption(grpc.ChainStreamInterceptor(interceptors...))
}
// WithServerOption sets the options for the grpc server.
// Apply to GRPC server instances.
func WithServerOption(opts ...grpc.ServerOption) Option {
return func(srv any) {
s, ok := srv.(*GRPC)
if !ok {
// skip does not apply to this instance.
return
}
s.options.serverOpts = append(s.options.serverOpts, opts...)
}
}
// GRPCObserver interface implemented by anything wants to append observer in the server thro UnaryServerInterceptor and StreamServerInterceptor.
type GRPCObserver interface {
UnaryServerInterceptor() func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error)
StreamServerInterceptor() func(srv any, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error
}
// WithGRPCObserver sets the GRPCObserver collector manager, used to append UnaryServerInterceptor and StreamServerInterceptor
// Apply to GRPC server instances.
func WithGRPCObserver(observer GRPCObserver) Option {
return func(srv any) {
s, ok := srv.(*GRPC)
if !ok {
// skip does not apply to this instance.
return
}
if s.options.observer != nil {
return
}
s.options.observer = observer
s.options.serverOpts = append(
s.options.serverOpts,
grpc.ChainUnaryInterceptor(observer.UnaryServerInterceptor()),
grpc.ChainStreamInterceptor(observer.StreamServerInterceptor()),
)
}
}
// WithGrpcHealthCheck sets service to enable health check.
// Apply to GRPC server instances.
func WithGrpcHealthCheck() Option {
return func(srv any) {
s, ok := srv.(*GRPC)
if !ok {
// skip does not apply to this instance.
return
}
s.options.healthCheck = true
}
}
// WithLogger sets service to use logger.
// Apply to GRPC server instances.
func WithLogger(logger ctxd.Logger) Option {
return func(srv any) {
s, ok := srv.(*GRPC)
if !ok {
// skip does not apply to this instance.
return
}
s.options.logger = logger
s.options.serverOpts = append(
s.options.serverOpts,
grpc.ChainUnaryInterceptor(grpcLogging.UnaryServerInterceptor(grpcInterceptorLogger(s.options.logger))),
grpc.ChainStreamInterceptor(grpcLogging.StreamServerInterceptor(grpcInterceptorLogger(s.options.logger))),
)
}
}
// grpcInterceptorLogger adapts zapctxd logger to interceptor logger.
func grpcInterceptorLogger(l ctxd.Logger) grpcLogging.Logger {
return grpcLogging.LoggerFunc(func(ctx context.Context, lvl grpcLogging.Level, msg string, fields ...any) {
ctx = ctxd.AddFields(ctx, fields...)
if sl, ok := l.(interface{ SkipCaller() *zapctxd.Logger }); ok {
l = sl.SkipCaller()
}
switch lvl {
case grpcLogging.LevelDebug:
l.Debug(ctx, msg)
case grpcLogging.LevelInfo:
l.Info(ctx, msg)
case grpcLogging.LevelWarn:
l.Warn(ctx, msg)
case grpcLogging.LevelError:
l.Error(ctx, msg)
default:
panic(fmt.Sprintf("unknown level %v", lvl))
}
})
}
// NewGRPC initiates a new wrapped grpc server.
func NewGRPC(config Config, opts ...Option) *GRPC {
srv := &GRPC{}
srv.Server = NewServer(config, opts...)
for _, o := range opts {
o(srv)
}
// Init GRPC Server.
grpcSrv := grpc.NewServer(srv.options.serverOpts...)
for _, register := range srv.options.registerServices {
register(grpcSrv)
}
// Make the service reflective so that APIs can be discovered.
if srv.options.reflection {
reflection.Register(grpcSrv)
}
// Init GRPC Health Server.
if srv.options.healthCheck {
grpcHealthServer := health.NewServer()
grpcHealthServer.SetServingStatus("", healthpb.HealthCheckResponse_SERVING)
grpcHealthServer.SetServingStatus(config.Name, healthpb.HealthCheckResponse_SERVING)
healthpb.RegisterHealthServer(grpcSrv, grpcHealthServer)
srv.grpcHealthServer = grpcHealthServer
}
srv.grpcServer = grpcSrv
return srv
}
type grpcOptions struct {
registerServices []GRPCRegisterServiceFunc
serverOpts []grpc.ServerOption
reflection bool
observer GRPCObserver
logger ctxd.Logger
limiter GRPCRateLimiter
healthCheck bool
}
// GRPC is a listening grpc server instance.
type GRPC struct {
*Server
options grpcOptions
grpcServer *grpc.Server
grpcHealthServer *health.Server
}
// Start starts serving the GRPC server.
func (srv *GRPC) Start() error {
if err := srv.Server.Start(); err != nil {
return err
}
if err := srv.serve(srv.grpcServer, srv.listener); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
return fmt.Errorf("%w: %v addr %s", ErrGRPCStart, err, srv.listener.Addr().String()) //nolint:errorlint
}
return nil
}
// Stop gracefully shuts down the GRPC server.
func (srv *GRPC) Stop() {
srv.grpcServer.GracefulStop()
srv.Server.Stop()
}