limits

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package limits caps work per key (a source, later a host): how many tasks run at once, and how fast requests go. The caps hold across all users' work. They live in memory, which fits a single server instance.

Index

Constants

This section is empty.

Variables

View Source
var Default = Limit{Concurrency: 2, Every: 500 * time.Millisecond, Burst: 4}

Default is the cap of keys nobody set: polite to sites that said nothing about their limits.

Functions

This section is empty.

Types

type Key

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

Key is the live cap of one key.

func (*Key) Acquire

func (k *Key) Acquire(ctx context.Context) error

Acquire waits for a concurrency slot; Release gives it back.

func (*Key) Release

func (k *Key) Release()

Release frees a slot taken with TryAcquire.

func (*Key) TryAcquire

func (k *Key) TryAcquire() bool

TryAcquire takes a concurrency slot if one is free.

func (*Key) Wait

func (k *Key) Wait(ctx context.Context) error

Wait blocks until the next request may go.

type Limit

type Limit struct {
	// Concurrency is how many tasks may hold the key at once (at least 1).
	Concurrency int
	// Every is the time between two requests; 0 means no rate limit.
	Every time.Duration
	// Burst is how many requests may go at once after a quiet spell.
	Burst int
}

Limit is the cap of one key.

type Registry

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

Registry holds the caps of every key.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns an empty registry.

func (*Registry) For

func (r *Registry) For(key string) *Key

For returns the live cap of key.

func (*Registry) Limit

func (r *Registry) Limit(key string) (Limit, bool)

Limit returns the cap set for key, if any.

func (*Registry) Set

func (r *Registry) Set(key string, l Limit)

Set changes a key's cap. Work holding the old cap finishes under it.

Jump to

Keyboard shortcuts

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