Documentation
¶
Overview ¶
Package ratelimit implements rate limiting.
Package ratelimit implements rate limiting.
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
- type SQLiteQuotaStore
- type SQLiteRateLimiter
- func (l *SQLiteRateLimiter) Allow(ctx context.Context, key ratelimit.RateLimitKey) (*ratelimit.RateLimitResult, error)
- func (l *SQLiteRateLimiter) Consume(ctx context.Context, key ratelimit.RateLimitKey, tokens int64) error
- func (l *SQLiteRateLimiter) Remaining(ctx context.Context, key ratelimit.RateLimitKey) (int64, error)
- func (l *SQLiteRateLimiter) Reset(ctx context.Context, key ratelimit.RateLimitKey) 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.
type SQLiteQuotaStore ¶
type SQLiteQuotaStore struct {
// contains filtered or unexported fields
}
SQLiteQuotaStore implements ratelimit.QuotaStore using SQLite.
func NewSQLiteQuotaStore ¶
func NewSQLiteQuotaStore(db *sql.DB) *SQLiteQuotaStore
NewSQLiteQuotaStore creates a new SQLite-backed quota store.
func (*SQLiteQuotaStore) GetQuota ¶
func (s *SQLiteQuotaStore) GetQuota(ctx context.Context, tenantID string) (*ratelimit.QuotaConfig, error)
GetQuota retrieves the quota configuration for a tenant.
func (*SQLiteQuotaStore) SetQuota ¶
func (s *SQLiteQuotaStore) SetQuota(ctx context.Context, tenantID string, config *ratelimit.QuotaConfig) error
SetQuota creates or updates the quota configuration for a tenant.
type SQLiteRateLimiter ¶
type SQLiteRateLimiter struct {
// contains filtered or unexported fields
}
SQLiteRateLimiter implements ratelimit.RateLimiter using a SQLite-backed token bucket.
func NewSQLiteRateLimiter ¶
func NewSQLiteRateLimiter(db *sql.DB, quotaStore ratelimit.QuotaStore) *SQLiteRateLimiter
NewSQLiteRateLimiter creates a new SQLite-backed rate limiter.
func (*SQLiteRateLimiter) Allow ¶
func (l *SQLiteRateLimiter) Allow(ctx context.Context, key ratelimit.RateLimitKey) (*ratelimit.RateLimitResult, error)
Allow checks if the request is allowed without consuming quota. This is a read-only operation — uses a simple SELECT like Remaining.
func (*SQLiteRateLimiter) Consume ¶
func (l *SQLiteRateLimiter) Consume(ctx context.Context, key ratelimit.RateLimitKey, tokens int64) error
Consume deducts the specified number of tokens from the quota.
func (*SQLiteRateLimiter) Remaining ¶
func (l *SQLiteRateLimiter) Remaining(ctx context.Context, key ratelimit.RateLimitKey) (int64, error)
Remaining returns the remaining quota (read-only, no side effects).
func (*SQLiteRateLimiter) Reset ¶
func (l *SQLiteRateLimiter) Reset(ctx context.Context, key ratelimit.RateLimitKey) error
Reset deletes the rate limit bucket so the next request starts fresh.