rate

package
v1.3.3-testnet Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LimitKeyCacheSize     = 5000
	LimitKeyExpirationTTL = 60 * time.Second
)
View Source
const (
	// pre-defined default strategy name
	DefaultStrategy = "default"
)
View Source
const (
	GCScheduleInterval = 5 * time.Minute
)
View Source
const (
	LimitKeyLength = 32
)

Variables

This section is empty.

Functions

func GenerateRandomLimitKey

func GenerateRandomLimitKey(limitType LimitType) (string, error)

Types

type Config

type Config struct {
	// checksums for modification detection
	CheckSums ConfigCheckSums

	Strategies map[uint32]*Strategy      // limit strategies
	AllowLists map[uint32]*acl.AllowList // allow lists
}

type ConfigCheckSums

type ConfigCheckSums struct {
	Strategies map[uint32][md5.Size]byte
	AllowLists map[uint32][md5.Size]byte
}

ConfigCheckSums config md5 checksum

type FixedWindowOption

type FixedWindowOption struct {
	Interval time.Duration
	Quota    int
}

FixedWindowOption limit option for fixed window

func (*FixedWindowOption) UnmarshalJSON

func (fwo *FixedWindowOption) UnmarshalJSON(data []byte) error

UnmarshalJSON implements `json.Unmarshaler`

type KeyInfo

type KeyInfo struct {
	SID   uint32    // bound strategy ID
	AclID uint32    // bound allowlist ID
	Key   string    // limit key
	Type  LimitType // limit type
}

type KeyLoader

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

func NewKeyLoader

func NewKeyLoader(ksload ksLoadFunc) *KeyLoader

func (*KeyLoader) Load

func (l *KeyLoader) Load(key string) (*KeyInfo, bool)

Load loads key info from cache or raw loading from somewhere else if cache missed.

type KeysetFilter

type KeysetFilter struct {
	SIDs   []uint32 // strategy IDs
	KeySet []string // limit key set
	Limit  int      // result limit size (<= 0 means none)
}

type LimitAlgoType

type LimitAlgoType string
const (
	// rate limit algorithms, only `fixed_window` and `token bucket` are supported for now.
	LimitAlgoFixedWindow LimitAlgoType = "fixed_window"
	LimitAlgoTokenBucket LimitAlgoType = "token_bucket"
)

type LimitRule

type LimitRule struct {
	Algo   LimitAlgoType
	Option interface{}
}

LimitRule resource limit rule

func (*LimitRule) UnmarshalJSON

func (r *LimitRule) UnmarshalJSON(data []byte) (err error)

type LimitType

type LimitType int
const (
	// rate limit types, only `ip` or `key` are supported for now.
	LimitTypeByKey LimitType = iota
	LimitTypeByIp
)

type Registry

type Registry struct {
	*http.Registry
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry(kloader *KeyLoader, valFactory acl.ValidatorFactory) *Registry

func (Registry) Allow

func (r Registry) Allow(ctx acl.Context) error

func (*Registry) AutoReload

func (m *Registry) AutoReload(interval time.Duration, reloader func() (*Config, error))

func (*Registry) Create

func (r *Registry) Create(ctx context.Context, resource, group string) (rate.Limiter, error)

func (*Registry) GetGroupAndKey

func (r *Registry) GetGroupAndKey(
	ctx context.Context,
	resource string,
) (group, key string, err error)

type SVipStatus

type SVipStatus = KeyInfo

func SVipStatusFromContext

func SVipStatusFromContext(ctx context.Context) (*SVipStatus, bool)

SVipStatusFromContext returns SVIP status from context

type Strategy

type Strategy struct {
	ID   uint32 // strategy ID
	Name string // strategy name

	LimitOptions map[string]interface{} // resource => limit option
}

Strategy rate limit strategy

func NewStrategy

func NewStrategy(id uint32, name string) *Strategy

func (*Strategy) UnmarshalJSON

func (s *Strategy) UnmarshalJSON(data []byte) error

UnmarshalJSON implements `json.Unmarshaler`

type TokenBucketOption

type TokenBucketOption struct {
	Rate  rate.Limit
	Burst int
}

FixedWindowOption limit option for token bucket

func NewTokenBucketOption

func NewTokenBucketOption(r, b int) TokenBucketOption

Jump to

Keyboard shortcuts

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