ratelimiter

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// KeepAlivePeriod is period checking if endpoint is alive.
	KeepAlivePeriod = 33

	// DEFAULTUPDATEPERIOD is the default period(seconds) to update rate.
	DEFAULTUPDATEPERIOD = 1.0

	// RateLimiterTimeout is timer waiting for a rate limiter token.
	RateLimiterTimeout = 10

	// APIWaitMinThreshold is threshold(second) which will trigger rate limiter adjust.
	APIWaitMinThreshold = float64(0.01)

	// MAXRATELIMIT means max rate for rate limiter.
	MAXRATELIMIT = 100
)

Variables

View Source
var (
	APIReduceRateCodes = [2]int{429, 503}
)

APIReduceRateCodes is http status code set which will trigger rate limiter adjust.

Functions

This section is empty.

Types

type AIMDRateLimter

type AIMDRateLimter struct {
	sync.Mutex
	// contains filtered or unexported fields
}

AIMDRateLimter is rate limiter which could adjuct its' rate depending on wait time and http status code.

func (*AIMDRateLimter) AdjustRate

func (limiter *AIMDRateLimter) AdjustRate(waitTime time.Duration, statusCode int)

AdjustRate adjust upper limit for rate limiter.

func (*AIMDRateLimter) Wait

func (limiter *AIMDRateLimter) Wait()

Wait blocks the caller until a token is gain.

type FixRateLimiter

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

FixRateLimiter is rate limiter which has fix rate.

func (*FixRateLimiter) AdjustRate

func (limiter *FixRateLimiter) AdjustRate(waitTime time.Duration, statusCode int)

AdjustRate adjust upper limit for rate limiter, it's empty for FixRateLimiter.

func (*FixRateLimiter) Wait

func (limiter *FixRateLimiter) Wait()

Wait blocks the caller until a token is gained.

type RateLimiter

type RateLimiter interface {
	Wait()
	AdjustRate(time.Duration, int)
	// contains filtered or unexported methods
}

RateLimiter limits the REST API speed.

func NewAIMDRateLimiter

func NewAIMDRateLimiter(max int, period float64) RateLimiter

NewAIMDRateLimiter creates AIMD rate limiter. max ==0 disables rate limiter.

func NewFixRateLimiter

func NewFixRateLimiter(max int) RateLimiter

NewFixRateLimiter creates AIMD rate limiter. max ==0 disables rate limiter.

func NewRateLimiter

func NewRateLimiter(rateLimiterType Type) RateLimiter

NewRateLimiter creates rate limeter based on RateLimiterType

type Type

type Type int32

Type is rate limiter type.

const (
	// FIXRATE is a limiter which has fix rate.
	FIXRATE Type = iota
	// AIMD is a limiter which rate will adjuct depending on wait time and http status code.
	AIMD Type = 1
)

Jump to

Keyboard shortcuts

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