Skip to main content

Module rate_limit

Module rate_limit 

Source
Expand description

Rate-limit keys, quotas, and limiters for the Lighter adapter.

Lighter meters requests against both the caller IP and the account L1 address.

REST reads draw on a per-client read quota. Transactions (sendTx / sendTxBatch) are metered in one venue bucket per account regardless of transport; single orders go over the WebSocket and batches over HTTP, so both share one LighterTxRateLimiter to keep their combined rate under the single venue transaction limit.

§WebSocket client messages

Non-transaction WS frames (subscribe, unsubscribe, resubscribe) face two independent per-IP caps: 200 messages per minute and 50 unacknowledged (inflight) messages. The adapter honours each with a separate mechanism:

  • Rate: one ws_message_rate_limiter per venue URL, shared by the data and execution clients so their combined send rate counts against a single bucket. It paces at the documented 200/min with a matching 50-message burst.
  • Inflight: a rate limiter cannot bound inflight, because the unacknowledged count tracks venue acknowledgement latency (multi-second and fat-tailed), not emission rate. The feed handler instead gates subscribe dispatch on a closed-loop count of unacknowledged subscribes (crate::common::consts::SUBSCRIBE_INFLIGHT_MAX), releasing a slot on each ack. Without it, a subscribe storm at startup or reconnect drives inflight past 50 and the venue returns 30009 / 30010.

sendTx is metered in the transaction bucket, not the WS message bucket.

Constants§

LIGHTER_REST_BUCKET
Rate-limit bucket key shared by all REST read endpoints.
LIGHTER_TX_BUCKET
Rate-limit bucket key for the venue transaction bucket.
LIGHTER_WS_MESSAGE_BUCKET
Rate-limit bucket key for non-transaction WebSocket client messages.
LIGHTER_WS_MESSAGE_BURST
Rate-limiter burst, at Lighter’s documented 50-message inflight cap. The closed-loop subscribe gate (crate::common::consts::SUBSCRIBE_INFLIGHT_MAX) is the real inflight bound; this burst only shapes send rate.
LIGHTER_WS_MESSAGE_RATE_PER_MIN
Lighter’s documented WebSocket message rate: 200 per IP per minute.

Statics§

LIGHTER_REST_QUOTA
Conservative Lighter REST rate limit for standard accounts.
LIGHTER_WS_MESSAGE_QUOTA
Lighter WebSocket client-message quota, excluding sendTx / sendTxBatch.
LIGHTER_WS_MESSAGE_RATE_LIMIT_KEY
Pre-interned rate-limit key for non-transaction WebSocket client messages.

Functions§

await_tx_quota
Awaits transaction-bucket capacity before a sendTx on either transport.
build_tx_rate_limiter
Builds the shared transaction limiter from a sendtx_quota_per_min override, keyed on LIGHTER_TX_BUCKET. Unset or zero falls back to the standard 60 req/min.
resolve_quota
Resolves a per-minute override to a quota, falling back to the conservative standard-account quota when unset or zero.
ws_message_rate_limiter
Returns the shared WS message limiter for url, creating it on first access. Subsequent calls with the same url return the same Arc.

Type Aliases§

LighterTxRateLimiter
Per-account transaction rate limiter, shared across the HTTP and WebSocket sendTx paths so their combined rate honours the single venue bucket.
LighterWsMessageRateLimiter
Shared WebSocket message limiter, keyed by venue WS URL. Both data and execution clients (and the backend balance poller) draw from one bucket per URL so their combined send rate honours the venue’s per-IP cap.