Documentation
¶
Index ¶
- Constants
- Variables
- func GetNumbersFromPair(pair string) (int, time.Duration)
- func GetRetryAfterHeader(retryAfterHeader string) time.Duration
- func ValidateRateLimitOptions(limitUsageFactor float64, intervalOverhead time.Duration) (float64, time.Duration)
- func WaitN(ctx context.Context, estimated time.Time, duration time.Duration) error
- type Bucket
- type InternalRateLimitStore
- type Limit
- type Limits
- type RateLimit
- type Store
- type StoreType
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 ¶
Returns the limit and interval in seconds from a pair of numbers separated by a colon.
func GetRetryAfterHeader ¶
Returns the time.Duration in seconds to wait from the Retry-After header, DEFAULT_RETRY_AFTER if not found.
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 (*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 ¶
Increments the number of tokens in the bucket and returns if the bucket is rate limited.
func (*Bucket) MarshalZerologObject ¶
type InternalRateLimitStore ¶
type InternalRateLimitStore struct {
Route map[string]*Limits
// contains filtered or unexported fields
}
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 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 ¶
Checks if any of the buckets provided are rate limited, and if so, blocks until the next reset.
func (*Limit) LimitsMatch ¶
Checks if the limits given in the header match the current buckets.
func (*Limit) MarshalZerologObject ¶
func (*Limit) SetRetryAfter ¶
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 ¶
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
}
Click to show internal directories.
Click to hide internal directories.