ratelimit

package
v0.1.44 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: AGPL-3.0 Imports: 5 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 provides the byte token buckets shared by node engines: one per credential (the device limit) and one per user on the node (the person limit, see Registry).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WaitDown added in v0.1.39

func WaitDown(ctx context.Context, device, person *Limiter, n uint64) bool

WaitDown is WaitUp for the download direction.

func WaitUp added in v0.1.39

func WaitUp(ctx context.Context, device, person *Limiter, n uint64) bool

WaitUp waits until n upload bytes may pass both the device limiter and the person limiter; either may be nil (no such limit). The device bucket goes first: a device held back by its own limit must not sit on tokens of the person bucket that the person's other devices could use. No lock is held while waiting. False: ctx ended.

Types

type Holder added in v0.1.39

type Holder struct {
	CredID, UserID string
	BPS            uint64
}

Holder is one credential asking for its user's bucket. BPS 0 (or no user) means the credential has no person limit.

type Limiter

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

Limiter keeps independent upload and download buckets. BPS is the configured limit in bits per second; SetBPS changes it in place, so everything holding the limiter follows.

func New

func New(bps uint64) *Limiter

func (*Limiter) BPS

func (l *Limiter) BPS() uint64

func (*Limiter) SetBPS added in v0.1.39

func (l *Limiter) SetBPS(bps uint64)

SetBPS changes the limit of both buckets in place. Tokens already in a bucket stay (capped by the new burst).

func (*Limiter) WaitDown

func (l *Limiter) WaitDown(ctx context.Context, n uint64) bool

func (*Limiter) WaitUp

func (l *Limiter) WaitUp(ctx context.Context, n uint64) bool

type Registry added in v0.1.39

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

Registry holds the person limit: one Limiter per user id, shared by every credential of that user on this node, in every inbound and every engine. The buckets are per node on purpose: a person on two nodes at once can reach twice the limit, there is no coordination between nodes.

A credential is a holder of its user's bucket. Holders come and go inbound by inbound (Sync); the bucket lives while it has at least one holder, changes its rate in place when the value changes (everything that got it from Sync follows), and is dropped with its last holder. The credentials of one user carry the same value; if they disagree (an inbound applied before its neighbour) the bucket takes the smallest, so the outcome does not depend on the order of arrival.

func NewRegistry added in v0.1.39

func NewRegistry() *Registry

func (*Registry) Limiter added in v0.1.39

func (r *Registry) Limiter(userID string) *Limiter

Limiter returns the bucket of a user, nil when no credential of theirs is limited on this node.

func (*Registry) Sync added in v0.1.39

func (r *Registry) Sync(scope string, hs []Holder) map[string]*Limiter

Sync makes hs the complete set of holders of scope (an inbound id; nil/empty releases the scope) and returns the bucket of each limited credential, keyed by credential id. The credentials the scope no longer lists let go of their buckets; a bucket nobody holds any more is dropped. Safe for concurrent use; the returned limiters are used without any registry lock.

Jump to

Keyboard shortcuts

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