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
- Variables
- func New(perSecond uint64, maxBurst int) *rate.Limiter
- func NewClient(perSecond float64, unit int) *rate.Limiter
- func PerSecond(rate float64) uint64
- func Proto(bytesPerSecond, rowsPerSecond float64) *transferv1.RateLimit
- func Wait(ctx context.Context, l *rate.Limiter, n int) error
- type Node
- type Relay
- func (r *Relay) Burst() int
- func (r *Relay) Limit() rate.Limit
- func (r *Relay) Open(bytesPerSecond uint64) (release func())
- func (r *Relay) Pay(ctx context.Context, n int) error
- func (r *Relay) PieceSize() int
- func (r *Relay) Take(ctx context.Context, n int) (int, error)
- func (r *Relay) Unlimited() bool
Constants ¶
const MaxBurst = 1 << 20
MaxBurst is the largest byte burst of a Relay and of a Node: one full chunk.
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 ¶
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 ¶
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 ¶
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 ¶
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.
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 (*Node) Pay ¶
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 (*Relay) Open ¶
Open applies bytesPerSecond for a relay that starts now and returns the function, called once, that ends the relay.
func (*Relay) Take ¶
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.