-
Notifications
You must be signed in to change notification settings - Fork 27
/
config.go
238 lines (199 loc) · 6.05 KB
/
config.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 config
import (
"time"
)
import (
"github.com/dubbogo/triple/pkg/common/constant"
loggerInterface "github.com/dubbogo/triple/pkg/common/logger"
"github.com/dubbogo/triple/pkg/common/logger/default_logger"
)
// triple option
type Option struct {
// network opts
Timeout time.Duration
BufferSize uint32
// service opts
Location string
Protocol string
CodecType constant.CodecType
//SerializerTypeInWrapper is used in pbWrapperCodec, to write serializeType field, if empty, use Option.CodecType as default
SerializerTypeInWrapper string
// triple header opts
HeaderGroup string
HeaderAppVersion string
// grpc opts
GRPCMaxCallSendMsgSize int
GRPCMaxServerSendMsgSize int
GRPCMaxCallRecvMsgSize int
GRPCMaxServerRecvMsgSize int
GRPCKeepAliveTime time.Duration
GRPCKeepAliveTimeout time.Duration
// tracing
JaegerAddress string
JaegerServiceName string
JaegerUseAgent bool
// logger
Logger loggerInterface.Logger
// NumWorkers is num of gr in ConnectionPool
NumWorkers uint32
// proxy mode for gateway
ProxyModeEnable bool
//tls
CACertFile string
TLSCertFile string
TLSKeyFile string
TLSServerName string
}
// Validate sets empty field to default config
func (o *Option) Validate() {
if o.Timeout == time.Duration(0) {
o.Timeout = constant.DefaultTimeout
}
if o.BufferSize == uint32(0) {
o.BufferSize = uint32(constant.DefaultHttp2ControllerReadBufferSize)
}
if o.Location == "" {
o.Location = constant.DefaultListeningAddress
}
if o.Logger == nil {
o.Logger = default_logger.GetDefaultLogger()
}
if o.Protocol == "" {
o.Protocol = constant.TRIPLE
}
if o.CodecType == "" {
o.CodecType = constant.PBCodecName
}
if o.NumWorkers <= 0 {
o.NumWorkers = constant.DefaultNumWorkers
}
}
// nolint
type OptionFunction func(o *Option)
// NewTripleOption return Triple Option with given config defined by @fs
func NewTripleOption(fs ...OptionFunction) *Option {
opt := &Option{}
for _, v := range fs {
v(opt)
}
opt.Validate()
return opt
}
// WithClientTimeout return OptionFunction with timeout of @timeout
func WithClientTimeout(timeout time.Duration) OptionFunction {
return func(o *Option) {
o.Timeout = timeout
}
}
// WithBufferSize return OptionFunction with buffer read size of @size
func WithBufferSize(size uint32) OptionFunction {
return func(o *Option) {
o.BufferSize = size
}
}
// WithCodecType return OptionFunction with target @serializerType, now we support "protobuf" and "hessian2"
func WithCodecType(serializerType constant.CodecType) OptionFunction {
return func(o *Option) {
o.CodecType = serializerType
}
}
// WithProtocol return OptionFunction with target @protocol, now we support "tri"
func WithProtocol(protocol string) OptionFunction {
return func(o *Option) {
o.Protocol = protocol
}
}
// WithLocation return OptionFunction with target @location, for example "127.0.0.1:20001"
func WithLocation(location string) OptionFunction {
return func(o *Option) {
o.Location = location
}
}
// WithHeaderAppVersion return OptionFunction with target @appVersion, for example "1.0.0"
func WithHeaderAppVersion(appVersion string) OptionFunction {
return func(o *Option) {
o.HeaderAppVersion = appVersion
}
}
// WithHeaderGroup return OptionFunction with target @group, for example "dubbogo"
func WithHeaderGroup(group string) OptionFunction {
return func(o *Option) {
o.HeaderGroup = group
}
}
// WithLogger return OptionFunction with target @logger, which must impl triple/pkg/common/logger.Logger
// the input @logger should be AddCallerSkip(1)
func WithLogger(logger loggerInterface.Logger) OptionFunction {
return func(o *Option) {
o.Logger = loggerInterface.NewLoggerWrapper(logger)
}
}
// WithSerializerTypeInWrapper return OptionFunction with target @name as SerializerTypeInWrapper
func WithSerializerTypeInWrapper(name string) OptionFunction {
return func(o *Option) {
o.SerializerTypeInWrapper = name
}
}
func WithNumWorker(numWorkers uint32) OptionFunction {
return func(o *Option) {
o.NumWorkers = numWorkers
}
}
func WithGRPCMaxCallSendMessageSize(maxCallSendMsgSize int) OptionFunction {
return func(o *Option) {
o.GRPCMaxCallSendMsgSize = maxCallSendMsgSize
}
}
func WithGRPCMaxCallRecvMessageSize(maxCallRecvMsgSize int) OptionFunction {
return func(o *Option) {
o.GRPCMaxCallRecvMsgSize = maxCallRecvMsgSize
}
}
func WithGRPCMaxServerSendMessageSize(maxServerSendMsgSize int) OptionFunction {
return func(o *Option) {
o.GRPCMaxServerSendMsgSize = maxServerSendMsgSize
}
}
func WithGRPCMaxServerRecvMessageSize(maxServerRecvMsgSize int) OptionFunction {
return func(o *Option) {
o.GRPCMaxServerRecvMsgSize = maxServerRecvMsgSize
}
}
func WithGRPCKeepAliveTimeInterval(keepAliveInterval time.Duration) OptionFunction {
return func(o *Option) {
o.GRPCKeepAliveTime = keepAliveInterval
}
}
func WithGRPCKeepAliveTimeout(keepAliveTimeout time.Duration) OptionFunction {
return func(o *Option) {
o.GRPCKeepAliveTimeout = keepAliveTimeout
}
}
func WithJaegerConfig(address, serviceName string, useAgent bool) OptionFunction {
return func(o *Option) {
o.JaegerAddress = address
o.JaegerServiceName = serviceName
o.JaegerUseAgent = useAgent
}
}
func WithProxyModeEnable(enable bool) OptionFunction {
return func(o *Option) {
o.ProxyModeEnable = enable
}
}