-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathclient.go
370 lines (318 loc) · 8.16 KB
/
client.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
// Copyright 2019 GitBitEx.com
//
// 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
//
// https://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 pushing
import (
"context"
"encoding/json"
"github.com/gitbitex/gitbitex-spot/service"
"github.com/gorilla/websocket"
"github.com/siddontang/go-log/log"
"sync"
"sync/atomic"
"time"
)
const (
writeWait = 10 * time.Second
pongWait = 60 * time.Second
pingPeriod = (pongWait * 9) / 10
maxMessageSize = 512
)
var id int64
// 每个连接对应一个client,client负责该连接的数据I/O
type Client struct {
id int64
conn *websocket.Conn
writeCh chan interface{}
l2ChangeCh chan *Level2Change
sub *subscription
channels map[string]struct{}
mu sync.Mutex
}
func NewClient(conn *websocket.Conn, sub *subscription) *Client {
return &Client{
id: atomic.AddInt64(&id, 1),
conn: conn,
writeCh: make(chan interface{}, 256),
l2ChangeCh: make(chan *Level2Change, 512),
sub: sub,
channels: map[string]struct{}{},
}
}
func (c *Client) startServe() {
go c.runReader()
go c.runWriter()
}
func (c *Client) runReader() {
c.conn.SetReadLimit(maxMessageSize)
err := c.conn.SetReadDeadline(time.Now().Add(pongWait))
if err != nil {
log.Error(err)
}
c.conn.SetPongHandler(func(string) error {
return c.conn.SetReadDeadline(time.Now().Add(pongWait))
})
for {
_, message, err := c.conn.ReadMessage()
if err != nil {
c.close()
break
}
var req Request
err = json.Unmarshal(message, &req)
if err != nil {
log.Errorf("bad message : %v %v", string(message), err)
c.close()
break
}
c.onMessage(&req)
}
}
func (c *Client) runWriter() {
ctx, cancel := context.WithCancel(context.Background())
ticker := time.NewTicker(pingPeriod)
defer func() {
cancel()
ticker.Stop()
_ = c.conn.Close()
}()
go c.runL2ChangeWriter(ctx)
for {
select {
case message := <-c.writeCh:
// 转发l2change消息,进行增量推送
switch message.(type) {
case *Level2Change:
c.l2ChangeCh <- message.(*Level2Change)
continue
}
err := c.conn.SetWriteDeadline(time.Now().Add(writeWait))
if err != nil {
_ = c.conn.WriteMessage(websocket.CloseMessage, []byte{})
c.close()
return
}
buf, err := json.Marshal(message)
if err != nil {
continue
}
err = c.conn.WriteMessage(websocket.TextMessage, buf)
if err != nil {
c.close()
return
}
case <-ticker.C:
_ = c.conn.SetWriteDeadline(time.Now().Add(writeWait))
err := c.conn.WriteMessage(websocket.PingMessage, nil)
if err != nil {
c.close()
return
}
}
}
}
func (c *Client) runL2ChangeWriter(ctx context.Context) {
type state struct {
resendSnapshot bool
changes []*Level2Change
lastSeq int64
}
states := map[string]*state{}
stateOf := func(productId string) *state {
s, found := states[productId]
if found {
return s
}
s = &state{
resendSnapshot: true,
changes: nil,
lastSeq: 0,
}
states[productId] = s
return s
}
for {
select {
case <-ctx.Done():
return
case l2Change := <-c.l2ChangeCh:
state := stateOf(l2Change.ProductId)
if state.resendSnapshot || l2Change.Seq == 0 {
snapshot := getLastLevel2Snapshot(l2Change.ProductId)
if snapshot == nil {
log.Warnf("no snapshot for %v", l2Change.ProductId)
continue
}
// 最新的snapshot版本太旧了,丢弃,等待更新的snapshot版本
if state.lastSeq > snapshot.Seq {
log.Warnf("last snapshot too old: %v changeSeq=%v snapshotSeq=%v",
l2Change.ProductId, state.lastSeq, snapshot.Seq)
continue
}
state.lastSeq = snapshot.Seq
state.resendSnapshot = false
c.writeCh <- &Level2SnapshotMessage{
Type: Level2TypeSnapshot,
ProductId: l2Change.ProductId,
Bids: snapshot.Bids,
Asks: snapshot.Asks,
}
continue
}
// 丢弃seq小于snapshot seq的变更
if l2Change.Seq <= state.lastSeq {
log.Infof("discard l2changeSeq=%v snapshotSeq=%v", l2Change.Seq, state.lastSeq)
continue
}
// seq不连续,发生了消息丢失,重新发送快照
if l2Change.Seq != state.lastSeq+1 {
log.Infof("l2change lost newSeq=%v lastSeq=%v", l2Change.Seq, state.lastSeq)
state.resendSnapshot = true
state.changes = nil
state.lastSeq = l2Change.Seq
if len(c.l2ChangeCh) == 0 {
c.l2ChangeCh <- &Level2Change{ProductId: l2Change.ProductId}
}
continue
}
state.lastSeq = l2Change.Seq
state.changes = append(state.changes, l2Change)
// 如果chan还有消息继续读满缓冲区
if len(c.l2ChangeCh) > 0 && len(state.changes) < 10 {
continue
}
updateMsg := &Level2UpdateMessage{
Type: Level2TypeUpdate,
ProductId: l2Change.ProductId,
}
for _, change := range state.changes {
updateMsg.Changes = append(updateMsg.Changes, [3]interface{}{change.Side, change.Price, change.Size})
}
c.writeCh <- updateMsg
state.changes = nil
}
}
}
func (c *Client) onMessage(req *Request) {
switch req.Type {
case "subscribe":
c.onSub(req.CurrencyIds, req.ProductIds, req.Channels, req.Token)
case "unsubscribe":
c.onUnSub(req.CurrencyIds, req.ProductIds, req.Channels, req.Token)
default:
}
}
func (c *Client) onSub(currencyIds []string, productIds []string, channels []string, token string) {
user, err := service.CheckToken(token)
if err != nil {
log.Error(err)
}
var userId int64
if user != nil {
userId = user.Id
}
for range currencyIds {
for _, channel := range channels {
switch Channel(channel) {
case ChannelFunds:
c.subscribe(ChannelFunds.FormatWithUserId(userId))
}
}
}
for _, productId := range productIds {
for _, channel := range channels {
switch Channel(channel) {
case ChannelLevel2:
if c.subscribe(ChannelLevel2.FormatWithProductId(productId)) {
if len(c.l2ChangeCh) == 0 {
c.l2ChangeCh <- &Level2Change{ProductId: productId}
}
}
case ChannelMatch:
c.subscribe(ChannelMatch.FormatWithProductId(productId))
case ChannelTicker:
if c.subscribe(ChannelTicker.FormatWithProductId(productId)) {
ticker := getLastTicker(productId)
if ticker != nil {
c.writeCh <- ticker
}
}
case ChannelOrder:
c.subscribe(ChannelOrder.Format(productId, userId))
default:
continue
}
}
}
}
func (c *Client) onUnSub(currencyIds []string, productIds []string, channels []string, token string) {
user, err := service.CheckToken(token)
if err != nil {
log.Error(err)
}
var userId int64
if user != nil {
userId = user.Id
}
for range currencyIds {
for _, channel := range channels {
switch Channel(channel) {
case ChannelFunds:
c.unsubscribe(ChannelFunds.FormatWithUserId(userId))
}
}
}
for _, productId := range productIds {
for _, channel := range channels {
switch Channel(channel) {
case ChannelLevel2:
c.unsubscribe(ChannelLevel2.FormatWithProductId(productId))
case ChannelMatch:
c.unsubscribe(ChannelMatch.FormatWithProductId(productId))
case ChannelTicker:
c.unsubscribe(ChannelTicker.FormatWithProductId(productId))
case ChannelOrder:
c.unsubscribe(ChannelOrder.Format(productId, userId))
default:
continue
}
}
}
}
func (c *Client) subscribe(channel string) bool {
c.mu.Lock()
defer c.mu.Unlock()
_, found := c.channels[channel]
if found {
return false
}
if c.sub.subscribe(channel, c) {
c.channels[channel] = struct{}{}
return true
}
return false
}
func (c *Client) unsubscribe(channel string) {
c.mu.Lock()
defer c.mu.Unlock()
if c.sub.unsubscribe(channel, c) {
delete(c.channels, channel)
}
}
func (c *Client) close() {
c.mu.Lock()
defer c.mu.Unlock()
for channel := range c.channels {
c.sub.unsubscribe(channel, c)
}
}