Skip to content

Commit e1e39a5

Browse files
authored
Merge pull request #1651 from c9s/edwin/okx/add-conn-info-event
FEATURE: [okx] add conn info event
2 parents 907a1c8 + bafa5a4 commit e1e39a5

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

pkg/exchange/okex/parse.go

+21-4
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ func parseWebSocketEvent(in []byte) (interface{}, error) {
9999
type WsEventType string
100100

101101
const (
102-
WsEventTypeLogin = "login"
103-
WsEventTypeError = "error"
104-
WsEventTypeSubscribe = "subscribe"
105-
WsEventTypeUnsubscribe = "unsubscribe"
102+
WsEventTypeLogin WsEventType = "login"
103+
WsEventTypeError WsEventType = "error"
104+
WsEventTypeSubscribe WsEventType = "subscribe"
105+
WsEventTypeUnsubscribe WsEventType = "unsubscribe"
106+
WsEventTypeConnectionInfo WsEventType = "channel-conn-count"
107+
WsEventTypeConnectionError WsEventType = "channel-conn-count-error"
106108
)
107109

108110
type WebSocketEvent struct {
@@ -115,6 +117,8 @@ type WebSocketEvent struct {
115117
} `json:"arg,omitempty"`
116118
Data json.RawMessage `json:"data"`
117119
ActionType ActionType `json:"action"`
120+
Channel Channel `json:"channel"`
121+
ConnCount string `json:"connCount"`
118122
}
119123

120124
func (w *WebSocketEvent) IsValid() error {
@@ -133,6 +137,12 @@ func (w *WebSocketEvent) IsValid() error {
133137
}
134138
return nil
135139

140+
case WsEventTypeConnectionInfo:
141+
return nil
142+
143+
case WsEventTypeConnectionError:
144+
return fmt.Errorf("connection rate limit exceeded, channel: %s, connCount: %s", w.Channel, w.ConnCount)
145+
136146
default:
137147
return fmt.Errorf("unexpected event type: %+v", w)
138148
}
@@ -401,3 +411,10 @@ func (m *MarketTradeEvent) toGlobalTrade() (types.Trade, error) {
401411
FeeCurrency: "", // not supported
402412
}, nil
403413
}
414+
415+
type ConnectionInfoEvent struct {
416+
Event string `json:"event"`
417+
Channel Channel `json:"channel"`
418+
ConnCount string `json:"connCount"`
419+
ConnId string `json:"connId"`
420+
}

pkg/exchange/okex/parse_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,46 @@ func TestWebSocketEvent_IsValid(t *testing.T) {
849849

850850
assert.ErrorContains(t, opEvent.IsValid(), "unexpected event type")
851851
})
852+
853+
t.Run("conn count info", func(t *testing.T) {
854+
input := `{
855+
"event":"channel-conn-count",
856+
"channel":"orders",
857+
"connCount": "2",
858+
"connId":"abcd1234"
859+
}`
860+
res, err := parseWebSocketEvent([]byte(input))
861+
assert.NoError(t, err)
862+
opEvent, ok := res.(*WebSocketEvent)
863+
assert.True(t, ok)
864+
assert.Equal(t, WebSocketEvent{
865+
Event: "channel-conn-count",
866+
Channel: "orders",
867+
ConnCount: "2",
868+
}, *opEvent)
869+
870+
assert.NoError(t, opEvent.IsValid())
871+
})
872+
873+
t.Run("conn count error", func(t *testing.T) {
874+
input := `{
875+
"event": "channel-conn-count-error",
876+
"channel": "orders",
877+
"connCount": "20",
878+
"connId":"a4d3ae55"
879+
}`
880+
res, err := parseWebSocketEvent([]byte(input))
881+
assert.NoError(t, err)
882+
opEvent, ok := res.(*WebSocketEvent)
883+
assert.True(t, ok)
884+
assert.Equal(t, WebSocketEvent{
885+
Event: "channel-conn-count-error",
886+
Channel: "orders",
887+
ConnCount: "20",
888+
}, *opEvent)
889+
890+
assert.ErrorContains(t, opEvent.IsValid(), "rate limit")
891+
})
852892
}
853893

854894
func TestOrderTradeEvent(t *testing.T) {

pkg/exchange/okex/stream.go

+3
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ func (s *Stream) subscribePrivateChannels(next func()) func() {
196196
{Channel: "orders", InstrumentType: string(okexapi.InstrumentTypeSpot)},
197197
}
198198

199+
// https://www.okx.com/docs-v5/zh/#overview-websocket-connect
200+
// **NOTICE** 2024/06/03 Since the number of channels we are currently subscribed to is far less
201+
// than the rate limit of 20, rate limiting is not supported for now.
199202
log.Infof("subscribing private channels: %+v", subs)
200203
err := s.Conn.WriteJSON(WebsocketOp{
201204
Op: "subscribe",

0 commit comments

Comments
 (0)