rate

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetRateLimitHTTPHeaders

func SetRateLimitHTTPHeaders(w http.ResponseWriter, res *Result)

SetRateLimitHTTPHeaders sets rate limit headers to the response.

Types

type Config

type Config struct {
	Enable bool `yaml:"enable"`

	StoreType string `yaml:"storeType"`

	Redis *RedisStoreConfig `yaml:"redis"`

	// Rate is allowed tokens for the period.
	Rate int `yaml:"rate"`
	// Period is the time period for the rate.
	Period time.Duration `yaml:"period"`
	// Burst is the maximum burst capacity.
	Burst int `yaml:"burst"`
}

Config is the configuration for rate limiter.

func (*Config) Validate

func (c *Config) Validate() error

Validate validates the configuration.

type Limiter

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

Limiter is a rate limiter.

func NewLimiter

func NewLimiter(c Config, logger logr.Logger) Limiter

NewLimiter returns a new rate limiter.

func (*Limiter) Take

func (l *Limiter) Take(ctx context.Context, key string) (*Result, error)

Take takes a token from the given key if available.

type RedisStoreConfig

type RedisStoreConfig struct {
	// host:port address.
	Address string `yaml:"address"`

	Username string `yaml:"username"`
	Password string `yaml:"-"`
	Database int    `yaml:"database"`
}

RedisStoreConfig is the configuration for RedisStore.

type Result

type Result struct {
	// Allowed is true if the token is available.
	Allowed bool
	// Limit is the maximum number of tokens.
	Limit int
	// Remaining is the number of remaining tokens.
	Remaining int
	// RetryAfter is the duration until the next token becomes available.
	RetryAfter time.Duration
	// ResetAfter is the duration until the rate limit completely resets.
	ResetAfter time.Duration
}

Result is the result of a Take call.

Jump to

Keyboard shortcuts

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