Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type InMemoryRateLimiterStorage ¶
type InMemoryRateLimiterStorage struct {
// contains filtered or unexported fields
}
InMemoryRateLimiterStorage implements RateLimiterStorage using in-memory token buckets. This can be easily replaced with a Redis-based implementation for distributed rate limiting.
func NewInMemoryRateLimiterStorage ¶
func NewInMemoryRateLimiterStorage() *InMemoryRateLimiterStorage
NewInMemoryRateLimiterStorage creates a new in-memory rate limiter storage. It includes a background cleanup goroutine to remove unused buckets.
func (*InMemoryRateLimiterStorage) Allow ¶
func (s *InMemoryRateLimiterStorage) Allow(ctx context.Context, virtualKeyID string, rateLimit gateway.RateLimit) (bool, error)
Allow checks if a request is allowed and consumes a token if available.
func (*InMemoryRateLimiterStorage) Stop ¶
func (s *InMemoryRateLimiterStorage) Stop()
Stop stops the background cleanup goroutine. Call this when shutting down.
type RateLimiterStorage ¶
type RateLimiterStorage interface {
// Allow checks if a request is allowed and consumes a token if available.
// Returns true if allowed, false if rate limited, and any error that occurred.
Allow(ctx context.Context, virtualKeyID string, rateLimit gateway.RateLimit) (allowed bool, err error)
}
RateLimiterStorage is an interface for storing and retrieving token bucket state. This abstraction allows for easy migration from in-memory to Redis-based distributed rate limiting.
type RedisRateLimiterStorage ¶
type RedisRateLimiterStorage struct {
// contains filtered or unexported fields
}
RedisRateLimiterStorage implements RateLimiterStorage using Redis for distributed rate limiting. This allows rate limiting to work across multiple gateway instances.
Usage example:
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0, // use default DB
})
// Verify connection
if err := redisClient.Ping(context.Background()).Err(); err != nil {
log.Fatal("Failed to connect to Redis:", err)
}
// Create Redis rate limiter storage
rateLimiterStorage := NewRedisRateLimiterStorage(redisClient, "rate_limit:")
// Use it with VirtualKeyMiddleware
middleware := NewVirtualKeyMiddleware(configStore, rateLimiterStorage)
The implementation uses Lua scripts for atomic operations, ensuring thread-safety and consistency across distributed systems. Token buckets are stored as Redis hashes with automatic expiration based on the rate limit window duration.
func NewRedisRateLimiterStorage ¶
func NewRedisRateLimiterStorage(client *redis.Client, keyPrefix string) *RedisRateLimiterStorage
NewRedisRateLimiterStorage creates a new Redis-based rate limiter storage. The client should be a configured Redis client that's ready to use. keyPrefix is optional and defaults to "rate_limit:" if empty.
func (*RedisRateLimiterStorage) Allow ¶
func (r *RedisRateLimiterStorage) Allow(ctx context.Context, virtualKeyID string, rateLimit gateway.RateLimit) (bool, error)
Allow checks if a request is allowed and consumes a token if available. Uses a Lua script for atomic token bucket operations in Redis.
func (*RedisRateLimiterStorage) Close ¶
func (r *RedisRateLimiterStorage) Close() error
Close closes the Redis client connection. Call this when shutting down to properly clean up resources.
type VirtualKeyMiddleware ¶
type VirtualKeyMiddleware struct {
// contains filtered or unexported fields
}
func NewVirtualKeyMiddleware ¶
func NewVirtualKeyMiddleware(configStore gateway.ConfigStore, rateLimiterStorage ...RateLimiterStorage) *VirtualKeyMiddleware
NewVirtualKeyMiddleware creates a new VirtualKeyMiddleware with in-memory rate limiting. To use Redis-based distributed rate limiting, pass a RedisRateLimiterStorage implementation as the optional rateLimiterStorage parameter. If no rateLimiterStorage is provided, an in-memory implementation will be used.
func (*VirtualKeyMiddleware) HandleRequest ¶
func (middleware *VirtualKeyMiddleware) HandleRequest(next gateway.RequestHandler) gateway.RequestHandler
func (*VirtualKeyMiddleware) HandleStreamingRequest ¶
func (middleware *VirtualKeyMiddleware) HandleStreamingRequest(next gateway.StreamingRequestHandler) gateway.StreamingRequestHandler