pkg/exchange: add conn count info event

This commit is contained in:
edwin 2024-06-03 17:01:57 +08:00
parent 907a1c8c53
commit 57618ced7c
2 changed files with 61 additions and 4 deletions

View File

@ -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 {
@ -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 {
@ -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)
}
@ -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"`
}

View File

@ -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) {