-
Notifications
You must be signed in to change notification settings - Fork 5
/
common_options.go
392 lines (343 loc) · 9.31 KB
/
common_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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2023-2024 antlabs. All rights reserved.
//
// 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 greatws
import (
"time"
"unicode/utf8"
)
// 0. CallbackFunc
func WithClientCallbackFunc(open OnOpenFunc, m OnMessageFunc, c OnCloseFunc) ClientOption {
return func(o *DialOption) {
o.cb = &funcToCallback{
onOpen: open,
onMessage: m,
onClose: c,
}
}
}
// 配置服务端回调函数
func WithServerCallbackFunc(open OnOpenFunc, m OnMessageFunc, c OnCloseFunc) ServerOption {
return func(o *ConnOption) {
o.cb = &funcToCallback{
onOpen: open,
onMessage: m,
onClose: c,
}
}
}
// 1. callback
// 配置客户端callback
func WithClientCallback(cb Callback) ClientOption {
return func(o *DialOption) {
o.cb = cb
}
}
// 配置服务端回调函数
func WithServerCallback(cb Callback) ServerOption {
return func(o *ConnOption) {
o.cb = cb
}
}
// 2. 设置TCP_NODELAY
// 设置客户端TCP_NODELAY
func WithClientTCPDelay() ClientOption {
return func(o *DialOption) {
o.tcpNoDelay = false
}
}
// 设置TCP_NODELAY 为false, 开启nagle算法
// 设置服务端TCP_NODELAY
func WithServerTCPDelay() ServerOption {
return func(o *ConnOption) {
o.tcpNoDelay = false
}
}
// 3.关闭utf8检查
func WithServerEnableUTF8Check() ServerOption {
return func(o *ConnOption) {
o.utf8Check = utf8.Valid
}
}
func WithClientEnableUTF8Check() ClientOption {
return func(o *DialOption) {
o.utf8Check = utf8.Valid
}
}
// 4.仅仅配置OnMessae函数
// 仅仅配置OnMessae函数
func WithServerOnMessageFunc(cb OnMessageFunc) ServerOption {
return func(o *ConnOption) {
o.cb = OnMessageFunc(cb)
}
}
// 仅仅配置OnMessae函数
func WithClientOnMessageFunc(cb OnMessageFunc) ClientOption {
return func(o *DialOption) {
o.cb = OnMessageFunc(cb)
}
}
// 5.
// 配置自动回应ping frame, 当收到ping, 回一个pong
func WithServerReplyPing() ServerOption {
return func(o *ConnOption) {
o.replyPing = true
}
}
// 配置自动回应ping frame, 当收到ping, 回一个pong
func WithClientReplyPing() ClientOption {
return func(o *DialOption) {
o.replyPing = true
}
}
// 6 配置忽略pong消息
func WithClientIgnorePong() ClientOption {
return func(o *DialOption) {
o.ignorePong = true
}
}
func WithServerIgnorePong() ServerOption {
return func(o *ConnOption) {
o.ignorePong = true
}
}
// 7.
// 设置几倍payload的缓冲区
// 只有解析方式是窗口的时候才有效
// 如果为1.0就是1024 + 14, 如果是2.0就是2048 + 14
func WithServerWindowsMultipleTimesPayloadSize(mt float32) ServerOption {
return func(o *ConnOption) {
if mt < 1.0 {
mt = 1.0
}
o.windowsMultipleTimesPayloadSize = mt
}
}
func WithClientWindowsMultipleTimesPayloadSize(mt float32) ClientOption {
return func(o *DialOption) {
if mt < 1.0 {
mt = 1.0
}
o.windowsMultipleTimesPayloadSize = mt
}
}
// 10 配置解压缩
func WithClientDecompression() ClientOption {
return func(o *DialOption) {
o.Decompression = true
}
}
func WithServerDecompression() ServerOption {
return func(o *ConnOption) {
o.Decompression = true
}
}
// 11 关闭bufio clear hack优化
func WithServerDisableBufioClearHack() ServerOption {
return func(o *ConnOption) {
o.disableBufioClearHack = true
}
}
func WithClientDisableBufioClearHack() ClientOption {
return func(o *DialOption) {
o.disableBufioClearHack = true
}
}
// 13. 配置延迟发送
// 配置延迟最大发送时间
func WithServerMaxDelayWriteDuration(d time.Duration) ServerOption {
return func(o *ConnOption) {
o.maxDelayWriteDuration = d
}
}
// 13. 配置延迟发送
// 配置延迟最大发送时间
func WithClientMaxDelayWriteDuration(d time.Duration) ClientOption {
return func(o *DialOption) {
o.maxDelayWriteDuration = d
}
}
// 14.1 配置最大延迟个数.server
func WithServerMaxDelayWriteNum(n int32) ServerOption {
return func(o *ConnOption) {
o.maxDelayWriteNum = n
}
}
// 14.2 配置最大延迟个数.client
func WithClientMaxDelayWriteNum(n int32) ClientOption {
return func(o *DialOption) {
o.maxDelayWriteNum = n
}
}
// 15.1 配置延迟包的初始化buffer大小
func WithServerDelayWriteInitBufferSize(n int32) ServerOption {
return func(o *ConnOption) {
o.delayWriteInitBufferSize = n
}
}
// 15.2 配置延迟包的初始化buffer大小
func WithClientDelayWriteInitBufferSize(n int32) ClientOption {
return func(o *DialOption) {
o.delayWriteInitBufferSize = n
}
}
// 16. 配置读超时时间
//
// 16.1 .设置服务端读超时时间
func WithServerReadTimeout(t time.Duration) ServerOption {
return func(o *ConnOption) {
o.readTimeout = t
}
}
// 16.2 .设置客户端读超时时间
func WithClientReadTimeout(t time.Duration) ClientOption {
return func(o *DialOption) {
o.readTimeout = t
}
}
// 17。 只配置OnClose
// 17.1 配置服务端OnClose
func WithServerOnCloseFunc(onClose func(c *Conn, err error)) ServerOption {
return func(o *ConnOption) {
o.cb = OnCloseFunc(onClose)
}
}
// 17.2 配置客户端OnClose
func WithClientOnCloseFunc(onClose func(c *Conn, err error)) ClientOption {
return func(o *DialOption) {
o.cb = OnCloseFunc(onClose)
}
}
// 18.1 配置服务端Callback相关方法在io event loop中执行
func WithServerCallbackInEventLoop() ServerOption {
return func(o *ConnOption) {
o.runInGoTask = "io"
}
}
// 18.2 配置服务端Callback相关方法在io event loop中执行
func WithClientCallbackInEventLoop() ClientOption {
return func(o *DialOption) {
o.runInGoTask = "io"
}
}
// 默认模式
// 19.1 配置服务端使用stream模式处理请求,go程会复用,保证OnMessage的处理是有序的
func WithServerStreamMode() ServerOption {
return func(o *ConnOption) {
o.runInGoTask = "stream2"
}
}
// 默认模式
// 19.2 配置客户端使用stream模式处理请求,go程会复用,保证OnMessage的处理是有序的
func WithClientStreamMode() ClientOption {
return func(o *DialOption) {
o.runInGoTask = "stream2"
}
}
// 20.1 配置自定义task, 需要确保传入的值是有效的,不然会panic
func WithServerCustomTaskMode(taskName string) ServerOption {
return func(o *ConnOption) {
if len(taskName) > 0 {
o.runInGoTask = taskName
}
}
}
// 20.2 配置自定义task, 需要确保传入的值是有效的,不然会panic
func WithClientCustomTaskMode(taskName string) ClientOption {
return func(o *DialOption) {
if len(taskName) > 0 {
o.runInGoTask = taskName
}
}
}
// 20.1 配置服务端使用unstream模式处理请求,go程会复用, 但是不保证顺序性,如果你的请求对时序有要求,请不要使用这个模式,
func WithServerUnstreamMode() ServerOption {
return func(o *ConnOption) {
o.runInGoTask = "unstream"
}
}
// 默认stream2,忽略这个API
// 19.2 配置客户端使用stream模式处理请求,go程会复用,但是不保证顺序性,如果你的请求对时序有要求,请不要使用这个模式,
func WithClientUnstreamMode() ClientOption {
return func(o *DialOption) {
o.runInGoTask = "unstream"
}
}
// 20.1 配置event
func WithServerMultiEventLoop(m *MultiEventLoop) ServerOption {
return func(o *ConnOption) {
o.multiEventLoop = m
}
}
// 20.2 配置event
func WithClientMultiEventLoop(m *MultiEventLoop) ClientOption {
return func(o *DialOption) {
o.multiEventLoop = m
}
}
// 21.1 配置压缩和解压缩
func WithServerDecompressAndCompress() ServerOption {
return func(o *ConnOption) {
o.Compression = true
o.Decompression = true
}
}
// 21.2 配置压缩和解压缩
func WithClientDecompressAndCompress() ClientOption {
return func(o *DialOption) {
o.Compression = true
o.Decompression = true
}
}
// 21.1 设置客户端支持上下文接管, 默认不支持上下文接管
func WithClientContextTakeover() ClientOption {
return func(o *DialOption) {
o.ClientContextTakeover = true
}
}
// 21.2 设置服务端支持上下文接管, 默认不支持上下文接管
func WithServerContextTakeover() ServerOption {
return func(o *ConnOption) {
o.ServerContextTakeover = true
}
}
// 21.1 设置客户端最大窗口位数,使用上下文接管时,这个参数才有效
func WithClientMaxWindowsBits(bits uint8) ClientOption {
return func(o *DialOption) {
if bits < 8 || bits > 15 {
return
}
o.ClientMaxWindowBits = bits
}
}
// 22.2 设置服务端最大窗口位数, 使用上下文接管时,这个参数才有效
func WithServerMaxWindowBits(bits uint8) ServerOption {
return func(o *ConnOption) {
if bits < 8 || bits > 15 {
return
}
o.ServerMaxWindowBits = bits
}
}
// 22.1 设置客户端最大可以读取的message的大小, 默认没有限制
func WithClientReadMaxMessage(size int64) ClientOption {
return func(o *DialOption) {
o.readMaxMessage = size
}
}
// 22.2 设置服务端最大可以读取的message的大小,默认没有限制
func WithServerReadMaxMessage(size int64) ServerOption {
return func(o *ConnOption) {
o.readMaxMessage = size
}
}