ratelimit

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 6 Imported by: 0 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 ratelimit keeps token buckets per class (API, sign-in, sign-up, CLI login, webhooks) and key (a token or a client address), in memory. Each server process counts on its own: behind a load balancer with N replicas, a client gets up to N times the configured rate.

Index

Constants

This section is empty.

Variables

Classes lists every class, in the order the docs and config show them.

Functions

func EnvName

func EnvName(c Class) string

EnvName is the server environment variable that sets class's rate.

func RetrySeconds

func RetrySeconds(d time.Duration) int

RetrySeconds is d in whole seconds, rounded up, at least 1: the value of a Retry-After header.

func RetryText

func RetryText(d time.Duration) string

RetryText is the "retry in N s" part of a refusal: whole seconds, rounded up, at least 1.

Types

type Class

type Class string

Class is what a bucket limits.

const (
	APIToken Class = "api_token" // API and MCP calls with one token
	APIIP    Class = "api_ip"    // API and MCP calls from one address, with or without a token
	SignIn   Class = "signin"    // sign-in pages and callbacks, per address
	SignUp   Class = "signup"    // new accounts, per address
	CLILogin Class = "cli_login" // vx login started or approved, per address
	Webhook  Class = "webhook"   // webhook deliveries, per address
)

type Config

type Config map[Class]Rate

Config is the rate per class. Missing classes are not limited.

func Defaults

func Defaults() Config

Defaults are generous for a team, and stop one client from exhausting a shared server. An agent landing or syncing makes a few dozen API calls.

func FromEnv

func FromEnv(getenv func(string) string) (Config, error)

FromEnv is Defaults with each VX_RATE_<CLASS> that getenv returns applied, and VX_RATE_LIMITS=off turning every limit off.

type Limiter

type Limiter struct {
	Now func() time.Time
	// contains filtered or unexported fields
}

Limiter is a set of token buckets. The zero value and nil limit nothing.

func New

func New(cfg Config) *Limiter

New returns a limiter for cfg.

func (*Limiter) Allow

func (l *Limiter) Allow(c Class, key string) (bool, time.Duration)

Allow takes one request from key's bucket of class. When it is empty, Allow returns false and how long until one request is allowed again.

func (*Limiter) Config

func (l *Limiter) Config() Config

Config returns the limiter's rates.

type Rate

type Rate struct {
	PerSecond float64
	Burst     int
}

Rate is a token bucket: Burst requests at once, refilled at PerSecond. The zero Rate means no limit.

func ParseRate

func ParseRate(s string) (Rate, error)

ParseRate reads "N/s", "N/m" or "N/h", optionally followed by ",burst=B", or "off". Examples: "20/s,burst=200", "30/m", "off". Without burst=, the burst is N: a client may use a whole period's requests at once, then waits for the refill.

func (Rate) Off

func (r Rate) Off() bool

Off reports whether the rate limits nothing.

func (Rate) String

func (r Rate) String() string

String is the rate in ParseRate's format.

Jump to

Keyboard shortcuts

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