Documentation
¶
Index ¶
- type Cache
- type ClientLimiter
- type Config
- type Limiter
- func (l *Limiter) CheckAllowed(ip, destination, identifier string, maxReqs ...int) int
- func (l *Limiter) Clear()
- func (l *Limiter) Close() error
- func (l *Limiter) IsAllowed(ip, destination, identifier string, maxReqs ...int) int
- func (l *Limiter) IsAllowedWithDecrement(ip, destination, identifier string, decrement bool, maxReqs ...int) int
- type MemoryCache
- type RedisCache
- type RedisConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface {
// Get retrieves a value from the cache
// Returns the ClientLimiter if found, nil if not found
Get(key uint64) *ClientLimiter
// Set stores a value in the cache
// The value will be stored with the specified key
Set(key uint64, value *ClientLimiter)
// Delete removes a value from the cache
// If the key doesn't exist, this is a no-op
Delete(key uint64)
// Clear removes all entries from the cache
Clear()
// Close cleans up any resources used by the cache
// Returns an error if cleanup fails
Close() error
}
Cache represents a key-value cache interface for rate limiting
type ClientLimiter ¶
type ClientLimiter struct {
// contains filtered or unexported fields
}
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
func NewWithCache ¶
NewWithCache creates a limiter with a custom cache implementation
func NewWithRedis ¶
func NewWithRedis(config Config, redisConfig RedisConfig) (*Limiter, error)
NewWithRedis creates a limiter with Redis cache
func (*Limiter) CheckAllowed ¶ added in v1.4.0
CheckAllowed performs the same rate limiting check as IsAllowed but doesn't count against the user's requests
func (*Limiter) Clear ¶ added in v1.3.0
func (l *Limiter) Clear()
Clear removes all entries from the rate limiter cache
type MemoryCache ¶
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache implements the Cache interface using an in-memory LRU cache
func NewMemoryCache ¶
func NewMemoryCache(capacity int, expiration time.Duration) *MemoryCache
NewMemoryCache creates a new memory cache with the specified capacity and expiration
func (*MemoryCache) Clear ¶ added in v1.3.0
func (m *MemoryCache) Clear()
Clear removes all entries from the cache
func (*MemoryCache) Close ¶
func (m *MemoryCache) Close() error
Close cleans up any resources used by the cache
func (*MemoryCache) Delete ¶
func (m *MemoryCache) Delete(key uint64)
Delete removes a value from the cache
func (*MemoryCache) Get ¶
func (m *MemoryCache) Get(key uint64) *ClientLimiter
Get retrieves a value from the cache
func (*MemoryCache) Set ¶
func (m *MemoryCache) Set(key uint64, value *ClientLimiter)
Set stores a value in the cache
type RedisCache ¶
type RedisCache struct {
// contains filtered or unexported fields
}
RedisCache implements the Cache interface using Redis
func NewRedisCache ¶
func NewRedisCache(config RedisConfig) (*RedisCache, error)
NewRedisCache creates a new Redis cache with the specified configuration
func (*RedisCache) Clear ¶ added in v1.3.0
func (r *RedisCache) Clear()
Clear removes all entries from the cache by deleting all keys with the prefix
func (*RedisCache) Delete ¶
func (r *RedisCache) Delete(key uint64)
Delete removes a value from Redis
func (*RedisCache) Get ¶
func (r *RedisCache) Get(key uint64) *ClientLimiter
Get retrieves a value from Redis
func (*RedisCache) Set ¶
func (r *RedisCache) Set(key uint64, value *ClientLimiter)
Set stores a value in Redis
type RedisConfig ¶
type RedisConfig struct {
Addr string // Redis server address (e.g., "localhost:6379")
Password string // Redis password (empty for no auth)
DB int // Redis database number
KeyPrefix string // Prefix for all keys (e.g., "ratelimit:")
TTL time.Duration // Default TTL for keys
}
RedisConfig holds configuration for the Redis cache