Documentation
¶
Overview ¶
Package ratelimit implements rate limiting.
Index ¶
- type InMemoryLimiter
- func (r *InMemoryLimiter) Allow(ctx context.Context, key ratelimit.RateLimitKey) (*ratelimit.RateLimitResult, error)
- func (r *InMemoryLimiter) Consume(ctx context.Context, key ratelimit.RateLimitKey, tokens int64) error
- func (r *InMemoryLimiter) Remaining(ctx context.Context, key ratelimit.RateLimitKey) (int64, error)
- func (r *InMemoryLimiter) Reset(ctx context.Context, key ratelimit.RateLimitKey) error
- func (r *InMemoryLimiter) SetQuota(ctx context.Context, tenantID string, config *ratelimit.QuotaConfig) error
- type RedisLimiter
- func (r *RedisLimiter) Allow(ctx context.Context, key ratelimit.RateLimitKey) (*ratelimit.RateLimitResult, error)
- func (r *RedisLimiter) Consume(ctx context.Context, key ratelimit.RateLimitKey, tokens int64) error
- func (r *RedisLimiter) Remaining(ctx context.Context, key ratelimit.RateLimitKey) (int64, error)
- func (r *RedisLimiter) Reset(ctx context.Context, key ratelimit.RateLimitKey) error
- func (r *RedisLimiter) SetQuota(ctx context.Context, tenantID string, config *ratelimit.QuotaConfig) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type InMemoryLimiter ¶
type InMemoryLimiter struct {
// contains filtered or unexported fields
}
InMemoryLimiter implements RateLimiter using an in-memory map.
func NewInMemoryLimiter ¶
func NewInMemoryLimiter() *InMemoryLimiter
NewInMemoryLimiter creates a new rate limiter.
func (*InMemoryLimiter) Allow ¶
func (r *InMemoryLimiter) Allow(ctx context.Context, key ratelimit.RateLimitKey) (*ratelimit.RateLimitResult, error)
Allow checks if the request is allowed.
func (*InMemoryLimiter) Consume ¶
func (r *InMemoryLimiter) Consume(ctx context.Context, key ratelimit.RateLimitKey, tokens int64) error
Consume deducts tokens from the quota.
func (*InMemoryLimiter) Remaining ¶
func (r *InMemoryLimiter) Remaining(ctx context.Context, key ratelimit.RateLimitKey) (int64, error)
Remaining returns the remaining quota.
func (*InMemoryLimiter) Reset ¶
func (r *InMemoryLimiter) Reset(ctx context.Context, key ratelimit.RateLimitKey) error
Reset resets the quota for a key.
func (*InMemoryLimiter) SetQuota ¶
func (r *InMemoryLimiter) SetQuota(ctx context.Context, tenantID string, config *ratelimit.QuotaConfig) error
SetQuota sets the quota config for a tenant.
type RedisLimiter ¶
type RedisLimiter struct {
// contains filtered or unexported fields
}
RedisLimiter implements RateLimiter using Redis.
func NewRedisLimiter ¶
func NewRedisLimiter(client *redis.Client) *RedisLimiter
NewRedisLimiter creates a new Redis rate limiter.
func (*RedisLimiter) Allow ¶
func (r *RedisLimiter) Allow(ctx context.Context, key ratelimit.RateLimitKey) (*ratelimit.RateLimitResult, error)
Allow checks if the request is allowed.
func (*RedisLimiter) Consume ¶
func (r *RedisLimiter) Consume(ctx context.Context, key ratelimit.RateLimitKey, tokens int64) error
Consume deducts tokens from the quota.
func (*RedisLimiter) Remaining ¶
func (r *RedisLimiter) Remaining(ctx context.Context, key ratelimit.RateLimitKey) (int64, error)
Remaining returns the remaining quota.
func (*RedisLimiter) Reset ¶
func (r *RedisLimiter) Reset(ctx context.Context, key ratelimit.RateLimitKey) error
Reset resets the quota for a key.
func (*RedisLimiter) SetQuota ¶
func (r *RedisLimiter) SetQuota(ctx context.Context, tenantID string, config *ratelimit.QuotaConfig) error
SetQuota stores the quota config in Redis.