This repository has been archived by the owner on Oct 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
145 lines (122 loc) · 3.19 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
package dola
import (
"context"
"errors"
"fmt"
"github.com/rs/zerolog/log"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
)
var (
ErrWebsocketNotSupported = errors.New("websocket not supported")
ErrWebsocketNotEnabled = errors.New("websocket is not enabled")
)
func Stream(ctx context.Context, k *Keep, e exchange.IBotExchange, s Strategy) error {
ws, err := openWebsocket(e)
if err != nil {
return err
}
// Init strategy for this exchange.
if err := s.Init(k, e); err != nil {
return err
}
// This goroutine never, I repeat, *never* finishes.
for data := range ws.ToRoutine {
err := handleData(k, e, s, data)
if err != nil {
What(log.Error().
Err(err),
"error handling data")
}
}
// Deinit strategy for this exchange.
if err := s.Deinit(k, e); err != nil {
return err
}
panic("unexpected end of channel")
}
// handleData resembles github.com/thrasher-corp/gocryptotrader.engine.websocketRoutineManager.WebsocketDataHandler.
//
//nolint:cyclop
func handleData(k *Keep, e exchange.IBotExchange, s Strategy, data interface{}) error {
switch x := data.(type) {
case string:
unhandledType(data, true)
case error:
return x
case stream.FundingData:
handleError("OnFunding", s.OnFunding(k, e, x))
case *ticker.Price:
handleError("OnPrice", s.OnPrice(k, e, *x))
case stream.KlineData:
handleError("OnKline", s.OnKline(k, e, x))
case *orderbook.Base:
handleError("OnOrderBook", s.OnOrderBook(k, e, *x))
case *order.Detail:
k.OnOrder(e, *x)
handleError("OnOrder", s.OnOrder(k, e, *x))
case *order.Modify:
handleError("OnModify", s.OnModify(k, e, *x))
case order.ClassificationError:
unhandledType(data, true)
if x.Err == nil {
panic("unexpected error")
}
return x.Err
case stream.UnhandledMessageWarning:
unhandledType(data, true)
case account.Change:
handleError("OnBalanceChange", s.OnBalanceChange(k, e, x))
default:
handleError("OnUnrecognized", s.OnUnrecognized(k, e, data))
}
return nil
}
func handleError(method string, err error) {
if err != nil {
What(log.Warn().
Err(err).
Str("method", method),
"method failed")
}
}
func openWebsocket(e exchange.IBotExchange) (*stream.Websocket, error) {
// Check whether websocket is enabled.
if !e.IsWebsocketEnabled() {
return nil, ErrWebsocketNotEnabled
}
// Check whether websocket is supported.
if !e.SupportsWebsocket() {
return nil, ErrWebsocketNotSupported
}
// Instantiate a websocket.
ws, err := e.GetWebsocket()
if err != nil {
return nil, err
}
// Connect.
if !ws.IsConnecting() && !ws.IsConnected() {
err = ws.Connect()
if err != nil {
return nil, err
}
err = ws.FlushChannels()
if err != nil {
return nil, err
}
}
return ws, nil
}
func unhandledType(data interface{}, warn bool) {
e := log.Debug()
if warn {
e = log.Warn()
}
t := fmt.Sprintf("%T", data)
e = e.Interface("data", data).Str("type", t)
What(e, "unhandled type")
}