limit

package
v0.18.1 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package limit implements various rate limiters. In the current stage (2023-03-13) limiters can only be used when for waiting until request limits permit operations. In the future, we might add support for limiters that can be used to limit incoming traffic for services.

Because of limitations in the testing process of the rate limiters be aware that the limiter package is currently BETA and can change anytime.

Index

Constants

View Source
const (
	MetricNameRateLimitRelease  = "rate-limit-release"
	MetricNameRateLimitTake     = "rate-limit-take"
	MetricNameRateLimitThrottle = "rate-limit-throttle"
	MetricNameRateLimitError    = "rate-limit-error"
)

Variables

This section is empty.

Functions

func NewFixedWindowLimiter

func NewFixedWindowLimiter(backend Incrementer, clock clock.Clock, config FixedWindowConfig, builder *invocationBuilder) *fixedWindow

func NewMetricMiddlewareWithInterfaces

func NewMetricMiddlewareWithInterfaces(metricWriter metric.Writer) *metricMiddleware

Types

type Factory

type Factory func() (Limiter, error)

type FixedWindowConfig

type FixedWindowConfig struct {
	Name   string
	Cap    int
	Window time.Duration
}

type Incrementer

type Incrementer interface {
	Increment(ctx context.Context, prefix string) (incr *int, ttl *time.Duration, err error)
}

type Invocation

type Invocation interface {
	GetTraceId() string
	GetPrefix() string
	GetName() string
}

type Limiter

type Limiter interface {
	Wait(ctx context.Context, prefix string) error
}

func Chain

func Chain(ls ...Limiter) (Limiter, error)

func ChainFactories

func ChainFactories(fs ...func() (Limiter, error)) (Limiter, error)

func NewLeakyBucketLimiter

func NewLeakyBucketLimiter(name string, rate int) (Limiter, error)

func NewUnlimited

func NewUnlimited() Limiter

type LimiterWithMiddleware

type LimiterWithMiddleware interface {
	Limiter
	WithMiddleware(...MiddlewareFactory)
}

func NewFixedWindowDdb

func NewFixedWindowDdb(ctx context.Context, config cfg.Config, logger log.Logger, settings *ddb.Settings, c FixedWindowConfig) (LimiterWithMiddleware, error)

func NewFixedWindowRedis

func NewFixedWindowRedis(ctx context.Context, config cfg.Config, logger log.Logger, c FixedWindowConfig) (LimiterWithMiddleware, error)

func NewFixedWindowRedisWithInterfaces

func NewFixedWindowRedisWithInterfaces(clock clock.Clock, redis redis.Client, config FixedWindowConfig, builder *invocationBuilder) LimiterWithMiddleware

type Middleware

type Middleware interface {
	OnTake(ctx context.Context, i Invocation)
	OnRelease(ctx context.Context, i Invocation)
	OnError(ctx context.Context, i Invocation)
	OnThrottle(ctx context.Context, i Invocation)
}

func ChainMiddleware

func ChainMiddleware(fs ...MiddlewareFactory) Middleware

func NewMetricMiddleware

func NewMetricMiddleware() Middleware

type MiddlewareFactory

type MiddlewareFactory func() Middleware

func NewLoggingMiddleware

func NewLoggingMiddleware(logger log.Logger) MiddlewareFactory

Jump to

Keyboard shortcuts

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