-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
157 lines (128 loc) · 4.16 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
package grpc
import (
"context"
"github.com/alexfalkowski/go-service/crypto/tls"
"github.com/alexfalkowski/go-service/limiter"
sg "github.com/alexfalkowski/go-service/net/grpc"
"github.com/alexfalkowski/go-service/server"
"github.com/alexfalkowski/go-service/time"
gl "github.com/alexfalkowski/go-service/transport/grpc/limiter"
"github.com/alexfalkowski/go-service/transport/grpc/meta"
logger "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"
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
v3 "github.com/ulule/limiter/v3"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"go.uber.org/zap"
"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 trace.Tracer
Meter metric.Meter
Limiter *v3.Limiter
Key limiter.KeyFunc
Unary []grpc.UnaryServerInterceptor `optional:"true"`
Stream []grpc.StreamServerInterceptor `optional:"true"`
}
// Server for gRPC.
type Server struct {
server *grpc.Server
srv *server.Server
}
// NewServer for gRPC.
func NewServer(params ServerParams) (*Server, error) {
opt, err := creds(params.Config)
if err != nil {
return nil, err
}
metrics := metrics.NewServer(params.Meter)
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,
}
s := grpc.NewServer(opts...)
reflection.Register(s)
sv, err := sg.NewServer(s, config(params.Config))
if err != nil {
return nil, err
}
svr := server.NewServer("grpc", sv, params.Logger, params.Shutdowner)
return &Server{srv: svr, server: s}, nil
}
// Start the server.
func (s *Server) Start() {
s.srv.Start()
}
// Stop the server.
func (s *Server) Stop(ctx context.Context) {
s.srv.Stop(ctx)
}
// Server for gRPC.
func (s *Server) Server() *grpc.Server {
return s.server
}
func unaryServerOption(params ServerParams, m *metrics.Server, interceptors ...grpc.UnaryServerInterceptor) grpc.ServerOption {
defaultInterceptors := []grpc.UnaryServerInterceptor{
meta.UnaryServerInterceptor(UserAgent(params.Config)),
tracer.UnaryServerInterceptor(params.Tracer),
logger.UnaryServerInterceptor(params.Logger),
m.UnaryInterceptor(),
gl.UnaryServerInterceptor(params.Limiter, params.Key),
}
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),
logger.StreamServerInterceptor(params.Logger),
m.StreamInterceptor(),
gl.StreamServerInterceptor(params.Limiter, params.Key),
}
defaultInterceptors = append(defaultInterceptors, interceptors...)
return grpc.StreamInterceptor(middleware.ChainStreamServer(defaultInterceptors...))
}
func creds(cfg *Config) (grpc.ServerOption, error) {
if !IsEnabled(cfg) || !tls.IsEnabled(cfg.TLS) {
return grpc.EmptyServerOption{}, nil
}
conf, err := tls.NewConfig(cfg.TLS)
if err != nil {
return grpc.EmptyServerOption{}, err
}
creds := credentials.NewTLS(conf)
return grpc.Creds(creds), nil
}
func config(cfg *Config) sg.Config {
c := sg.Config{}
if !IsEnabled(cfg) {
return c
}
c.Enabled = true
c.Port = cfg.Port
return c
}