-
Notifications
You must be signed in to change notification settings - Fork 3
/
websocket.go
326 lines (293 loc) · 10.2 KB
/
websocket.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
package bitmex
import (
"encoding/json"
"fmt"
"io"
"os"
"sync"
"time"
"github.com/adampointer/go-bitmex/types"
evbus "github.com/asaskevich/EventBus"
"github.com/desertbit/timer"
"github.com/pkg/errors"
"github.com/sacOO7/gowebsocket"
log "github.com/sirupsen/logrus"
"go.uber.org/multierr"
)
const heartbeatRate = 5 * time.Second
// EventHandler is a function called in response to websocket events
type EventHandler func(obj interface{})
// APITopicHandler is a function called in response to a API subscription event being received
type APITopicHandler func(response *types.SubscriptionResponse)
// WebSocketClientConfig holds configuration data for the websocket client
type WebsocketClientConfig struct {
ApiKey, ApiSecret, URL string
}
// NewWebsocketClientConfig returns a new config struct
func NewWebsocketClientConfig() *WebsocketClientConfig {
return &WebsocketClientConfig{}
}
// WithURL sets the url to use e.g. wss://testnet.bitmex.com/realtime
func (c *WebsocketClientConfig) WithURL(u string) *WebsocketClientConfig {
c.URL = u
return c
}
// WithAuth sets the credentials and is optional if you are exclusively using public endpoints
func (c *WebsocketClientConfig) WithAuth(apiKey, apiSecret string) *WebsocketClientConfig {
c.ApiKey = apiKey
c.ApiSecret = apiSecret
return c
}
// WebsocketClient is a BitMEX websocket client
type WebsocketClient struct {
socket gowebsocket.Socket
bus evbus.Bus
topics types.SubscriptionTopics
conMutex sync.Mutex
connected bool
config *WebsocketClientConfig
heartbeatLock sync.Mutex
heartbeat *timer.Timer
log *log.Logger
logWriter io.Writer
}
// NewWebsocketClient returns a new BitMEX websocket client with the logrus standard logger
func NewWebsocketClient(cfg *WebsocketClientConfig) *WebsocketClient {
return &WebsocketClient{
socket: gowebsocket.New(cfg.URL),
bus: evbus.New(),
config: cfg,
log: log.StandardLogger(),
}
}
// SetConfig allows you to set the configuration after struct initialisation
func (w *WebsocketClient) SetConfig(cfg *WebsocketClientConfig) *WebsocketClient {
w.config = cfg
return w
}
// WithLogger overrides the logrus standard logger with a Logger instance
func (w *WebsocketClient) WithLogger(l *log.Logger) *WebsocketClient {
w.log = l
return w
}
// WithLogWriter sets the output stream for the logger
func (w *WebsocketClient) WithLogWriter(o io.Writer) *WebsocketClient {
w.log.SetOutput(o)
return w
}
// Start initialises the event bus and connects the websocket
func (w *WebsocketClient) Start() {
w.setupWebsocketSubscribers()
w.setupWebsocketPublishers()
w.socket.Connect()
}
// Restart the websocket connection
func (w *WebsocketClient) Restart() {
w.socket.Connect()
}
// Shutdown closes the websocket and unsubscribes listeners
func (w *WebsocketClient) Shutdown() {
w.destroyWebsocketSubscribers()
defer func() {
if err := recover(); err != nil {
w.log.Warn(err)
}
}()
w.socket.Close()
}
// SendCommand sends command json to the remote
func (w *WebsocketClient) SendCommand(command *types.Command) error {
msg, err := json.Marshal(command)
if err != nil {
return errors.Wrap(err, "error marshalling command to json")
}
w.log.Tracef("===x %s", string(msg))
w.socket.SendText(string(msg))
return nil
}
// SubscribeToEvents allows you to register a handler to listen to websocket events
func (w *WebsocketClient) SubscribeToEvents(evt types.Event, handler EventHandler) error {
return w.bus.SubscribeAsync(evt.String(), handler, false)
}
// SubscribeToOneEvent allows you to register a handler to listen to a single websocket event
// it will be unsubscribed after the first instance automatically
func (w *WebsocketClient) SubscribeToOneEvent(evt types.Event, handler EventHandler) error {
return w.bus.SubscribeOnceAsync(evt.String(), handler)
}
// UnsubscribeFromEvents unsubscribes a previously subscribed handler
func (w *WebsocketClient) UnsubscribeFromEvents(evt types.Event, handler EventHandler) error {
return w.bus.Unsubscribe(evt.String(), handler)
}
// SubscribeToApiTopic allows you to subscribe to a BitMEX API topic without message ordering guarantees
func (w *WebsocketClient) SubscribeToApiTopic(topic types.SubscriptionTopic, handler APITopicHandler) error {
if w.connected {
err := w.SendCommand(&types.Command{
Op: types.CommandOpSubscribe,
Args: types.CommandArgs{topic.String()},
})
if err != nil {
return err
}
} else {
w.topics = append(w.topics, topic)
}
return w.bus.SubscribeAsync(fmt.Sprintf("%s:%s", types.EventApiResponseSubscription, topic.Topic()), handler, true)
}
// UnsubscribeFromApiTopic allows you to subscribe a handler from a previously subscribed BitMEX API topic
func (w *WebsocketClient) UnsubscribeFromApiTopic(topic types.SubscriptionTopic, handler APITopicHandler) error {
return w.bus.Unsubscribe(fmt.Sprintf("%s:%s", types.EventApiResponseSubscription, topic.Topic()), handler)
}
func (w *WebsocketClient) setupWebsocketPublishers() {
w.socket.OnConnectError = func(err error, socket gowebsocket.Socket) {
w.bus.Publish(types.EventWebsocketError.String(), err)
}
w.socket.OnConnected = func(_ gowebsocket.Socket) {
w.bus.Publish(types.EventWebsocketConnected.String(), struct{}{})
}
w.socket.OnDisconnected = func(_ error, socket gowebsocket.Socket) {
w.bus.Publish(types.EventWebsocketDisconnected.String(), struct{}{})
}
w.socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
w.heartbeatLock.Lock()
if w.heartbeat != nil {
w.heartbeat.Reset(2 * heartbeatRate)
}
w.heartbeatLock.Unlock()
w.bus.Publish(types.EventWebsocketMessage.String(), message)
}
}
func (w *WebsocketClient) setupWebsocketSubscribers() {
err := multierr.Combine(
w.bus.SubscribeAsync(types.EventWebsocketError.String(), w.websocketErrorHandler, false),
w.bus.SubscribeAsync(types.EventWebsocketDisconnected.String(), w.websocketDisconnectHandler, false),
w.bus.SubscribeAsync(types.EventWebsocketConnected.String(), w.websocketConnectHandler, false),
w.bus.SubscribeAsync(types.EventWebsocketMessage.String(), w.websocketMessageHandler, false),
w.bus.SubscribeAsync(types.EventApiResponseSubscription.String(), w.apiTopicHandler, false),
w.bus.SubscribeAsync(types.EventApiResponseSuccess.String(), w.apiTopicHandler, false),
)
if err != nil {
w.log.WithError(err).Fatal("unable to setup subscribers")
}
}
func (w *WebsocketClient) destroyWebsocketSubscribers() {
err := multierr.Combine(
w.bus.Unsubscribe(types.EventWebsocketError.String(), w.websocketErrorHandler),
w.bus.Unsubscribe(types.EventWebsocketDisconnected.String(), w.websocketDisconnectHandler),
w.bus.Unsubscribe(types.EventWebsocketConnected.String(), w.websocketConnectHandler),
w.bus.Unsubscribe(types.EventWebsocketMessage.String(), w.websocketMessageHandler),
w.bus.Unsubscribe(types.EventApiResponseSubscription.String(), w.apiTopicHandler),
w.bus.Unsubscribe(types.EventApiResponseSuccess.String(), w.apiTopicHandler),
)
if err != nil {
w.log.WithError(err).Fatal("unable to setup subscribers")
os.Exit(1)
}
}
func (w *WebsocketClient) websocketErrorHandler(err error) {
w.log.WithError(err).Error("websocket error")
w.disconnectAndRetry()
}
func (w *WebsocketClient) websocketConnectHandler(_ struct{}) {
w.log.Info("connected")
if len(w.config.ApiKey) != 0 {
cmd, err := websocketAuthCommand(w.config.ApiKey, w.config.ApiSecret)
if err != nil {
w.log.WithError(err).Fatal("unable to generate auth command")
os.Exit(1)
}
w.log.Info("sent auth command")
err = w.SendCommand(cmd)
if err != nil {
w.log.WithError(err).Fatal("unable to send auth")
os.Exit(1)
}
}
if len(w.topics) > 0 {
err := w.SendCommand(&types.Command{
Op: types.CommandOpSubscribe,
Args: w.topics.Args(),
})
if err != nil {
w.log.WithError(err).Fatal("unable to send subscription request")
os.Exit(1)
}
}
w.conMutex.Lock()
w.connected = true
w.conMutex.Unlock()
w.startHeartbeat()
}
func (w *WebsocketClient) websocketDisconnectHandler(_ struct{}) {
w.disconnectAndRetry()
}
func (w *WebsocketClient) disconnectAndRetry() {
w.log.Warn("disconnected")
w.heartbeat.Stop()
w.conMutex.Lock()
w.connected = false
w.conMutex.Unlock()
w.log.Info("retrying websocket connection in 30s")
time.Sleep(30 * time.Second)
w.Restart()
}
func (w *WebsocketClient) websocketMessageHandler(msg string) {
w.log.Tracef("x=== %s", msg)
if msg == "pong" {
return
}
raw := &types.CompositeResponse{}
if err := json.Unmarshal([]byte(msg), raw); err != nil {
w.log.WithError(err).Error("error decoding message")
return
}
switch {
case raw.IsAutoCancelResponse():
w.bus.Publish(types.EventApiResponseAutoCancel.String(), raw.ToAutoCancelResponse().WithRequest(raw.Request))
case raw.IsErrorResponse():
w.bus.Publish(types.EventApiResponseError.String(), raw.ToErrorResponse().WithRequest(raw.Request))
case raw.IsInfoResponse():
w.bus.Publish(types.EventApiResponseInfo.String(), raw.ToInfoResponse())
case raw.IsSubscriptionResponse():
w.bus.Publish(types.EventApiResponseSubscription.String(), raw.ToSubscriptionResponse().WithRequest(raw.Request))
case raw.IsSuccessResponse():
w.bus.Publish(types.EventApiResponseSuccess.String(), raw.ToSuccessResponse().WithRequest(raw.Request))
default:
w.log.Errorf("unknown message: %s", msg)
}
}
func (w *WebsocketClient) apiTopicHandler(obj interface{}) {
switch o := obj.(type) {
case *types.SuccessResponse:
fields := log.Fields{
"args": o.Request.Args,
}
if *o.Success {
w.log.WithFields(fields).Infof("%s operation successful", o.Request.Op)
} else {
w.log.WithFields(fields).Infof("%s operation failed'", o.Request.Op)
}
case *types.SubscriptionResponse:
w.bus.Publish(fmt.Sprintf("%s:%s", types.EventApiResponseSubscription, o.Table), o)
default:
w.log.Error("error casting apiTopic object")
}
}
func (w *WebsocketClient) startHeartbeat() {
w.log.Info("starting heartbeat")
w.heartbeatLock.Lock()
w.heartbeat = timer.NewTimer(2 * heartbeatRate)
w.heartbeatLock.Unlock()
ticker := time.NewTicker(heartbeatRate)
go func() {
<-w.heartbeat.C
ticker.Stop()
w.log.Warn("no response to heartbeat, restarting websocket")
w.Restart()
}()
go func() {
for {
<-ticker.C
w.socket.SendText("ping")
}
}()
}