rate

package
v2.0.49 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2024 License: Apache-2.0 Imports: 3 Imported by: 8

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExampleAdaptiveRateLimitTracker

func ExampleAdaptiveRateLimitTracker()

func ExampleAdaptiveRateLimiter

func ExampleAdaptiveRateLimiter()

func ExampleRateLimiter

func ExampleRateLimiter()

Types

type AdaptiveRateLimitTracker

type AdaptiveRateLimitTracker interface {
	RunRateLimited(label string) (RateLimitControl, error)
	RunRateLimitedF(label string, f func(control RateLimitControl) error) error
	IsRateLimited() bool
}

An AdaptiveRateLimitTracker works similarly to an AdaptiveRateLimiter, except it just manages the rate limiting without actually running the work. Because it doesn't run the work itself, it has to account for the possibility that some work may never report as complete or failed. It thus has a configurable timeout at which point outstanding work will be marked as failed.

type AdaptiveRateLimiter

type AdaptiveRateLimiter interface {
	RunRateLimited(f func() error) (RateLimitControl, error)
}

An AdaptiveRateLimiter allows running arbitrary, sequential operations with a limiter, so that only N operations can be queued to run at any given time. If the system is too busy, the rate limiter will return an ApiError indicating that the server is too busy.

The rate limiter returns a RateLimitControl, allow the calling code to indicate if the operation finished in time. If operations are timing out before the results are available, the rate limiter should allow fewer operations in, as they will likely time out before the results can be used.

The rate limiter doesn't have a set queue size, it has a window which can grow and shrink. When a timeout is signaled, using the RateLimitControl, it shrinks the window based on queue position of the timed out operation. For example, if an operation was queued at position 200, but the times out, we assume that we need to limit the queue size to something less than 200 for now.

The limiter will also reject already queued operations if the window size changes and the operation was queued at a position larger than the current window size.

The window size will slowly grow back towards the max as successes are noted in the RateLimitControl.

type NoOpAdaptiveRateLimitTracker

type NoOpAdaptiveRateLimitTracker struct{}

func (NoOpAdaptiveRateLimitTracker) IsRateLimited

func (n NoOpAdaptiveRateLimitTracker) IsRateLimited() bool

func (NoOpAdaptiveRateLimitTracker) RunRateLimited

func (NoOpAdaptiveRateLimitTracker) RunRateLimitedF

func (n NoOpAdaptiveRateLimitTracker) RunRateLimitedF(_ string, f func(control RateLimitControl) error) error

type NoOpAdaptiveRateLimiter

type NoOpAdaptiveRateLimiter struct{}

func (NoOpAdaptiveRateLimiter) RunRateLimited

func (self NoOpAdaptiveRateLimiter) RunRateLimited(f func() error) (RateLimitControl, error)

type NoOpRateLimiter

type NoOpRateLimiter struct{}

func (NoOpRateLimiter) GetQueueFillPct

func (self NoOpRateLimiter) GetQueueFillPct() float64

func (NoOpRateLimiter) RunRateLimited

func (self NoOpRateLimiter) RunRateLimited(f func() error) error

type RateLimitControl

type RateLimitControl interface {
	// Success indicates the operation was a success
	Success()

	// Backoff indicates that we need to backoff
	Backoff()

	// Failed indicates the operation was not a success, but a backoff isn't required
	Failed()
}

func NoOpRateLimitControl

func NoOpRateLimitControl() RateLimitControl

type RateLimiter

type RateLimiter interface {
	RunRateLimited(func() error) error
	GetQueueFillPct() float64
}

A RateLimiter allows running arbitrary, sequential operations with a limiter, so that only N operations can be queued to run at any given time. If the system is too busy, the rate limiter will return an ApiError indicating that the server is too busy

Jump to

Keyboard shortcuts

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