services

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

Types

type DatabaseProvider

type DatabaseProvider struct {
	// contains filtered or unexported fields
}

DatabaseProvider is a database-backed rate limit provider for persistent rate limiting

func NewDatabaseProvider

func NewDatabaseProvider(db bun.IDB) (*DatabaseProvider, error)

NewDatabaseProvider creates a new database rate limit provider

func NewDatabaseProviderWithConfig

func NewDatabaseProviderWithConfig(db bun.IDB, config types.DatabaseStorageConfig) (*DatabaseProvider, error)

NewDatabaseProviderWithConfig creates a new database rate limit provider with custom config

func (*DatabaseProvider) CheckAndIncrement

func (p *DatabaseProvider) CheckAndIncrement(ctx context.Context, key string, window time.Duration, maxRequests int) (bool, int, time.Time, error)

CheckAndIncrement checks if a request is allowed and increments the counter

func (*DatabaseProvider) Close

func (p *DatabaseProvider) Close() error

Close closes the provider (no-op since we don't own the database connection)

func (*DatabaseProvider) DeleteRule

func (p *DatabaseProvider) DeleteRule(ctx context.Context, key string) error

DeleteRule removes the stored rule for a key.

func (*DatabaseProvider) GetName

func (p *DatabaseProvider) GetName() string

GetName returns the provider name

func (*DatabaseProvider) GetRule

func (p *DatabaseProvider) GetRule(ctx context.Context, key string) (time.Duration, int, bool, error)

GetRule retrieves a stored per-key rule. Returns 0, 0, false, nil when not found.

func (*DatabaseProvider) SetRule

func (p *DatabaseProvider) SetRule(ctx context.Context, key string, window time.Duration, maxRequests int) error

SetRule stores a per-key rate-limit rule without consuming quota.

type InMemoryProvider

type InMemoryProvider struct {
	// contains filtered or unexported fields
}

func NewInMemoryProvider

func NewInMemoryProvider() *InMemoryProvider

func NewInMemoryProviderWithConfig

func NewInMemoryProviderWithConfig(config types.MemoryStorageConfig) *InMemoryProvider

func (*InMemoryProvider) CheckAndIncrement

func (p *InMemoryProvider) CheckAndIncrement(ctx context.Context, key string, window time.Duration, maxRequests int) (bool, int, time.Time, error)

func (*InMemoryProvider) Close

func (p *InMemoryProvider) Close() error

func (*InMemoryProvider) DeleteRule

func (p *InMemoryProvider) DeleteRule(_ context.Context, key string) error

func (*InMemoryProvider) GetName

func (p *InMemoryProvider) GetName() string

func (*InMemoryProvider) GetRule

func (p *InMemoryProvider) GetRule(_ context.Context, key string) (time.Duration, int, bool, error)

func (*InMemoryProvider) SetRule

func (p *InMemoryProvider) SetRule(_ context.Context, key string, window time.Duration, maxRequests int) error

type RateLimiterService

type RateLimiterService interface {
	CheckAndIncrement(ctx context.Context, key string, window time.Duration, maxRequests int) (bool, int, time.Time, error)
	SetRule(ctx context.Context, key string, window time.Duration, maxRequests int) error
	GetRule(ctx context.Context, key string) (window time.Duration, maxRequests int, found bool, err error)
	DeleteRule(ctx context.Context, key string) error
}

type SecondaryStorageProvider

type SecondaryStorageProvider struct {
	// contains filtered or unexported fields
}

SecondaryStorageProvider wraps a SecondaryStorage backend for rate limiting This allows rate limits to use distributed storage (Redis, database) instead of in-memory

func NewSecondaryStorageProvider

func NewSecondaryStorageProvider(name string, storage models.SecondaryStorage) *SecondaryStorageProvider

NewSecondaryStorageProvider creates a new provider wrapping a SecondaryStorage backend

func (*SecondaryStorageProvider) CheckAndIncrement

func (p *SecondaryStorageProvider) CheckAndIncrement(ctx context.Context, key string, window time.Duration, maxRequests int) (bool, int, time.Time, error)

CheckAndIncrement checks if a request is allowed and increments the counter

func (*SecondaryStorageProvider) Close

func (p *SecondaryStorageProvider) Close() error

Close closes the provider (no-op since we don't own the storage)

func (*SecondaryStorageProvider) DeleteRule

func (p *SecondaryStorageProvider) DeleteRule(ctx context.Context, key string) error

DeleteRule removes the stored rule for a key.

func (*SecondaryStorageProvider) GetName

func (p *SecondaryStorageProvider) GetName() string

GetName returns the provider name

func (*SecondaryStorageProvider) GetRule

GetRule retrieves a stored per-key rule. Returns 0, 0, false, nil when not found.

func (*SecondaryStorageProvider) SetRule

func (p *SecondaryStorageProvider) SetRule(ctx context.Context, key string, window time.Duration, maxRequests int) error

SetRule stores a per-key rate-limit rule without consuming quota. The rule is persisted under the key prefix "rule:" with no TTL.

Jump to

Keyboard shortcuts

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