limiter

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2024 License: GPL-3.0 Imports: 7 Imported by: 0

README

Rate Limiter GO

A basic rate limiter module for HTTP servers using the limiter package (based off the token bucket algorithm)

Used in my personal VAS project

Documentation

Index

Constants

View Source
const CLEANUP_FREQUENCY = 120
View Source
const VISITOR_AGE_THRESHOLD = 180

Variables

This section is empty.

Functions

This section is empty.

Types

type CooldownConfig added in v1.1.0

type CooldownConfig struct {
	Requests uint
	Cooldown uint
}

type CooldownLimiter added in v1.1.0

type CooldownLimiter struct {
	Name     string
	Mutex    sync.Mutex
	Limiters map[string]Visitor
	// Threshold at which old visitors are deleted from the map (in seconds since last visit)
	VisitorAgeThreshold int
	// Frequency of cleanup (in seconds)
	CleanupFrequency int
	Configuration    CooldownConfig
}

func NewCooldownLimiter added in v1.1.0

func NewCooldownLimiter(name string, config CooldownConfig) *CooldownLimiter

func (*CooldownLimiter) CleanupLimiters added in v1.1.0

func (cl *CooldownLimiter) CleanupLimiters()

Function that runs periodically in a goroutine to cleanup old visitors (threshold set with const)

func (*CooldownLimiter) GetLimiter added in v1.1.0

func (cl *CooldownLimiter) GetLimiter(ip string) *rate.Limiter

func (*CooldownLimiter) LimiterHandler added in v1.1.0

func (cl *CooldownLimiter) LimiterHandler(next http.HandlerFunc) http.HandlerFunc

type LimiterConfig added in v1.1.0

type LimiterConfig struct {
	MaxRequests uint
	BurstLimit  uint
}

type LoginIdentifier added in v1.1.0

type LoginIdentifier struct {
	UserId    uint
	IpAddress string
}

type LoginLimiter added in v1.1.0

type LoginLimiter struct {
	Mutex    sync.Mutex
	Limiters map[LoginIdentifier]Visitor
	// Threshold at which old visitors are deleted from the map (in seconds since last visit)
	VisitorAgeThreshold int
	// Frequency of cleanup (in seconds)
	CleanupFrequency int
	Configuration    CooldownConfig
}

func NewLoginLimiter added in v1.1.0

func NewLoginLimiter(config CooldownConfig) *LoginLimiter

func (*LoginLimiter) CleanupLimiters added in v1.1.0

func (ll *LoginLimiter) CleanupLimiters()

Function that runs periodically in a goroutine to cleanup old visitors (threshold set with const)

func (*LoginLimiter) GetLimiter added in v1.1.0

func (ll *LoginLimiter) GetLimiter(userId uint, ip string) *rate.Limiter

type RateLimiter

type RateLimiter struct {
	Mutex    sync.Mutex
	Limiters map[string]Visitor
	// Threshold at which old visitors are deleted from the map (in seconds since last visit)
	VisitorAgeThreshold int
	// Frequency of cleanup (in seconds)
	CleanupFrequency int
	Configuration    LimiterConfig
}

func NewLimiter

func NewLimiter(config LimiterConfig) *RateLimiter

func (*RateLimiter) CleanupLimiters

func (rl *RateLimiter) CleanupLimiters()

Function that runs periodically in a goroutine to cleanup old visitors (threshold set with const)

func (*RateLimiter) GetLimiter

func (rl *RateLimiter) GetLimiter(ip string) *rate.Limiter

func (*RateLimiter) LimiterHandler

func (rl *RateLimiter) LimiterHandler(next http.HandlerFunc) http.HandlerFunc

type Visitor

type Visitor struct {
	Limiter  *rate.Limiter
	LastSeen time.Time
}

Jump to

Keyboard shortcuts

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