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

server/comms: add new routes to limiter #1288

Merged
merged 2 commits into from Nov 24, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions dex/msgjson/types.go
Expand Up @@ -181,8 +181,8 @@ const (
// PenaltyRoute is the DEX-originating notification-type message
// informing of a broken rule and the resulting penalty.
PenaltyRoute = "penalty"
// SpotsRoute is the HTTP or WebSocket request to get the spot price and
// volume for the DEX's markets.
// SpotsRoute is the client-originating HTTP or WebSocket request to get the
// spot price and volume for the DEX's markets.
SpotsRoute = "spots"
// FeeRateRoute is the client-originating request asking for the most
// recently recorded transaction fee estimate for an asset.
Expand Down
23 changes: 15 additions & 8 deletions server/comms/server.go
Expand Up @@ -51,11 +51,12 @@ const (
// Per-websocket-connection limits in requests per second. Rate should be a
// reasonable sustained rate, while burst should consider bulk reconnect
// operations. Consider which routes are authenticated when setting these.
wsRateStatus, wsBurstStatus = 10, 500 // order_status and match_status (combined)
wsRateOrder, wsBurstOrder = 5, 100 // market, limit, and cancel (combined)
wsRateInfo, wsBurstInfo = 10, 200 // low-cost route limiter for: config, fee_rate (combined)
wsRateBook, wsBurstBook = 1, 100 // orderbook, assuming 100 markets subscribed max
wsRateRegister, wsBurstRegister = 1 / 60.0, 1 // register, rate.Every(time.Minute) but const
wsRateStatus, wsBurstStatus = 10, 500 // order_status and match_status (combined)
wsRateOrder, wsBurstOrder = 5, 100 // market, limit, and cancel (combined)
wsRateInfo, wsBurstInfo = 10, 200 // low-cost route limiter for: config, fee_rate, spots, candles (combined)
wsRateSubs, wsBurstSubs = 1 / 2.0, 100 // subscriptions: orderbook and price feed (combined)
wsRateRegister, wsBurstRegister = 1 / 60.0, 1 // register, rate.Every(time.Minute) but const
wsRateConnect, wsBurstConnect = 1 / 5.0, 100 // connect, account discovery requires bursts - (*Core).discoverAccount
// The cumulative rates below would need to be less than sum of above to
// actually trip unless it is also applied to unspecified routes.
wsRateTotal, wsBurstTotal = 40, 1000
Expand Down Expand Up @@ -212,9 +213,12 @@ func newRouteLimiter() *routeLimiter {
statusLimiter := rate.NewLimiter(wsRateStatus, wsBurstStatus)
orderLimiter := rate.NewLimiter(wsRateOrder, wsBurstOrder)
infoLimiter := rate.NewLimiter(wsRateInfo, wsBurstInfo)
marketSubsLimiter := rate.NewLimiter(wsRateSubs, wsBurstSubs)
return &routeLimiter{
cumulative: rate.NewLimiter(wsRateTotal, wsBurstTotal),
routes: map[string]allower{
// Connect (authorize) route
msgjson.ConnectRoute: rate.NewLimiter(wsRateConnect, wsBurstConnect),
// Meter the 'register' route the most.
msgjson.RegisterRoute: rate.NewLimiter(wsRateRegister, wsBurstRegister),
// Status checking of matches and orders
Expand All @@ -224,11 +228,14 @@ func newRouteLimiter() *routeLimiter {
msgjson.LimitRoute: orderLimiter,
msgjson.MarketRoute: orderLimiter,
msgjson.CancelRoute: orderLimiter,
// Order book subscription with full snapshot
msgjson.OrderBookRoute: rate.NewLimiter(wsRateBook, wsBurstBook),
// Config and fee rate
// Order book and price feed subscriptions
msgjson.OrderBookRoute: marketSubsLimiter,
msgjson.PriceFeedRoute: marketSubsLimiter,
// Config, fee rate, spot prices, and candles
msgjson.FeeRateRoute: infoLimiter,
msgjson.ConfigRoute: infoLimiter,
msgjson.SpotsRoute: infoLimiter,
msgjson.CandlesRoute: infoLimiter,
},
}
}
Expand Down