-
Notifications
You must be signed in to change notification settings - Fork 4
/
option.go
276 lines (233 loc) · 6.27 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
package ws
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"time"
"github.com/gorilla/websocket"
"github.com/liumingmin/goutils/log"
)
// 连接动态参数选项
type ConnOption func(*Connection)
type HubOption func(IHub)
type ServerOption struct {
HubOpts []HubOption
}
func DebugOption(debug bool) ConnOption {
return func(conn *Connection) {
conn.debug = debug
}
}
// callback
func ConnEstablishHandlerOption(handler EventHandler) ConnOption {
return func(conn *Connection) {
if handler != nil {
conn.connEstablishHandler = func(ctx context.Context, c IConnection) {
defer log.Recover(ctx, func(e interface{}) string {
return fmt.Sprintf("%v connEstablishHandler panic, error is: %v", c.ConnType(), e)
})
handler(ctx, c)
}
}
}
}
func ConnClosingHandlerOption(handler EventHandler) ConnOption {
return func(conn *Connection) {
if handler != nil {
conn.connClosingHandler = func(ctx context.Context, c IConnection) {
defer log.Recover(ctx, func(e interface{}) string {
return fmt.Sprintf("%v connClosingHandler panic, error is: %v", c.ConnType(), e)
})
handler(ctx, c)
}
}
}
}
func ConnClosedHandlerOption(handler EventHandler) ConnOption {
return func(conn *Connection) {
if handler != nil {
conn.connClosedHandler = func(ctx context.Context, c IConnection) {
defer log.Recover(ctx, func(e interface{}) string {
return fmt.Sprintf("%v connClosedHandler panic, error is: %v", c.ConnType(), e)
})
handler(ctx, c)
}
}
}
}
func RecvPingHandlerOption(handler EventHandler) ConnOption {
return func(conn *Connection) {
if handler != nil {
conn.recvPingHandler = func(ctx context.Context, c IConnection) {
defer log.Recover(ctx, func(e interface{}) string {
return fmt.Sprintf("%v recvPingHandler panic, error is: %v", c.ConnType(), e)
})
handler(ctx, c)
}
}
}
}
func RecvPongHandlerOption(handler EventHandler) ConnOption {
return func(conn *Connection) {
if handler != nil {
conn.recvPongHandler = func(ctx context.Context, c IConnection) {
defer log.Recover(ctx, func(e interface{}) string {
return fmt.Sprintf("%v recvPongHandler panic, error is: %v", c.ConnType(), e)
})
handler(ctx, c)
}
}
}
}
func SendBufferOption(bufferSize int) ConnOption {
return func(conn *Connection) {
conn.sendBuffer = make(chan *Message, bufferSize)
}
}
func CompressionLevelOption(compressionLevel int) ConnOption {
return func(conn *Connection) {
if compressionLevel <= 0 {
return
}
conn.compressionLevel = compressionLevel
}
}
func defaultNetParamsOption() ConnOption {
return func(conn *Connection) {
conn.maxFailureRetry = 10 //重试次数
conn.readWait = 60 * time.Second //读等待
conn.writeWait = 60 * time.Second //写等待
conn.temporaryWait = 500 * time.Millisecond //网络抖动重试等待
}
}
func NetMaxFailureRetryOption(maxFailureRetry int) ConnOption {
return func(conn *Connection) {
if maxFailureRetry < 0 {
return
}
conn.maxFailureRetry = maxFailureRetry
}
}
func NetReadWaitOption(readWait time.Duration) ConnOption {
return func(conn *Connection) {
if readWait <= 0 {
return
}
conn.readWait = readWait
}
}
func NetWriteWaitOption(writeWait time.Duration) ConnOption {
return func(conn *Connection) {
if writeWait <= 0 {
return
}
conn.writeWait = writeWait
}
}
func NetTemporaryWaitOption(temporaryWait time.Duration) ConnOption {
return func(conn *Connection) {
if temporaryWait <= 0 {
return
}
conn.temporaryWait = temporaryWait
}
}
func MaxMessageBytesSizeOption(size uint32) ConnOption {
return func(con *Connection) {
con.maxMessageBytesSize = size
}
}
// 服务端特有
// upgrader定制
func SrvUpgraderOption(upgrader *websocket.Upgrader) ConnOption {
return func(conn *Connection) {
conn.upgrader = upgrader
}
}
// 为每种消息拉取逻辑分别注册不同的通道
func SrvPullChannelsOption(pullChannelIds []int) ConnOption {
return func(conn *Connection) {
if len(pullChannelIds) == 0 {
return
}
pullChannelMap := make(map[int]chan struct{})
for _, channel := range pullChannelIds {
pullChannelMap[channel] = make(chan struct{}, 1)
}
conn.pullChannelMap = pullChannelMap
}
}
func SrvUpgraderCompressOption(compress bool) ConnOption {
return func(conn *Connection) {
conn.upgrader.EnableCompression = compress
}
}
func SrvCheckOriginOption(checkOrigin func(r *http.Request) bool) ConnOption {
return func(conn *Connection) {
conn.upgrader.CheckOrigin = checkOrigin
}
}
// 客户端专用
// 默认使用时间戳来记录客户端所连服务器的id
func ClientIdOption(id string) ConnOption {
return func(conn *Connection) {
conn.id = id
}
}
func ClientDialOption(dialer *websocket.Dialer) ConnOption {
return func(conn *Connection) {
conn.dialer = dialer
}
}
func ClientDialWssOption(sUrl string, secureWss bool) ConnOption {
u, err := url.Parse(sUrl)
if err != nil {
log.Error(context.Background(), "Parse url %s err:%v", sUrl, err)
}
return func(conn *Connection) {
if u != nil && u.Scheme == "wss" && !secureWss {
conn.dialer.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
}
}
func ClientDialCompressOption(compress bool) ConnOption { //, compressLevel int
return func(conn *Connection) {
conn.dialer.EnableCompression = compress
}
}
func ClientDialHandshakeTimeoutOption(handshakeTimeout time.Duration) ConnOption {
return func(conn *Connection) {
conn.dialer.HandshakeTimeout = handshakeTimeout
}
}
func ClientDialRetryOption(retryNum int, retryInterval time.Duration) ConnOption {
return func(conn *Connection) {
conn.dialRetryNum = retryNum
conn.dialRetryInterval = retryInterval
}
}
func ClientDialConnFailedHandlerOption(handler EventHandler) ConnOption {
return func(conn *Connection) {
if handler != nil {
conn.dialConnFailedHandler = func(ctx context.Context, c IConnection) {
defer log.Recover(ctx, func(e interface{}) string {
return fmt.Sprintf("%v dialConnFailedHandler panic, error is: %v", c.ConnType(), e)
})
handler(ctx, c)
}
}
}
}
func HubShardOption(cnt uint16) HubOption {
return func(hub IHub) {
sHub, ok := hub.(*shardHub)
if !ok {
return
}
for i := uint16(0); i < cnt; i++ {
sHub.hubs = append(sHub.hubs, newHub())
}
}
}