-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathgrpc.go
334 lines (283 loc) · 10.1 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Package server is a package that holds the http or grpc service.
package server
import (
"context"
"fmt"
"net"
"net/http"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/go-dev-frame/sponge/pkg/app"
"github.com/go-dev-frame/sponge/pkg/errcode"
"github.com/go-dev-frame/sponge/pkg/grpc/gtls"
"github.com/go-dev-frame/sponge/pkg/grpc/interceptor"
"github.com/go-dev-frame/sponge/pkg/grpc/metrics"
"github.com/go-dev-frame/sponge/pkg/logger"
"github.com/go-dev-frame/sponge/pkg/prof"
"github.com/go-dev-frame/sponge/pkg/servicerd/registry"
"github.com/go-dev-frame/sponge/internal/config"
"github.com/go-dev-frame/sponge/internal/ecode"
"github.com/go-dev-frame/sponge/internal/service"
)
var _ app.IServer = (*grpcServer)(nil)
var (
defaultTokenAppID = "grpc"
defaultTokenAppKey = "mko09ijn"
)
type grpcServer struct {
addr string
server *grpc.Server
listen net.Listener
mux *http.ServeMux
httpServer *http.Server
registerMetricsMuxAndMethodFunc func() error
iRegistry registry.Registry
instance *registry.ServiceInstance
}
// Start grpc service
func (s *grpcServer) Start() error {
// registration Services
if s.iRegistry != nil {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second) //nolint
if err := s.iRegistry.Register(ctx, s.instance); err != nil {
return err
}
}
if s.registerMetricsMuxAndMethodFunc != nil {
if err := s.registerMetricsMuxAndMethodFunc(); err != nil {
return err
}
}
// if either pprof or metrics is enabled, the http service will be started
if s.mux != nil {
addr := fmt.Sprintf(":%d", config.Get().Grpc.HTTPPort)
s.httpServer = &http.Server{
Addr: addr,
Handler: s.mux,
}
go func() {
fmt.Printf("http address of pprof and metrics %s\n", addr)
if err := s.httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic("listen and serve error: " + err.Error())
}
}()
}
listen := metrics.NewCustomListener(s.listen, metrics.WithConnectionsLogger(logger.Get()), metrics.WithConnectionsGauge())
if err := s.server.Serve(listen); err != nil { // block
return err
}
return nil
}
// Stop grpc service
func (s *grpcServer) Stop() error {
if s.iRegistry != nil {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
go func() {
_ = s.iRegistry.Deregister(ctx, s.instance)
cancel()
}()
<-ctx.Done()
}
s.server.GracefulStop()
if s.httpServer != nil {
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) //nolint
if err := s.httpServer.Shutdown(ctx); err != nil {
return err
}
}
return nil
}
// String comment
func (s *grpcServer) String() string {
return "grpc service address " + s.addr
}
// secure option
func (s *grpcServer) secureServerOption() grpc.ServerOption {
switch config.Get().Grpc.ServerSecure.Type {
case "one-way": // server side certification
credentials, err := gtls.GetServerTLSCredentials(
config.Get().Grpc.ServerSecure.CertFile,
config.Get().Grpc.ServerSecure.KeyFile,
)
if err != nil {
panic(err)
}
logger.Info("grpc security type: sever-side certification")
return grpc.Creds(credentials)
case "two-way": // both client and server side certification
credentials, err := gtls.GetServerTLSCredentialsByCA(
config.Get().Grpc.ServerSecure.CaFile,
config.Get().Grpc.ServerSecure.CertFile,
config.Get().Grpc.ServerSecure.KeyFile,
)
if err != nil {
panic(err)
}
logger.Info("grpc security type: both client-side and server-side certification")
return grpc.Creds(credentials)
}
logger.Info("grpc security type: insecure")
return nil
}
// setting up unary server interceptors
func (s *grpcServer) unaryServerOptions() grpc.ServerOption {
unaryServerInterceptors := []grpc.UnaryServerInterceptor{
interceptor.UnaryServerRecovery(),
interceptor.UnaryServerRequestID(),
}
// logger interceptor, to print simple messages, replace interceptor.UnaryServerLog with interceptor.UnaryServerSimpleLog
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerLog(
logger.Get(),
interceptor.WithReplaceGRPCLogger(),
))
// token interceptor
if config.Get().Grpc.EnableToken {
checkToken := func(appID string, appKey string) error {
// todo the defaultTokenAppID and defaultTokenAppKey are usually retrieved from the cache or database
if appID != defaultTokenAppID || appKey != defaultTokenAppKey {
return status.Errorf(codes.Unauthenticated, "app id or app key checksum failure")
}
return nil
}
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerToken(checkToken))
}
// jwt token interceptor
//unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerJwtAuth(
// // choose a verification method as needed
//interceptor.WithStandardVerify(standardVerifyFn), // standard verify (default), you can set standardVerifyFn to nil if you don't need it
//interceptor.WithCustomVerify(customVerifyFn), // custom verify
// // specify the grpc API to ignore token verification(full path)
//interceptor.WithAuthIgnoreMethods("/api.user.v1.User/Register", "/api.user.v1.User/Login"),
//))
// metrics interceptor
if config.Get().App.EnableMetrics {
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerMetrics())
s.registerMetricsMuxAndMethodFunc = s.registerMetricsMuxAndMethod()
}
// limit interceptor
if config.Get().App.EnableLimit {
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerRateLimit())
}
// circuit breaker interceptor
if config.Get().App.EnableCircuitBreaker {
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerCircuitBreaker(
// set rpc code for circuit breaker, default already includes codes.Internal and codes.Unavailable
interceptor.WithValidCode(ecode.StatusInternalServerError.Code()),
interceptor.WithValidCode(ecode.StatusServiceUnavailable.Code()),
))
}
// trace interceptor
if config.Get().App.EnableTrace {
unaryServerInterceptors = append(unaryServerInterceptors, interceptor.UnaryServerTracing())
}
return grpc.ChainUnaryInterceptor(unaryServerInterceptors...)
}
// setting up stream server interceptors
func (s *grpcServer) streamServerOptions() grpc.ServerOption {
streamServerInterceptors := []grpc.StreamServerInterceptor{
interceptor.StreamServerRecovery(),
//interceptor.StreamServerRequestID(),
}
// logger interceptor, to print simple messages, replace interceptor.StreamServerLog with interceptor.StreamServerSimpleLog
streamServerInterceptors = append(streamServerInterceptors, interceptor.StreamServerLog(
logger.Get(),
interceptor.WithReplaceGRPCLogger(),
))
// token interceptor
if config.Get().Grpc.EnableToken {
checkToken := func(appID string, appKey string) error {
// todo the defaultTokenAppID and defaultTokenAppKey are usually retrieved from the cache or database
if appID != defaultTokenAppID || appKey != defaultTokenAppKey {
return status.Errorf(codes.Unauthenticated, "app id or app key checksum failure")
}
return nil
}
streamServerInterceptors = append(streamServerInterceptors, interceptor.StreamServerToken(checkToken))
}
// jwt token interceptor
//streamServerInterceptors = append(streamServerInterceptors, interceptor.StreamServerJwtAuth(
// // choose a verification method as needed
//interceptor.WithStandardVerify(standardVerifyFn), // standard verify (default), you can set standardVerifyFn to nil if you don't need it
//interceptor.WithCustomVerify(customVerifyFn), // custom verify
// // specify the grpc API to ignore token verification(full path)
// interceptor.WithAuthIgnoreMethods("/api.user.v1.User/Register", "/api.user.v1.User/Login"),
//))
// metrics interceptor
if config.Get().App.EnableMetrics {
streamServerInterceptors = append(streamServerInterceptors, interceptor.StreamServerMetrics())
}
// limit interceptor
if config.Get().App.EnableLimit {
streamServerInterceptors = append(streamServerInterceptors, interceptor.StreamServerRateLimit())
}
// circuit breaker interceptor
if config.Get().App.EnableCircuitBreaker {
streamServerInterceptors = append(streamServerInterceptors, interceptor.StreamServerCircuitBreaker(
// set rpc code for circuit breaker, default already includes codes.Internal and codes.Unavailable
interceptor.WithValidCode(ecode.StatusInternalServerError.Code()),
interceptor.WithValidCode(ecode.StatusServiceUnavailable.Code()),
))
}
// trace interceptor
if config.Get().App.EnableTrace {
streamServerInterceptors = append(streamServerInterceptors, interceptor.StreamServerTracing())
}
return grpc.ChainStreamInterceptor(streamServerInterceptors...)
}
func (s *grpcServer) getOptions() []grpc.ServerOption {
var options []grpc.ServerOption
secureOption := s.secureServerOption()
if secureOption != nil {
options = append(options, secureOption)
}
options = append(options, s.unaryServerOptions())
options = append(options, s.streamServerOptions())
return options
}
func (s *grpcServer) registerMetricsMuxAndMethod() func() error {
return func() error {
if s.mux == nil {
s.mux = http.NewServeMux()
}
metrics.Register(s.mux, s.server)
return nil
}
}
func (s *grpcServer) registerProfMux() {
if s.mux == nil {
s.mux = http.NewServeMux()
}
prof.Register(s.mux, prof.WithIOWaitTime())
}
func (s *grpcServer) addHTTPRouter() {
if s.mux == nil {
s.mux = http.NewServeMux()
}
s.mux.HandleFunc("/codes", errcode.ListGRPCErrCodes) // error codes router
cfgStr := config.Show()
s.mux.HandleFunc("/config", errcode.ShowConfig([]byte(cfgStr))) // config router
}
// NewGRPCServer creates a new grpc server
func NewGRPCServer(addr string, opts ...GrpcOption) app.IServer {
var err error
o := defaultGrpcOptions()
o.apply(opts...)
s := &grpcServer{
addr: addr,
iRegistry: o.iRegistry,
instance: o.instance,
}
s.addHTTPRouter()
if config.Get().App.EnableHTTPProfile {
s.registerProfMux()
}
s.listen, err = net.Listen("tcp", addr)
if err != nil {
panic(err)
}
s.server = grpc.NewServer(s.getOptions()...)
service.RegisterAllService(s.server) // register for all services
return s
}