ratelimit

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2017 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var OpenThrottle = &openThrottle{}

OpenThrottle is a singleton open throttle. An open throttle provides no rate limit.

Functions

func WithBurstLimit

func WithBurstLimit(burstLimit int) func(*throttleOptions)

WithBurstLimit returns an option for ratelimit.New that provides an alternate limit for a burst of requests with an idle throttle.

func WithClock

func WithClock(clock Clock) func(*throttleOptions)

WithClock returns an option for ratelimit.New that provides an alternate Clock implementation, typically a mock Clock for testing.

func WithoutSlack

func WithoutSlack(options *throttleOptions)

WithoutSlack is an option for ratelimit.New that initializes the limiter without any initial tolerance for bursts of traffic.

Types

type Clock

type Clock interface {
	Now() time.Time
}

Clock is the minimum necessary interface to instantiate a throttle with a clock or fake clock, compatible with clocks created using github.com/andres-erbsen/clock or go.uber.org/yarpc/internal/clock.

type Option

type Option func(*throttleOptions)

Option is an option for a rate limiter constructor.

type Throttle

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

Throttle is a rate limiter.

func NewThrottle

func NewThrottle(rps int, opts ...Option) (*Throttle, error)

NewThrottle returns a Throttle that will limit to the given RPS.

func (*Throttle) Throttle

func (t *Throttle) Throttle() bool

Throttle returns whether a call should be dropped to ensure that accepted requests remain time.Second/rate on average. All other calls count toward the rate limit.

type UnaryInboundMiddleware

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

UnaryInboundMiddleware is a unary inbound middleware that sheds inbound requests above a rate limit, with some slack for bursts.

func NewUnaryInboundMiddleware

func NewUnaryInboundMiddleware(rps int, opts ...Option) (*UnaryInboundMiddleware, error)

NewUnaryInboundMiddleware creates a unary inbound middleware that introduces a throttle, shedding inbound requests if they arrive more often than the configured rate limit.

func (*UnaryInboundMiddleware) Handle

Handle drops inbound requests with a ResourceExhaustedError if the arrive more frequently than the configured rate limit.

type UnaryInboundMiddlewareConfig

type UnaryInboundMiddlewareConfig struct {
	// RPS is the maximum requests per second, after which the inbound will
	// throttle inbound requests with a ResourceExhaustedError of "rate limit
	// exceeded".
	RPS int `config:"rps"`
	// BurstLimit determines how much slack the rate limiter will tolerate for
	// a burst of requests from an idle state before throttling.
	// The default is 10. A burstLimit of 0 implies the default.
	// Use "noSlack" to configure a rate limiter without slack.
	BurstLimit int `config:"burstLimit"`
	// NoSlack configures the rate limiter without any slack, even after idling
	// indefinitely.
	NoSlack bool `config:"noSlack"`
}

UnaryInboundMiddlewareConfig describes how to configure and construct a unary inbound rate limiter.

func (UnaryInboundMiddlewareConfig) Build

Build creates a unary inbound rate limit middleware, or returns an error if the configuration is invalid.

Jump to

Keyboard shortcuts

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