rate_limit

package
v0.0.0-...-10a1391 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AppRateLimitHeader         = "X-App-Rate-Limit"
	AppRateLimitCountHeader    = "X-App-Rate-Limit-Count"
	MethodRateLimitHeader      = "X-Method-Rate-Limit"
	MethodRateLimitCountHeader = "X-Method-Rate-Limit-Count"
)

Variables

View Source
var (
	ErrRateLimitingIsDisabled = errors.New("Rate limiting is disabled")
)

Functions

This section is empty.

Types

type Enpoints

type Enpoints struct {
	Endpoints map[string]*Methods
	AppRate   *Rate
}

type InternalRateLimitClient

type InternalRateLimitClient interface {
	Get(route interface{}, endpointName string, methodName string) (*Rate, error)
	GetAppRate(route interface{}) (*Rate, error)
	Set(route interface{}, endpointName string, methodName string, headers *http.Header) error
	SetAppRate(route interface{}, headers *http.Header) error
	ParseHeaders(headers *http.Header, limitHeader string, countHeader string) (*Rate, error)
	IsRateLimited(rate *Rate) (bool, error)
}

type InternalRateStore

type InternalRateStore struct {
	Route map[interface{}]*Enpoints
	// contains filtered or unexported fields
}

func (*InternalRateStore) Get

func (s *InternalRateStore) Get(route interface{}, endpointName string, methodName string) (*Rate, error)

func (*InternalRateStore) GetAppRate

func (s *InternalRateStore) GetAppRate(route interface{}) (*Rate, error)

func (*InternalRateStore) IsRateLimited

func (s *InternalRateStore) IsRateLimited(rate *Rate) (bool, error)

func (*InternalRateStore) Set

func (s *InternalRateStore) Set(route interface{}, endpointName string, methodName string, headers *http.Header) error

func (*InternalRateStore) SetAppRate

func (s *InternalRateStore) SetAppRate(route interface{}, headers *http.Header) error

func (*InternalRateStore) UpdateInternalRateCount

func (s *InternalRateStore) UpdateInternalRateCount(old *Rate, new *Rate)

type Methods

type Methods struct {
	Methods map[string]*Rate
}

type Rate

type Rate struct {
	Seconds RateTiming
	Minutes RateTiming
}

func ParseHeaders

func ParseHeaders(headers *http.Header, limitHeader string, countHeader string) (*Rate, error)

type RateLimit

type RateLimit struct {
	Enabled   bool
	StoreType RateLimitStoreType
	// contains filtered or unexported fields
}

func NewInternalRateLimit

func NewInternalRateLimit() *RateLimit

func (*RateLimit) Get

func (r *RateLimit) Get(route interface{}, endpointName string, methodName string) (*Rate, error)

func (*RateLimit) GetAppRate

func (r *RateLimit) GetAppRate(route interface{}) (*Rate, error)

func (*RateLimit) IsRateLimited

func (r *RateLimit) IsRateLimited(rate *Rate) (bool, error)

func (*RateLimit) Set

func (r *RateLimit) Set(route interface{}, endpointName string, methodName string, headers *http.Header) error

func (*RateLimit) SetAppRate

func (r *RateLimit) SetAppRate(route interface{}, headers *http.Header) error

type RateLimitStoreType

type RateLimitStoreType string
const (
	InternalRateLimiter RateLimitStoreType = "Internal"
	RedisRateLimiter    RateLimitStoreType = "Redis"
)

type RateTiming

type RateTiming struct {
	// The amount in seconds the rate limit should reset.
	Time int
	// Maximum amount of requests in n seconds.
	Limit int
	// Current count of requests made in n seconds.
	Count int

	Expire time.Time
	Access time.Time
}

type Store

type Store interface {
	Get(route interface{}, endpointName string, methodName string) (*Rate, error)
	GetAppRate(route interface{}) (*Rate, error)
	Set(route interface{}, endpointName string, methodName string, headers *http.Header) error
	SetAppRate(route interface{}, headers *http.Header) error
	IsRateLimited(rate *Rate) (bool, error)
}

Jump to

Keyboard shortcuts

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