-
Notifications
You must be signed in to change notification settings - Fork 787
/
websocket_types.go
157 lines (129 loc) · 4.62 KB
/
websocket_types.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
package stream
import (
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/exchanges/fill"
"github.com/thrasher-corp/gocryptotrader/exchanges/protocol"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream/buffer"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
)
// Websocket functionality list and state consts
const (
WebsocketNotAuthenticatedUsingRest = "%v - Websocket not authenticated, using REST\n"
Ping = "ping"
Pong = "pong"
UnhandledMessage = " - Unhandled websocket message: "
)
type subscriptionMap map[any]*subscription.Subscription
const (
uninitialised uint32 = iota
disconnected
connecting
connected
)
// Websocket defines a return type for websocket connections via the interface
// wrapper for routine processing
type Websocket struct {
canUseAuthenticatedEndpoints atomic.Bool
enabled atomic.Bool
state atomic.Uint32
verbose bool
connectionMonitorRunning atomic.Bool
trafficMonitorRunning atomic.Bool
dataMonitorRunning atomic.Bool
trafficTimeout time.Duration
connectionMonitorDelay time.Duration
proxyAddr string
defaultURL string
defaultURLAuth string
runningURL string
runningURLAuth string
exchangeName string
m sync.Mutex
connector func() error
subscriptionMutex sync.RWMutex
subscriptions subscriptionMap
Subscribe chan []subscription.Subscription
Unsubscribe chan []subscription.Subscription
// Subscriber function for package defined websocket subscriber
// functionality
Subscriber func([]subscription.Subscription) error
// Unsubscriber function for packaged defined websocket unsubscriber
// functionality
Unsubscriber func([]subscription.Subscription) error
// GenerateSubs function for package defined websocket generate
// subscriptions functionality
GenerateSubs func() ([]subscription.Subscription, error)
DataHandler chan interface{}
ToRoutine chan interface{}
Match *Match
// shutdown synchronises shutdown event across routines
ShutdownC chan struct{}
Wg *sync.WaitGroup
// Orderbook is a local buffer of orderbooks
Orderbook buffer.Orderbook
// Trade is a notifier of occurring trades
Trade trade.Trade
// Fills is a notifier of occurring fills
Fills fill.Fills
// trafficAlert monitors if there is a halt in traffic throughput
TrafficAlert chan struct{}
// ReadMessageErrors will received all errors from ws.ReadMessage() and
// verify if its a disconnection
ReadMessageErrors chan error
features *protocol.Features
// Standard stream connection
Conn Connection
// Authenticated stream connection
AuthConn Connection
// Latency reporter
ExchangeLevelReporter Reporter
// MaxSubScriptionsPerConnection defines the maximum number of
// subscriptions per connection that is allowed by the exchange.
MaxSubscriptionsPerConnection int
}
// WebsocketSetup defines variables for setting up a websocket connection
type WebsocketSetup struct {
ExchangeConfig *config.Exchange
DefaultURL string
RunningURL string
RunningURLAuth string
Connector func() error
Subscriber func([]subscription.Subscription) error
Unsubscriber func([]subscription.Subscription) error
GenerateSubscriptions func() ([]subscription.Subscription, error)
Features *protocol.Features
// Local orderbook buffer config values
OrderbookBufferConfig buffer.Config
TradeFeed bool
// Fill data config values
FillsFeed bool
// MaxWebsocketSubscriptionsPerConnection defines the maximum number of
// subscriptions per connection that is allowed by the exchange.
MaxWebsocketSubscriptionsPerConnection int
}
// WebsocketConnection contains all the data needed to send a message to a WS
// connection
type WebsocketConnection struct {
Verbose bool
connected int32
// Gorilla websocket does not allow more than one goroutine to utilise
// writes methods
writeControl sync.Mutex
RateLimit int64
ExchangeName string
URL string
ProxyURL string
Wg *sync.WaitGroup
Connection *websocket.Conn
ShutdownC chan struct{}
Match *Match
ResponseMaxLimit time.Duration
Traffic chan struct{}
readMessageErrors chan error
Reporter Reporter
}