ratelimit

package
v0.51.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicManager

type AtomicManager struct {
	// contains filtered or unexported fields
}

AtomicManager wraps a Manager behind an atomic pointer, allowing the underlying rate limiter to be swapped at runtime without data races. It implements the same interfaces as Manager (IPRateLimiter, RateLimiter, ClientRateLimiter) so it can be used as a drop-in replacement.

func NewAtomicManager

func NewAtomicManager(m *Manager) *AtomicManager

NewAtomicManager creates an AtomicManager with the given initial Manager.

func (*AtomicManager) Allow

func (am *AtomicManager) Allow(addr net.Addr) bool

func (*AtomicManager) AllowConnection

func (am *AtomicManager) AllowConnection(addr net.Addr) bool

func (*AtomicManager) AllowPublish

func (am *AtomicManager) AllowPublish(clientID string) bool

func (*AtomicManager) AllowSubscribe

func (am *AtomicManager) AllowSubscribe(clientID string) bool

func (*AtomicManager) Load

func (am *AtomicManager) Load() *Manager

Load returns the current Manager.

func (*AtomicManager) OnClientDisconnect

func (am *AtomicManager) OnClientDisconnect(clientID string)

func (*AtomicManager) Stop

func (am *AtomicManager) Stop()

func (*AtomicManager) Swap

func (am *AtomicManager) Swap(m *Manager) *Manager

Swap replaces the underlying Manager with a new one and stops the old one. Returns the old Manager.

type ClientRateLimiter

type ClientRateLimiter struct {
	// contains filtered or unexported fields
}

ClientRateLimiter manages rate limiting for individual MQTT clients. Used to limit message publishing and subscription rates per client.

func NewClientRateLimiter

func NewClientRateLimiter(messageRate float64, messageBurst int, subRate float64, subBurst int) *ClientRateLimiter

NewClientRateLimiter creates a new client-based rate limiter.

func (*ClientRateLimiter) AllowPublish

func (l *ClientRateLimiter) AllowPublish(clientID string) bool

AllowPublish checks if a publish from the given client is allowed. Returns true if allowed, false if rate limited.

func (*ClientRateLimiter) AllowSubscribe

func (l *ClientRateLimiter) AllowSubscribe(clientID string) bool

AllowSubscribe checks if a subscription from the given client is allowed. Returns true if allowed, false if rate limited.

func (*ClientRateLimiter) RemoveClient

func (l *ClientRateLimiter) RemoveClient(clientID string)

RemoveClient removes rate limiters for a disconnected client.

type Config

type Config struct {
	Enabled bool `yaml:"enabled"`

	Connection ConnectionConfig `yaml:"connection"`
	Message    MessageConfig    `yaml:"message"`
	Subscribe  SubscribeConfig  `yaml:"subscribe"`
}

Config holds rate limiting configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a sensible default configuration.

type ConnectionConfig

type ConnectionConfig struct {
	Enabled         bool          `yaml:"enabled"`
	Rate            float64       `yaml:"rate"`             // connections per second per IP
	Burst           int           `yaml:"burst"`            // burst allowance
	CleanupInterval time.Duration `yaml:"cleanup_interval"` // cleanup interval for stale entries
}

ConnectionConfig holds per-IP connection rate limiting settings.

type IPRateLimiter

type IPRateLimiter struct {
	// contains filtered or unexported fields
}

IPRateLimiter manages rate limiting for IP addresses (connection layer). Used to limit connection attempts per IP to prevent DoS attacks.

func NewIPRateLimiter

func NewIPRateLimiter(r float64, burst int, cleanupInterval time.Duration) *IPRateLimiter

NewIPRateLimiter creates a new IP-based rate limiter. rate is connections per second, burst is the burst allowance.

func (*IPRateLimiter) Allow

func (l *IPRateLimiter) Allow(addr net.Addr) bool

Allow checks if a connection from the given IP address is allowed. Returns true if the connection is allowed, false if rate limited.

func (*IPRateLimiter) Stop

func (l *IPRateLimiter) Stop()

Stop stops the cleanup goroutine.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager coordinates all rate limiters.

func NewManager

func NewManager(cfg Config) *Manager

NewManager creates a new rate limit manager.

func (*Manager) Allow

func (m *Manager) Allow(addr net.Addr) bool

Allow implements the IPRateLimiter interface used by TCP and WebSocket servers.

func (*Manager) AllowConnection

func (m *Manager) AllowConnection(addr net.Addr) bool

AllowConnection checks if a new connection from the given address is allowed.

func (*Manager) AllowPublish

func (m *Manager) AllowPublish(clientID string) bool

AllowPublish checks if a publish from the given client is allowed.

func (*Manager) AllowSubscribe

func (m *Manager) AllowSubscribe(clientID string) bool

AllowSubscribe checks if a subscription from the given client is allowed.

func (*Manager) OnClientDisconnect

func (m *Manager) OnClientDisconnect(clientID string)

OnClientDisconnect cleans up rate limiters for a disconnected client.

func (*Manager) Stop

func (m *Manager) Stop()

Stop stops the rate limiter manager and cleans up resources.

type MessageConfig

type MessageConfig struct {
	Enabled bool    `yaml:"enabled"`
	Rate    float64 `yaml:"rate"`  // messages per second per client
	Burst   int     `yaml:"burst"` // burst allowance
}

MessageConfig holds per-client message rate limiting settings.

type RateLimitResult

type RateLimitResult struct {
	Allowed bool
	Reason  string
}

RateLimitResult represents the result of a rate limit check.

type SubscribeConfig

type SubscribeConfig struct {
	Enabled bool    `yaml:"enabled"`
	Rate    float64 `yaml:"rate"`  // subscriptions per second per client
	Burst   int     `yaml:"burst"` // burst allowance
}

SubscribeConfig holds per-client subscription rate limiting settings.

Jump to

Keyboard shortcuts

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