ratelimit

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package ratelimit implements rate limiting.

Package ratelimit implements rate limiting.

Package ratelimit implements rate limiting.

Index

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

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

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

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

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

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

Remaining returns the remaining quota (read-only, no side effects).

func (*SQLiteRateLimiter) Reset

Reset deletes the rate limit bucket so the next request starts fresh.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL