traffic

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 19 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package traffic provides a traffic/bandwidth limiter implementation that supports per-service, per-connection, per-IP, and per-CIDR rate limiting for both ingress (In) and egress (Out) directions.

Index

Constants

View Source
const (
	// ServiceLimitKey is the special key used for service-level rate limits.
	ServiceLimitKey = "$"
	// ConnLimitKey is the special key used for connection-level rate limits.
	ConnLimitKey = "$$"
)

Variables

This section is empty.

Functions

func NewLimiter

func NewLimiter(r int) limiter.Limiter

NewLimiter creates a rate-based traffic Limiter with the given rate (bytes per second). The burst size is set equal to the rate.

func NewLimiterWithBurst added in v0.28.1

func NewLimiterWithBurst(r, burst int) limiter.Limiter

NewLimiterWithBurst creates a rate-based traffic Limiter with the given rate (bytes per second) and burst size. If burst <= 0, it defaults to r.

func NewTrafficLimiter

func NewTrafficLimiter(opts ...Option) traffic.TrafficLimiter

NewTrafficLimiter creates a new TrafficLimiter with the given options. It starts a background goroutine for periodic reload of limit rules from external loaders.

Types

type Option

type Option func(opts *options)

Option configures a TrafficLimiter.

func FileLoaderOption

func FileLoaderOption(fileLoader loader.Loader) Option

FileLoaderOption sets a file-based loader for limit rules.

func HTTPLoaderOption

func HTTPLoaderOption(httpLoader loader.Loader) Option

HTTPLoaderOption sets an HTTP-based loader for limit rules.

func LimitsOption

func LimitsOption(limits ...string) Option

LimitsOption sets the static limit rules. Each string is a limit definition in the format "<key> <in> [out]" where in/out are byte quantities with optional SI/IEC suffixes (e.g., "100B", "1MB", "512KB").

func LoggerOption

func LoggerOption(logger logger.Logger) Option

LoggerOption sets the logger for the traffic limiter.

func RedisLoaderOption

func RedisLoaderOption(redisLoader loader.Loader) Option

RedisLoaderOption sets a Redis-based loader for limit rules.

func ReloadPeriodOption

func ReloadPeriodOption(period time.Duration) Option

ReloadPeriodOption sets the period for reloading limit rules from external loaders. A period of zero disables periodic reloading.

Directories

Path Synopsis
Package limiter provides a cached traffic limiter that wraps a TrafficLimiter with in-memory caching to reduce calls to the underlying limiter (e.g., plugin-based limiters).
Package limiter provides a cached traffic limiter that wraps a TrafficLimiter with in-memory caching to reduce calls to the underlying limiter (e.g., plugin-based limiters).
Package wrapper provides net.Conn, net.PacketConn, and io.ReadWriter wrappers that apply traffic rate limiting to reads and writes.
Package wrapper provides net.Conn, net.PacketConn, and io.ReadWriter wrappers that apply traffic rate limiting to reads and writes.

Jump to

Keyboard shortcuts

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