Documentation
¶
Overview ¶
Package limit provides limit functionality.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Counter ¶
type Counter struct {
// MaxConnections defines the maximum number of concurrent connections allowed.
MaxConnections int32
// CurrentConnections tracks the current number of active connections in the Counter middleware.
CurrentConnections int32
}
Counter tracks the current number of active connections and limits them based on a specified maximum.
func NewLimitCounter ¶
NewLimitCounter creates a new Counter instance with the specified maximum number of concurrent connections.
func (*Counter) Middleware ¶
func (lc *Counter) Middleware() gin.HandlerFunc
Middleware limits the number of concurrent connections handled by the server based on MaxConnections. It is context-aware and prioritizes certain types of requests.
func (*Counter) MiddlewareWithLogger ¶
func (lc *Counter) MiddlewareWithLogger(logger *slog.Logger) gin.HandlerFunc
MiddlewareWithLogger is the logger-injected variant of Middleware.
type IPRateLimiter ¶
type IPRateLimiter struct {
// contains filtered or unexported fields
}
IPRateLimiter manages rate limiters for individual IP addresses.
func NewIPRateLimiter ¶
func NewIPRateLimiter(r rate.Limit, b int) *IPRateLimiter
NewIPRateLimiter creates a new IPRateLimiter with the specified rate and burst. r: Number of tokens per second. b: Maximum burst size.
func NewIPRateLimiterWithConfig ¶
NewIPRateLimiterWithConfig creates an IP rate limiter using the shared trusted proxy configuration for client IP resolution.
func (*IPRateLimiter) AddIP ¶
func (i *IPRateLimiter) AddIP(ip string) *rate.Limiter
AddIP adds a new limiter for the given IP address.
func (*IPRateLimiter) GetLimiter ¶
func (i *IPRateLimiter) GetLimiter(ip string) *rate.Limiter
GetLimiter returns the rate limiter for the given IP address. If no limiter exists for the IP, it creates a new one.
func (*IPRateLimiter) Middleware ¶
func (i *IPRateLimiter) Middleware() gin.HandlerFunc
Middleware returns a gin middleware that performs rate limiting based on the client's IP address.