Documentation
¶
Overview ¶
Package ratelimit provides a generic, declarative rate limiting engine.
Each exchange declares its own RateLimitRule set and endpoint weights. The RateLimiter enforces these rules using a sliding window algorithm.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BucketStats ¶
type BucketStats struct {
Scope LimitScope
Category LimitCategory
Windows []WindowStats
}
BucketStats reports current usage for one rate-limit bucket.
type CategoryWeight ¶
type CategoryWeight struct {
Category LimitCategory
Weight int
}
CategoryWeight is the weight a single method consumes in a specific category.
type LimitCategory ¶
type LimitCategory string
LimitCategory defines which request types share a bucket.
const ( // CategoryAll means all requests share one bucket. CategoryAll LimitCategory = "all" // CategoryQuery covers read/query requests (FetchTicker, FetchOrderBook, etc). CategoryQuery LimitCategory = "query" // CategoryTrade covers write/trade requests (PlaceOrder, CancelOrder, etc). CategoryTrade LimitCategory = "trade" // CategoryOrder adds an extra order-count bucket (e.g. Binance ORDER count). CategoryOrder LimitCategory = "order" )
type LimitScope ¶
type LimitScope string
LimitScope defines what entity the limit tracks against.
const ( // ScopeIP tracks requests per IP address. ScopeIP LimitScope = "ip" // ScopeAccount tracks requests per account/wallet address. ScopeAccount LimitScope = "account" )
type RateLimitRule ¶
type RateLimitRule struct {
Scope LimitScope // What entity is tracked (IP / Account)
Category LimitCategory // What request types count toward this bucket
Windows []Window // One or more windows (requests must satisfy ALL windows)
}
RateLimitRule describes one rate limit bucket for an exchanges. An exchange may declare multiple rules (e.g. IP query bucket + Account trade bucket).
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter manages one or more rate limit buckets for a single exchange adapter.
func NewRateLimiter ¶
func NewRateLimiter(rules []RateLimitRule, name string) *RateLimiter
NewRateLimiter creates a limiter from the exchange's declared rules.
func NewRateLimiterWithLogger ¶
func NewRateLimiterWithLogger(rules []RateLimitRule, name string, logger Logger) *RateLimiter
NewRateLimiterWithLogger creates a limiter with a custom logger.
func (*RateLimiter) Acquire ¶
func (rl *RateLimiter) Acquire(ctx context.Context, weights []CategoryWeight) error
Acquire blocks until all applicable buckets have enough capacity for the given weights, or until ctx is cancelled.
If no bucket matches a given weight's category, that weight is skipped.
func (*RateLimiter) Stats ¶
func (rl *RateLimiter) Stats() []BucketStats
Stats returns current usage for all buckets.
func (*RateLimiter) TryAcquire ¶
func (rl *RateLimiter) TryAcquire(weights []CategoryWeight) error
TryAcquire is a non-blocking version. Returns ErrRateLimited if insufficient capacity.