ratelimit

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RATE_LIMIT_TYPE_HEADER = "X-Rate-Limit-Type"
	RETRY_AFTER_HEADER     = "Retry-After"

	APP_RATE_LIMIT_HEADER          = "X-App-Rate-Limit"
	APP_RATE_LIMIT_COUNT_HEADER    = "X-App-Rate-Limit-Count"
	METHOD_RATE_LIMIT_HEADER       = "X-Method-Rate-Limit"
	METHOD_RATE_LIMIT_COUNT_HEADER = "X-Method-Rate-Limit-Count"

	APP_RATE_LIMIT_TYPE     = "application"
	METHOD_RATE_LIMIT_TYPE  = "method"
	SERVICE_RATE_LIMIT_TYPE = "service"

	DEFAULT_RETRY_AFTER = 2 * time.Second
)

Variables

View Source
var (
	ErrContextDeadlineExceeded = errors.New("waiting would exceed context deadline")
)

Functions

func WaitN

func WaitN(ctx context.Context, estimated time.Time, duration time.Duration) error

WaitN waits for the given duration after checking if the context deadline will be exceeded.

Types

type Bucket

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

func NewBucket

func NewBucket(interval time.Duration, intervalOverhead time.Duration, baseLimit int, limit int, tokens int) *Bucket

func (*Bucket) MarshalZerologObject

func (b *Bucket) MarshalZerologObject(encoder *zerolog.Event)

type Limit

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

Limit represents a collection of buckets and the type of limit (application or method).

func NewLimit

func NewLimit(limitType string) *Limit

type Limits

type Limits struct {
	App     *Limit
	Methods map[string]*Limit
}

Limits in a route.

func NewLimits

func NewLimits() *Limits

type RateLimit

type RateLimit struct {
	Route   map[string]*Limits
	Enabled bool
	// Factor to be applied to the limit. E.g. if set to 0.5, the limit will be reduced by 50%.
	LimitUsageFactor float64
	// Delay in milliseconds to be added to reset intervals.
	IntervalOverhead time.Duration
	// contains filtered or unexported fields
}

func NewInternalRateLimit added in v0.19.3

func NewInternalRateLimit(limitUsageFactor float64, intervalOverhead time.Duration) *RateLimit

func (*RateLimit) CheckRetryAfter

func (r *RateLimit) CheckRetryAfter(route string, methodID string, headers http.Header) time.Duration

CheckRetryAfter returns the number of seconds to wait from the Retry-After header before retrying, or DEFAULT_RETRY_AFTER if not found.

func (*RateLimit) MarshalZerologObject added in v1.0.0

func (r *RateLimit) MarshalZerologObject(encoder *zerolog.Event)

func (*RateLimit) Reserve added in v1.0.2

func (r *RateLimit) Reserve(ctx context.Context, logger zerolog.Logger, route string, methodID string) error

Reserves one request for the App and Method buckets in a route.

If rate limited, will block until the next bucket reset.

func (*RateLimit) Update

func (r *RateLimit) Update(logger zerolog.Logger, route string, methodID string, headers http.Header)

Update creates new buckets in a route with the limits provided in the response headers.

Jump to

Keyboard shortcuts

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