limit

package
v3.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: GPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package limit provides limit functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Rate

func Rate(r float64) rate.Limit

Rate is a helper to convert float64 to rate.Limit.

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

func NewLimitCounter(maxConnections int32) *Counter

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

func NewIPRateLimiterWithConfig(r rate.Limit, b int, cfg config.File) *IPRateLimiter

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.

Jump to

Keyboard shortcuts

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