/
options.go
172 lines (151 loc) · 4.58 KB
/
options.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
/*
* @Author: lwnmengjing
* @Date: 2021/6/2 4:30 下午
* @Last Modified by: lwnmengjing
* @Last Modified time: 2021/6/2 4:30 下午
*/
package grpc
import (
"context"
"crypto/tls"
"math"
"time"
pbErr "github.com/Msimplehub/semporna-go/errors"
log "github.com/Msimplehub/semporna-go/logger"
"github.com/Msimplehub/semporna-go/server/grpc/interceptors/logging"
requesttag "github.com/Msimplehub/semporna-go/server/grpc/interceptors/request_tag"
recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"google.golang.org/grpc"
)
const (
infinity = time.Duration(math.MaxInt64)
defaultMaxMsgSize = 4 << 20
defaultMaxConcurrentStreams = 100000
defaultKeepAliveTime = 30 * time.Second
defaultConnectionIdleTime = 10 * time.Second
defaultMaxServerConnectionAgeGrace = 10 * time.Second
defaultMiniKeepAliveTimeRate = 2
)
type Option func(*Options)
type Options struct {
id string
domain string
addr string
tls *tls.Config
keepAlive time.Duration
timeout time.Duration
maxConnectionAge time.Duration
maxConnectionAgeGrace time.Duration
maxConcurrentStreams int
maxMsgSize int
unaryServerInterceptors []grpc.UnaryServerInterceptor
streamServerInterceptors []grpc.StreamServerInterceptor
ctx context.Context
}
func WithContextOption(c context.Context) Option {
return func(o *Options) {
o.ctx = c
}
}
func WithIDOption(s string) Option {
return func(o *Options) {
o.id = s
}
}
func WithDomainOption(s string) Option {
return func(o *Options) {
o.domain = s
}
}
func WithAddrOption(s string) Option {
return func(o *Options) {
o.addr = s
}
}
func WithTlsOption(tls *tls.Config) Option {
return func(o *Options) {
o.tls = tls
}
}
func WithKeepAliveOption(t time.Duration) Option {
return func(o *Options) {
o.keepAlive = t
}
}
func WithTimeoutOption(t time.Duration) Option {
return func(o *Options) {
o.keepAlive = t
}
}
func WithMaxConnectionAgeOption(t time.Duration) Option {
return func(o *Options) {
o.maxConnectionAge = t
}
}
func WithMaxConnectionAgeGraceOption(t time.Duration) Option {
return func(o *Options) {
o.maxConnectionAgeGrace = t
}
}
func WithMaxConcurrentStreamsOption(i int) Option {
return func(o *Options) {
o.maxConcurrentStreams = i
}
}
func WithMaxMsgSizeOption(i int) Option {
return func(o *Options) {
o.maxMsgSize = i
}
}
func WithUnaryServerInterceptorsOption(u ...grpc.UnaryServerInterceptor) Option {
return func(o *Options) {
if o.unaryServerInterceptors == nil {
o.unaryServerInterceptors = make([]grpc.UnaryServerInterceptor, 0)
}
o.unaryServerInterceptors = append(o.unaryServerInterceptors, u...)
}
}
func WithStreamServerInterceptorsOption(u ...grpc.StreamServerInterceptor) Option {
return func(o *Options) {
if o.streamServerInterceptors == nil {
o.streamServerInterceptors = make([]grpc.StreamServerInterceptor, 0)
}
o.streamServerInterceptors = append(o.streamServerInterceptors, u...)
}
}
func defaultOptions() *Options {
return &Options{
addr: ":0",
keepAlive: defaultKeepAliveTime,
timeout: defaultConnectionIdleTime,
maxConnectionAge: infinity,
maxConnectionAgeGrace: defaultMaxServerConnectionAgeGrace,
maxConcurrentStreams: defaultMaxConcurrentStreams,
maxMsgSize: defaultMaxMsgSize,
unaryServerInterceptors: []grpc.UnaryServerInterceptor{
requesttag.UnaryServerInterceptor(),
ctxtags.UnaryServerInterceptor(),
opentracing.UnaryServerInterceptor(),
logging.UnaryServerInterceptor(),
prometheus.UnaryServerInterceptor,
recovery.UnaryServerInterceptor(recovery.WithRecoveryHandler(customRecovery("", ""))),
},
streamServerInterceptors: []grpc.StreamServerInterceptor{
requesttag.StreamServerInterceptor(),
ctxtags.StreamServerInterceptor(),
opentracing.StreamServerInterceptor(),
logging.StreamServerInterceptor(),
prometheus.StreamServerInterceptor,
recovery.StreamServerInterceptor(recovery.WithRecoveryHandler(customRecovery("", ""))),
},
}
}
func customRecovery(id, domain string) recovery.RecoveryHandlerFunc {
return func(p interface{}) (err error) {
log.Errorf("panic triggered: %v", p)
return pbErr.New(id, domain, pbErr.InternalServerError)
}
}