-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
110 lines (88 loc) · 3.24 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
package grpc
import (
"context"
"errors"
"net"
szap "github.com/alexfalkowski/go-service/transport/grpc/logger/zap"
"github.com/alexfalkowski/go-service/transport/grpc/meta"
"github.com/alexfalkowski/go-service/transport/grpc/metrics/prometheus"
"github.com/alexfalkowski/go-service/transport/grpc/trace/opentracing"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
tags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/soheilhy/cmux"
"go.uber.org/fx"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"google.golang.org/grpc"
)
// ServerParams for gRPC.
type ServerParams struct {
fx.In
Shutdowner fx.Shutdowner
Config *Config
Logger *zap.Logger
Tracer opentracing.Tracer
Metrics *prometheus.ServerMetrics
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
params ServerParams
}
// NewServer for gRPC.
func NewServer(params ServerParams) *Server {
opts := []grpc.ServerOption{unaryServerOption(params, params.Unary...), streamServerOption(params, params.Stream...)}
server := &Server{Server: grpc.NewServer(opts...), params: params}
return server
}
// Start the server.
func (s *Server) Start(listener net.Listener) {
s.params.Logger.Info("starting grpc server", zap.String("addr", listener.Addr().String()))
if err := s.Server.Serve(listener); err != nil && !s.ignoreError(err) {
fields := []zapcore.Field{zap.String("addr", listener.Addr().String()), zap.Error(err)}
if err := s.params.Shutdowner.Shutdown(); err != nil {
fields = append(fields, zap.NamedError("shutdown_error", err))
}
s.params.Logger.Error("could not start grpc server", fields...)
}
}
// Stop the server.
func (s *Server) Stop(ctx context.Context) {
s.params.Logger.Info("stopping grpc server")
s.Server.GracefulStop()
}
func (s *Server) ignoreError(err error) bool {
return errors.Is(err, cmux.ErrListenerClosed) || errors.Is(err, cmux.ErrServerClosed)
}
func unaryServerOption(params ServerParams, interceptors ...grpc.UnaryServerInterceptor) grpc.ServerOption {
defaultInterceptors := []grpc.UnaryServerInterceptor{
meta.UnaryServerInterceptor(),
tags.UnaryServerInterceptor(),
szap.UnaryServerInterceptor(params.Logger),
params.Metrics.UnaryServerInterceptor(),
opentracing.UnaryServerInterceptor(params.Tracer),
}
defaultInterceptors = append(defaultInterceptors, interceptors...)
return grpc.UnaryInterceptor(middleware.ChainUnaryServer(defaultInterceptors...))
}
func streamServerOption(params ServerParams, interceptors ...grpc.StreamServerInterceptor) grpc.ServerOption {
defaultInterceptors := []grpc.StreamServerInterceptor{
meta.StreamServerInterceptor(),
tags.StreamServerInterceptor(),
szap.StreamServerInterceptor(params.Logger),
params.Metrics.StreamServerInterceptor(),
opentracing.StreamServerInterceptor(params.Tracer),
}
defaultInterceptors = append(defaultInterceptors, interceptors...)
return grpc.StreamInterceptor(middleware.ChainStreamServer(defaultInterceptors...))
}