ratelimit

package
v2.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 9 Imported by: 1

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 = 1 * time.Second
)

Variables

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

	ErrRateLimitIsDisabled = errors.New("rate limit is disabled")
)

Functions

func GetNumbersFromPair

func GetNumbersFromPair(pair string) (int, time.Duration)

Returns the limit and interval in seconds from a pair of numbers separated by a colon.

func GetRetryAfterHeader

func GetRetryAfterHeader(retryAfterHeader string) time.Duration

Returns the time.Duration in seconds to wait from the Retry-After header, DEFAULT_RETRY_AFTER if not found.

func ValidateRateLimitOptions

func ValidateRateLimitOptions(limitUsageFactor float64, intervalOverhead time.Duration) (float64, time.Duration)

Checks if the limit usage factor and interval overhead within a valid range.

func WaitN

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

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

Types

type Bucket

type Bucket struct {
	// Next reset.
	Next time.Time
	// Current number of tokens, starts at limit.
	Tokens int
	// The limit given in the header without any modifications.
	BaseLimit int
	// Maximum amount of tokens, modified by the LimitUsageFactor.
	Limit int
	// Time interval in seconds.
	Interval         time.Duration
	IntervalOverhead time.Duration
	// contains filtered or unexported fields
}

func NewBucket

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

func (*Bucket) Check

func (b *Bucket) Check()

Checks if the next reset is in the past, and if so, reset the bucket and set the next reset.

func (*Bucket) IsRateLimited

func (b *Bucket) IsRateLimited() bool

Increments the number of tokens in the bucket and returns if the bucket is rate limited.

func (*Bucket) MarshalZerologObject

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

type InternalRateLimitStore

type InternalRateLimitStore struct {
	Route map[string]*Limits
	// contains filtered or unexported fields
}

func (*InternalRateLimitStore) Reserve

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

func (*InternalRateLimitStore) Update

func (r *InternalRateLimitStore) Update(ctx context.Context, logger zerolog.Logger, route string, methodID string, headers http.Header, retryAfter time.Duration) error

type Limit

type Limit struct {
	Type       string
	Buckets    []*Bucket
	RetryAfter time.Duration
	// contains filtered or unexported fields
}

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

func NewLimit

func NewLimit(limitType string) *Limit

func ParseHeaders

func ParseHeaders(limitType string, limitHeader string, countHeader string, limitUsageFactor float64, intervalOverhead time.Duration) *Limit

Parses the headers and returns a new Limit with its buckets.

func (*Limit) CheckBuckets

func (l *Limit) CheckBuckets(ctx context.Context, logger zerolog.Logger, route string) error

Checks if any of the buckets provided are rate limited, and if so, blocks until the next reset.

func (*Limit) LimitsMatch

func (l *Limit) LimitsMatch(limitHeader string) bool

Checks if the limits given in the header match the current buckets.

func (*Limit) MarshalZerologObject

func (l *Limit) MarshalZerologObject(encoder *zerolog.Event)

func (*Limit) SetRetryAfter

func (l *Limit) SetRetryAfter(delay time.Duration)

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 {
	StoreType StoreType
	// Factor to be applied to any Limit. E.g. If set to 0.5, the limit will be reduced by 50%.
	LimitUsageFactor float64
	// Delay, in milliseconds, added to reset intervals.
	IntervalOverhead time.Duration
	Enabled          bool
	// contains filtered or unexported fields
}

func NewInternalRateLimit

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

func (*RateLimit) Reserve

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

func (*RateLimit) Update

func (r *RateLimit) Update(ctx context.Context, logger zerolog.Logger, route string, methodID string, headers http.Header, retryAfter time.Duration) error

type Store

type Store interface {
	// Reserves one request for the App and Method buckets in a route.
	//
	// If rate limited, will block until the next bucket reset.
	Reserve(ctx context.Context, logger zerolog.Logger, route string, methodID string, isRSO bool) error

	// Creates new buckets in a route with the limits provided in the response headers.
	Update(ctx context.Context, logger zerolog.Logger, route string, methodID string, headers http.Header, retryAfter time.Duration) error
}

type StoreType

type StoreType string
const (
	InternalRateLimit StoreType = "Internal"
)

Jump to

Keyboard shortcuts

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