ratelimit

package
v2.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChooseLimiterCapacity

func ChooseLimiterCapacity(
	rateHz float64,
	window time.Duration) (capacity uint64, err error)

Choose a limiter capacity that ensures that the action gated by the limiter will be limited to within a few percent of `rateHz * window` for any window of the given size.

This is not be possible for all rates and windows. In that case, an error will be returned.

func NewThrottledBucket

func NewThrottledBucket(
	opThrottle Throttle,
	egressThrottle Throttle,
	wrapped gcs.Bucket) (b gcs.Bucket)

Create a bucket that limits the rate at which it calls the wrapped bucket using opThrottle, and limits the bandwidth with which it reads from the wrapped bucket using egressThrottle.

func ThrottledReader

func ThrottledReader(
	ctx context.Context,
	r io.Reader,
	throttle Throttle) io.Reader

Create a reader that limits the bandwidth of reads made from r according to the supplied throttler. Reads are assumed to be made under the supplied context.

Types

type Throttle

type Throttle interface {
	// Return the maximum number of tokens that can be requested in a call to
	// Wait.
	Capacity() (c uint64)

	// Acquire the given number of tokens from the underlying token bucket, then
	// sleep until when it says to wake. If the context is cancelled before then,
	// return early with an error.
	//
	// REQUIRES: tokens <= capacity
	Wait(ctx context.Context, tokens uint64) (err error)
}

A simple interface for limiting the rate of some event. Unlike TokenBucket, does not allow the user control over what time means.

Safe for concurrent access.

func NewThrottle

func NewThrottle(
	rateHz float64,
	capacity uint64) (t Throttle)

Jump to

Keyboard shortcuts

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