{"modulePath":"github.com/QuantProcessing/exchanges","version":"v0.1.4","isLatest":false,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package ratelimit\n\nPackage ratelimit provides a generic, declarative rate limiting engine.\n\nEach exchange declares its own RateLimitRule set and endpoint weights. The RateLimiter enforces these rules using a sliding window algorithm.\n\n## Types\n\n```go\ntype BucketStats struct {\n\tScope    LimitScope\n\tCategory LimitCategory\n\tWindows  []WindowStats\n}\n```\nBucketStats reports current usage for one rate-limit bucket.\n\n```go\ntype CategoryWeight struct {\n\tCategory LimitCategory\n\tWeight   int\n}\n```\nCategoryWeight is the weight a single method consumes in a specific category.\n\n```go\ntype LimitCategory string\n```\nLimitCategory defines which request types share a bucket.\n\n```go\nconst (\n\t// CategoryAll means all requests share one bucket.\n\tCategoryAll LimitCategory = \"all\"\n\t// CategoryQuery covers read/query requests (FetchTicker, FetchOrderBook, etc).\n\tCategoryQuery LimitCategory = \"query\"\n\t// CategoryTrade covers write/trade requests (PlaceOrder, CancelOrder, etc).\n\tCategoryTrade LimitCategory = \"trade\"\n\t// CategoryOrder adds an extra order-count bucket (e.g. Binance ORDER count).\n\tCategoryOrder LimitCategory = \"order\"\n)\n```\n\n```go\ntype LimitScope string\n```\nLimitScope defines what entity the limit tracks against.\n\n```go\nconst (\n\t// ScopeIP tracks requests per IP address.\n\tScopeIP LimitScope = \"ip\"\n\t// ScopeAccount tracks requests per account/wallet address.\n\tScopeAccount LimitScope = \"account\"\n)\n```\n\n```go\ntype Logger interface {\n\tDebugw(msg string, keysAndValues ...any)\n}\n```\nLogger is the logging interface for the ratelimit package.\n\n```go\ntype RateLimitRule struct {\n\tScope    LimitScope    // What entity is tracked (IP / Account)\n\tCategory LimitCategory // What request types count toward this bucket\n\tWindows  []Window      // One or more windows (requests must satisfy ALL windows)\n}\n```\nRateLimitRule describes one rate limit bucket for an exchanges. An exchange may declare multiple rules (e.g. IP query bucket + Account trade bucket).\n\n```go\ntype RateLimiter struct {\n\t// contains filtered or unexported fields\n}\n```\nRateLimiter manages one or more rate limit buckets for a single exchange adapter.\n\n```go\nfunc NewRateLimiter(rules []RateLimitRule, name string) *RateLimiter\n```\nNewRateLimiter creates a limiter from the exchange's declared rules.\n\n```go\nfunc NewRateLimiterWithLogger(rules []RateLimitRule, name string, logger Logger) *RateLimiter\n```\nNewRateLimiterWithLogger creates a limiter with a custom logger.\n\n```go\nfunc (rl *RateLimiter) Acquire(ctx context.Context, weights []CategoryWeight) error\n```\nAcquire blocks until all applicable buckets have enough capacity for the given weights, or until ctx is cancelled.\n\nIf no bucket matches a given weight's category, that weight is skipped.\n\n```go\nfunc (rl *RateLimiter) Stats() []BucketStats\n```\nStats returns current usage for all buckets.\n\n```go\nfunc (rl *RateLimiter) TryAcquire(weights []CategoryWeight) error\n```\nTryAcquire is a non-blocking version. Returns ErrRateLimited if insufficient capacity.\n\n```go\ntype Window struct {\n\tDuration time.Duration // Window duration (e.g. 1*time.Minute, 10*time.Second)\n\tLimit    int           // Max total weight allowed within this window\n}\n```\nWindow defines a single rate limit window.\n\n```go\ntype WindowStats struct {\n\tDuration  time.Duration\n\tLimit     int\n\tUsed      int\n\tRemaining int\n}\n```\nWindowStats reports usage within a single time window.\n\n","path":"github.com/QuantProcessing/exchanges/ratelimit","name":"ratelimit","synopsis":"Package ratelimit provides a generic, declarative rate limiting engine.","isRedistributable":true}
