Documentation
¶
Overview ¶
Package glimiter provides rate limiter implementations for GoFrame. It supports both in-memory and Redis-based rate limiting with sliding window algorithm and concurrent-safe operations.
Package glimiter provides rate limiter implementations for GoFrame.
Package glimiter provides rate limiter implementations for GoFrame.
Package glimiter provides rate limiter implementations for GoFrame.
Index ¶
- func Middleware(config MiddlewareConfig) ghttp.HandlerFunc
- func MiddlewareByAPIKey(limiter Limiter, keyHeader string) ghttp.HandlerFunc
- func MiddlewareByIP(limiter Limiter) ghttp.HandlerFunc
- func MiddlewareByUser(limiter Limiter, userFunc func(r *ghttp.Request) string) ghttp.HandlerFunc
- type Limiter
- type MemoryLimiter
- func (l *MemoryLimiter) Allow(ctx context.Context, key string) (bool, error)
- func (l *MemoryLimiter) AllowN(ctx context.Context, key string, n int) (bool, error)
- func (l *MemoryLimiter) GetLimit() int
- func (l *MemoryLimiter) GetRemaining(ctx context.Context, key string) (int, error)
- func (l *MemoryLimiter) GetResetTime(ctx context.Context, key string) (time.Time, error)
- func (l *MemoryLimiter) GetWindow() time.Duration
- func (l *MemoryLimiter) Reset(ctx context.Context, key string) error
- func (l *MemoryLimiter) Wait(ctx context.Context, key string) error
- type MiddlewareConfig
- type RedisLimiter
- func (l *RedisLimiter) Allow(ctx context.Context, key string) (bool, error)
- func (l *RedisLimiter) AllowN(ctx context.Context, key string, n int) (bool, error)
- func (l *RedisLimiter) GetLimit() int
- func (l *RedisLimiter) GetRemaining(ctx context.Context, key string) (int, error)
- func (l *RedisLimiter) GetResetTime(ctx context.Context, key string) (time.Time, error)
- func (l *RedisLimiter) GetWindow() time.Duration
- func (l *RedisLimiter) Reset(ctx context.Context, key string) error
- func (l *RedisLimiter) Wait(ctx context.Context, key string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(config MiddlewareConfig) ghttp.HandlerFunc
Middleware creates and returns a rate limit middleware with the given config. It automatically sets standard rate limit headers (X-RateLimit-*) in responses.
Example:
limiter := glimiter.NewMemoryLimiter(100, time.Minute)
s.Group("/api", func(group *ghttp.RouterGroup) {
group.Middleware(glimiter.Middleware(glimiter.MiddlewareConfig{
Limiter: limiter,
}))
group.ALL("/user", handler)
})
func MiddlewareByAPIKey ¶
func MiddlewareByAPIKey(limiter Limiter, keyHeader string) ghttp.HandlerFunc
MiddlewareByAPIKey creates a middleware that limits by API key. The keyHeader parameter specifies which header contains the API key.
func MiddlewareByIP ¶
func MiddlewareByIP(limiter Limiter) ghttp.HandlerFunc
MiddlewareByIP creates a middleware that limits by IP address. This is a convenience function for the most common use case.
func MiddlewareByUser ¶
MiddlewareByUser creates a middleware that limits by user ID. The userFunc parameter extracts the user ID from the request context.
Types ¶
type Limiter ¶
type Limiter interface {
// Allow checks if a single request is allowed under the rate limit.
// It returns true if allowed, false otherwise.
Allow(ctx context.Context, key string) (bool, error)
// AllowN checks if N requests are allowed under the rate limit.
// It returns true if allowed, false otherwise.
AllowN(ctx context.Context, key string, n int) (bool, error)
// Wait blocks until a single request is allowed.
// It returns error if context is cancelled.
Wait(ctx context.Context, key string) error
// GetLimit returns the maximum number of requests allowed in the time window.
GetLimit() int
// GetWindow returns the time window duration for rate limiting.
GetWindow() time.Duration
// GetRemaining returns the remaining quota for the given key.
// It returns -1 if the key doesn't exist.
GetRemaining(ctx context.Context, key string) (int, error)
// GetResetTime returns the time when the rate limit will reset for the given key.
// It returns the exact timestamp when the oldest request in the window will expire.
// For keys that don't exist, it returns the current time.
GetResetTime(ctx context.Context, key string) (time.Time, error)
// Reset resets the rate limit for the given key.
Reset(ctx context.Context, key string) error
}
Limiter defines the interface for rate limiting operations.
type MemoryLimiter ¶
type MemoryLimiter struct {
// contains filtered or unexported fields
}
MemoryLimiter implements Limiter using in-memory cache. It uses gcache for storage with automatic expiration.
func NewMemoryLimiter ¶
func NewMemoryLimiter(limit int, window time.Duration) *MemoryLimiter
NewMemoryLimiter creates and returns a new memory-based rate limiter. The limit parameter specifies the maximum number of requests allowed, and window specifies the time window duration.
func (*MemoryLimiter) AllowN ¶
AllowN implements Limiter.AllowN. It uses CAS (Compare-And-Swap) to ensure atomic check-and-increment operations.
func (*MemoryLimiter) GetLimit ¶
func (l *MemoryLimiter) GetLimit() int
GetLimit implements Limiter.GetLimit.
func (*MemoryLimiter) GetRemaining ¶
GetRemaining implements Limiter.GetRemaining.
func (*MemoryLimiter) GetResetTime ¶
GetResetTime implements Limiter.GetResetTime. Returns the expiration time of the cache entry, which is when the rate limit will reset.
func (*MemoryLimiter) GetWindow ¶
func (l *MemoryLimiter) GetWindow() time.Duration
GetWindow implements Limiter.GetWindow.
type MiddlewareConfig ¶
type MiddlewareConfig struct {
// Limiter is the rate limiter instance to use.
Limiter Limiter
// KeyFunc generates the rate limit key from request.
// Default: use client IP address.
KeyFunc func(r *ghttp.Request) string
// ErrorHandler handles rate limit exceeded errors.
// Default: returns 429 Too Many Requests with JSON response.
ErrorHandler func(r *ghttp.Request)
}
MiddlewareConfig defines the configuration for rate limit middleware.
type RedisLimiter ¶
type RedisLimiter struct {
// contains filtered or unexported fields
}
RedisLimiter implements Limiter using Redis. It uses sliding window algorithm with Lua script for atomic operations.
func NewRedisLimiter ¶
NewRedisLimiter creates and returns a new Redis-based rate limiter. The limit parameter specifies the maximum number of requests allowed, and window specifies the time window duration.
func (*RedisLimiter) AllowN ¶
AllowN implements Limiter.AllowN. It uses Lua script to ensure atomic operations in Redis. The Lua script guarantees that check-and-add is atomic, preventing race conditions.
func (*RedisLimiter) GetLimit ¶
func (l *RedisLimiter) GetLimit() int
GetLimit implements Limiter.GetLimit.
func (*RedisLimiter) GetRemaining ¶
GetRemaining implements Limiter.GetRemaining.
func (*RedisLimiter) GetResetTime ¶
GetResetTime implements Limiter.GetResetTime. Returns the time when the oldest request in the sliding window will expire.
func (*RedisLimiter) GetWindow ¶
func (l *RedisLimiter) GetWindow() time.Duration
GetWindow implements Limiter.GetWindow.