ratelimit

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2026 License: Apache-2.0 Imports: 10 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 ratelimit paces data transfer streams: the byte rate a process relays and the byte and row rates a data node admits imported rows at.

Index

Constants

View Source
const MaxBurst = 1 << 20

MaxBurst is the largest byte burst of a Relay and of a Node: one full chunk.

View Source
const NodeSlack = 10 * time.Second

NodeSlack is how much longer than its own share a payment to a Node may wait for the payments admitted before it. A payment that would wait longer is refused with ErrBusy, so a writer that waits for the node's answer bounds the wait by its own share plus NodeSlack.

Variables

View Source
var ErrBusy = errors.New("the node is admitting other imports")

ErrBusy refuses a payment the node cannot admit in time; nothing was charged for it.

Functions

func New

func New(perSecond uint64, maxBurst int) *rate.Limiter

New returns a bucket of perSecond tokens a second whose burst is one second's worth, at most maxBurst; nil, unlimited, when perSecond is 0.

func NewClient

func NewClient(perSecond float64, unit int) *rate.Limiter

NewClient returns an import client's bucket of perSecond tokens a second whose burst is one second's worth or one unit, whichever is larger, so one batch or chunk is always taken in a single wait; nil, unlimited, when perSecond is 0.

func PerSecond

func PerSecond(rate float64) uint64

PerSecond rounds a normalized per-second rate down to the wire value, so the limit sent never exceeds the one configured; 0 is no limit, and the configuration rejects a non-zero rate below 1 per second.

func Proto

func Proto(bytesPerSecond, rowsPerSecond float64) *transferv1.RateLimit

Proto converts normalized per-second rates to the Init rate limit; 0 is no limit.

func Wait

func Wait(ctx context.Context, l *rate.Limiter, n int) error

Wait takes n tokens from l in steps of its burst; a nil bucket never waits. A wait that ends with ctx returns the gRPC status of ctx, and one that would outlast its deadline DeadlineExceeded.

Types

type Node

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

Node is the rate a data node admits import rows at: one byte pace and one row pace that every import writing to the node shares, whichever liaison it comes through. A pace is the time by which the node has paid out everything it admitted; each payment moves it on by its cost at the rate the payment names, so the last rate applies and a change carries no debt or burst over. A payment that names no rate for a pace is not charged to it. Up to one second's worth of a rate, at most MaxBurst bytes, passes without waiting on an idle node.

func NewNode

func NewNode() *Node

NewNode returns a Node that has admitted nothing yet.

func (*Node) Pay

func (l *Node) Pay(ctx context.Context, bytesPerSecond, rowsPerSecond uint64, n, rows int) error

Pay blocks until the node may write rows rows of n bytes at bytesPerSecond and rowsPerSecond, 0 meaning unlimited. It returns ErrBusy at once, charging nothing, when the payments admitted before it would make it wait more than NodeSlack beyond its own share, or past the deadline of ctx. A wait that ends with ctx gives the payment back unless a later one was admitted.

type Relay

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

Relay is a process-level byte rate shared by every stream that relays through it. The stream opened last sets the rate, 0 meaning unlimited, and the rate returns to unlimited once no relay is active. One bucket serves every rate, so opening a stream adds no burst. The burst is one second's worth of the rate, at most MaxBurst, and a chunk larger than the burst's share of one relay is relayed in pieces of that share, so a low rate still delivers a frame to every relay about every second instead of one whole chunk after a long wait.

func NewRelay

func NewRelay() *Relay

NewRelay returns an unlimited Relay.

func (*Relay) Burst

func (r *Relay) Burst() int

Burst is the current burst.

func (*Relay) Limit

func (r *Relay) Limit() rate.Limit

Limit is the current rate.

func (*Relay) Open

func (r *Relay) Open(bytesPerSecond uint64) (release func())

Open applies bytesPerSecond for a relay that starts now and returns the function, called once, that ends the relay.

func (*Relay) Pay

func (r *Relay) Pay(ctx context.Context, n int) error

Pay blocks until the limiter lets n bytes pass.

func (*Relay) PieceSize

func (r *Relay) PieceSize() int

PieceSize is the size of the next piece a relay would take.

func (*Relay) Take

func (r *Relay) Take(ctx context.Context, n int) (int, error)

Take blocks until the next step of n bytes may pass and returns its size: at most one relay's share of the burst, sized and reserved under the lock open takes to change the rate, so a relay opening with a lower rate never leaves a step larger than the burst and the caller sends every step as soon as it passes. It returns the gRPC status of ctx once ctx ends, giving the reserved bytes back.

func (*Relay) Unlimited

func (r *Relay) Unlimited() bool

Unlimited tells whether no stream currently limits the rate, so a batch need not be sized nor paid.

Jump to

Keyboard shortcuts

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