Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: [okx] add conn info event #1651

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions pkg/exchange/okex/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ func parseWebSocketEvent(in []byte) (interface{}, error) {
type WsEventType string

const (
WsEventTypeLogin = "login"
WsEventTypeError = "error"
WsEventTypeSubscribe = "subscribe"
WsEventTypeUnsubscribe = "unsubscribe"
WsEventTypeLogin WsEventType = "login"
WsEventTypeError WsEventType = "error"
WsEventTypeSubscribe WsEventType = "subscribe"
WsEventTypeUnsubscribe WsEventType = "unsubscribe"
WsEventTypeConnectionInfo WsEventType = "channel-conn-count"
WsEventTypeConnectionError WsEventType = "channel-conn-count-error"
)

type WebSocketEvent struct {
Expand All @@ -115,6 +117,8 @@ type WebSocketEvent struct {
} `json:"arg,omitempty"`
Data json.RawMessage `json:"data"`
ActionType ActionType `json:"action"`
Channel Channel `json:"channel"`
ConnCount string `json:"connCount"`
}

func (w *WebSocketEvent) IsValid() error {
Expand All @@ -133,6 +137,12 @@ func (w *WebSocketEvent) IsValid() error {
}
return nil

case WsEventTypeConnectionInfo:
return nil

case WsEventTypeConnectionError:
return fmt.Errorf("connection rate limit exceeded, channel: %s, connCount: %s", w.Channel, w.ConnCount)

default:
return fmt.Errorf("unexpected event type: %+v", w)
}
Expand Down Expand Up @@ -401,3 +411,10 @@ func (m *MarketTradeEvent) toGlobalTrade() (types.Trade, error) {
FeeCurrency: "", // not supported
}, nil
}

type ConnectionInfoEvent struct {
Event string `json:"event"`
Channel Channel `json:"channel"`
ConnCount string `json:"connCount"`
ConnId string `json:"connId"`
}
40 changes: 40 additions & 0 deletions pkg/exchange/okex/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,46 @@ func TestWebSocketEvent_IsValid(t *testing.T) {

assert.ErrorContains(t, opEvent.IsValid(), "unexpected event type")
})

t.Run("conn count info", func(t *testing.T) {
input := `{
"event":"channel-conn-count",
"channel":"orders",
"connCount": "2",
"connId":"abcd1234"
}`
res, err := parseWebSocketEvent([]byte(input))
assert.NoError(t, err)
opEvent, ok := res.(*WebSocketEvent)
assert.True(t, ok)
assert.Equal(t, WebSocketEvent{
Event: "channel-conn-count",
Channel: "orders",
ConnCount: "2",
}, *opEvent)

assert.NoError(t, opEvent.IsValid())
})

t.Run("conn count error", func(t *testing.T) {
input := `{
"event": "channel-conn-count-error",
"channel": "orders",
"connCount": "20",
"connId":"a4d3ae55"
}`
res, err := parseWebSocketEvent([]byte(input))
assert.NoError(t, err)
opEvent, ok := res.(*WebSocketEvent)
assert.True(t, ok)
assert.Equal(t, WebSocketEvent{
Event: "channel-conn-count-error",
Channel: "orders",
ConnCount: "20",
}, *opEvent)

assert.ErrorContains(t, opEvent.IsValid(), "rate limit")
})
}

func TestOrderTradeEvent(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions pkg/exchange/okex/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func (s *Stream) subscribePrivateChannels(next func()) func() {
{Channel: "orders", InstrumentType: string(okexapi.InstrumentTypeSpot)},
}

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