-
Notifications
You must be signed in to change notification settings - Fork 814
/
option.go
344 lines (288 loc) · 12 KB
/
option.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
335
336
337
338
339
340
341
342
343
344
/*
* Copyright 2021 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package server
import (
"context"
"fmt"
"net"
"time"
"github.com/cloudwego/kitex/pkg/streaming"
internal_server "github.com/cloudwego/kitex/internal/server"
"github.com/cloudwego/kitex/pkg/endpoint"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/pkg/limit"
"github.com/cloudwego/kitex/pkg/limiter"
"github.com/cloudwego/kitex/pkg/registry"
"github.com/cloudwego/kitex/pkg/remote"
"github.com/cloudwego/kitex/pkg/remote/trans/netpollmux"
"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/grpc"
"github.com/cloudwego/kitex/pkg/rpcinfo"
"github.com/cloudwego/kitex/pkg/stats"
"github.com/cloudwego/kitex/pkg/utils"
)
// Option is the only way to config server.
type Option = internal_server.Option
// Options is used to initialize the server.
type Options = internal_server.Options
// A Suite is a collection of Options. It is useful to assemble multiple associated
// Options as a single one to keep the order or presence in a desired manner.
type Suite interface {
Options() []Option
}
// WithSuite adds an option suite for server.
func WithSuite(suite Suite) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
var nested struct {
Suite string
Options utils.Slice
}
nested.Suite = fmt.Sprintf("%T(%+v)", suite, suite)
for _, op := range suite.Options() {
op.F(o, &nested.Options)
}
di.Push(nested)
}}
}
// WithMuxTransport specifies the transport type to be mux.
func WithMuxTransport() Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push("WithMuxTransport()")
o.RemoteOpt.SvrHandlerFactory = netpollmux.NewSvrTransHandlerFactory()
// set limit options
o.Limit.QPSLimitPostDecode = true
}}
}
// WithMiddleware adds middleware for server to handle request.
func WithMiddleware(mw endpoint.Middleware) Option {
mwb := func(ctx context.Context) endpoint.Middleware {
return mw
}
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithMiddleware(%+v)", utils.GetFuncName(mw)))
o.MWBs = append(o.MWBs, mwb)
}}
}
// WithMiddlewareBuilder adds middleware that depend on context for server to handle request
func WithMiddlewareBuilder(mwb endpoint.MiddlewareBuilder, funcName ...string) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithMiddlewareBuilder(%+v)", utils.GetFuncName(mwb)))
o.MWBs = append(o.MWBs, mwb)
}}
}
// WithReadWriteTimeout sets the read/write timeout on network.
// IMPORTANT: this option is not stable, and will be changed or removed in the future!!!
// We don't promise compatibility for this option in future versions!!!
func WithReadWriteTimeout(d time.Duration) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithReadWriteTimeout(%v)", d))
rpcinfo.AsMutableRPCConfig(o.Configs).SetReadWriteTimeout(d)
o.LockBits |= rpcinfo.BitReadWriteTimeout
}}
}
// WithLogger sets the Logger for kitex server.
// Deprecated: server uses the global klog.DefaultLogger.
func WithLogger(logger klog.FormatLogger) Option {
panic("server.WithLogger is deprecated")
}
// WithExitWaitTime sets the wait duration for graceful shutdown.
func WithExitWaitTime(timeout time.Duration) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithExitWaitTime(%+v)", timeout))
if timeout > 0 {
o.RemoteOpt.ExitWaitTime = timeout
}
}}
}
// WithMaxConnIdleTime sets the max idle time on connection from clients.
func WithMaxConnIdleTime(timeout time.Duration) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithMaxConnIdleTime(%+v)", timeout))
if timeout > 0 {
o.RemoteOpt.MaxConnectionIdleTime = timeout
}
}}
}
// WithLimit sets the limitation of concurrent connections or max QPS.
// IMPORTANT: this option is not stable, and will be changed or removed in the future!!!
// We don't promise compatibility for this option in future versions!!!
func WithLimit(lim *limit.Option) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithLimit(%+v)", lim))
o.Limit.Limits = lim
}}
}
// WithConnectionLimiter sets the limiter of connections.
// If both WithLimit and WithConnectionLimiter are called, only the latter will take effect.
func WithConnectionLimiter(conLimit limiter.ConcurrencyLimiter) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithConnectionLimiter(%T{%+v})", conLimit, conLimit))
o.Limit.ConLimit = conLimit
}}
}
// WithQPSLimiter sets the limiter of max QPS.
// If both WithLimit and WithQPSLimiter are called, only the latter will take effect.
func WithQPSLimiter(qpsLimit limiter.RateLimiter) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithQPSLimiter(%T{%+v})", qpsLimit, qpsLimit))
o.Limit.QPSLimit = qpsLimit
}}
}
// WithTracer adds a tracer to server.
func WithTracer(c stats.Tracer) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithTracer(%T{%+v})", c, c))
if o.TracerCtl == nil {
o.TracerCtl = &rpcinfo.TraceController{}
}
o.TracerCtl.Append(c)
}}
}
// WithStatsLevel sets the stats level for server.
func WithStatsLevel(level stats.Level) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithStatsLevel(%+v)", level))
l := level
o.StatsLevel = &l
}}
}
// WithServiceAddr sets the listen address for server.
func WithServiceAddr(addr net.Addr) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithServiceAddr(%+v)", addr))
o.RemoteOpt.Address = addr
}}
}
// WithCodec to set a codec that handle other protocols which not support by kitex
func WithCodec(c remote.Codec) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithCodec(%+v)", c))
o.RemoteOpt.Codec = c
}}
}
// WithPayloadCodec to set a payloadCodec that handle other payload which not support by kitex
func WithPayloadCodec(c remote.PayloadCodec) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithPayloadCodec(%+v)", c))
o.RemoteOpt.PayloadCodec = c
}}
}
// WithRegistry to set a Registry to register service
func WithRegistry(r registry.Registry) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithRegistry(%T)", r))
o.Registry = r
}}
}
// WithRegistryInfo to set Registry Info if needed.
// It is used to add customized info and is suggested to use with WithRegistry option.
func WithRegistryInfo(info *registry.Info) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithRegistryInfo(%+v)", info))
o.RegistryInfo = info
}}
}
// WithGRPCWriteBufferSize determines how much data can be batched before doing a write on the wire.
// The corresponding memory allocation for this buffer will be twice the size to keep syscalls low.
// The default value for this buffer is 32KB.
// Zero will disable the write buffer such that each write will be on underlying connection.
// Note: A Send call may not directly translate to a write.
// It corresponds to the WriteBufferSize ServerOption of gRPC.
func WithGRPCWriteBufferSize(s uint32) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithGRPCWriteBufferSize(%+v)", s))
o.RemoteOpt.GRPCCfg.WriteBufferSize = s
}}
}
// WithGRPCReadBufferSize lets you set the size of read buffer, this determines how much data can be read at most
// for one read syscall.
// The default value for this buffer is 32KB.
// Zero will disable read buffer for a connection so data framer can access the underlying
// conn directly.
// It corresponds to the ReadBufferSize ServerOption of gRPC.
func WithGRPCReadBufferSize(s uint32) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithGRPCReadBufferSize(%+v)", s))
o.RemoteOpt.GRPCCfg.ReadBufferSize = s
}}
}
// WithGRPCInitialWindowSize returns a Option that sets window size for stream.
// The lower bound for window size is 64K and any value smaller than that will be ignored.
// It corresponds to the InitialWindowSize ServerOption of gRPC.
func WithGRPCInitialWindowSize(s uint32) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithRegistryInfo(%+v)", s))
o.RemoteOpt.GRPCCfg.InitialWindowSize = s
}}
}
// WithGRPCInitialConnWindowSize returns an Option that sets window size for a connection.
// The lower bound for window size is 64K and any value smaller than that will be ignored.
// It corresponds to the InitialConnWindowSize ServerOption of gRPC.
func WithGRPCInitialConnWindowSize(s uint32) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithGRPCInitialConnWindowSize(%+v)", s))
o.RemoteOpt.GRPCCfg.InitialConnWindowSize = s
}}
}
// WithGRPCKeepaliveParams returns an Option that sets keepalive and max-age parameters for the server.
// It corresponds to the KeepaliveParams ServerOption of gRPC.
func WithGRPCKeepaliveParams(kp grpc.ServerKeepalive) Option {
if kp.Time > 0 && kp.Time < time.Second {
kp.Time = time.Second
}
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithGRPCKeepaliveParams(%+v)", kp))
o.RemoteOpt.GRPCCfg.KeepaliveParams = kp
}}
}
// WithGRPCKeepaliveEnforcementPolicy returns an Option that sets keepalive enforcement policy for the server.
// It corresponds to the KeepaliveEnforcementPolicy ServerOption of gRPC.
func WithGRPCKeepaliveEnforcementPolicy(kep grpc.EnforcementPolicy) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithGRPCKeepaliveEnforcementPolicy(%+v)", kep))
o.RemoteOpt.GRPCCfg.KeepaliveEnforcementPolicy = kep
}}
}
// WithGRPCMaxConcurrentStreams returns an Option that will apply a limit on the number
// of concurrent streams to each ServerTransport.
// It corresponds to the MaxConcurrentStreams ServerOption of gRPC.
func WithGRPCMaxConcurrentStreams(n uint32) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithGRPCMaxConcurrentStreams(%+v)", n))
o.RemoteOpt.GRPCCfg.MaxStreams = n
}}
}
// WithGRPCMaxHeaderListSize returns a ServerOption that sets the max (uncompressed) size
// of header list that the server is prepared to accept.
// It corresponds to the MaxHeaderListSize ServerOption of gRPC.
func WithGRPCMaxHeaderListSize(s uint32) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithGRPCMaxHeaderListSize(%+v)", s))
o.RemoteOpt.GRPCCfg.MaxHeaderListSize = &s
}}
}
func WithGRPCUnknownServiceHandler(f func(ctx context.Context, methodName string, stream streaming.Stream) error) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithGRPCUnknownServiceHandler(%+v)", utils.GetFuncName(f)))
o.RemoteOpt.GRPCUnknownServiceHandler = f
}}
}
// Deprecated: Use WithConnectionLimiter instead.
func WithConcurrencyLimiter(conLimit limiter.ConcurrencyLimiter) Option {
return Option{F: func(o *internal_server.Options, di *utils.Slice) {
di.Push(fmt.Sprintf("WithConcurrencyLimiter(%T{%+v})", conLimit, conLimit))
o.Limit.ConLimit = conLimit
}}
}