rate

package
v1.6.7-testnet Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LimitKeyCacheSize     = 5000
	LimitKeyExpirationTTL = 60 * time.Second
)
View Source
const (
	UserTypeGuest       = "guest"
	UserTypeWeb3Pay     = "web3pay_user"
	UserTypeProvisioned = "provisioned_user"
)
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 CompositeResolver

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

CompositeResolver tries multiple resolvers in order until one matches.

func NewCompositeResolver

func NewCompositeResolver(resolvers ...StrategyResolver) *CompositeResolver

func (*CompositeResolver) Resolve

func (r *CompositeResolver) Resolve(ctx context.Context, reg *Registry) (*StrategyDecision, error)

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 DefaultResolver

type DefaultResolver struct{}

func (*DefaultResolver) Resolve

func (r *DefaultResolver) Resolve(ctx context.Context, reg *Registry) (*StrategyDecision, error)

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
)

func (LimitType) String

func (typ LimitType) String() string

type ProvisionedResolver

type ProvisionedResolver struct{}

func (*ProvisionedResolver) Resolve

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)

func (*Registry) Resolve

func (r *Registry) Resolve(ctx context.Context) (*StrategyDecision, error)

func (*Registry) Strategies

func (r *Registry) Strategies() []*Strategy

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 StrategyDecision

type StrategyDecision struct {
	Strategy  *Strategy            // matched static strategy
	LimitType LimitType            // rate limit type
	LimitKey  string               // unique key for limiter
	UserMeta  UserMetaData         // runtime user metadata
	MatchedBy StrategyResolverType // which resolver matched
}

StrategyDecision is the result of resolving which rate limit strategy applies to the current request, including its runtime parameters.

type StrategyResolver

type StrategyResolver interface {
	Resolve(ctx context.Context, reg *Registry) (decision *StrategyDecision, err error)
}

type StrategyResolverType

type StrategyResolverType string
const (
	ResolverDefault     StrategyResolverType = "default"
	ResolverWeb3pay     StrategyResolverType = "web3pay"
	ResolverProvisioned StrategyResolverType = "provisioned"
)

type TokenBucketOption

type TokenBucketOption struct {
	Rate  rate.Limit
	Burst int
}

TokenBucketOption limit option for token bucket

func NewTokenBucketOption

func NewTokenBucketOption(r, b int) TokenBucketOption

type UserMetaData

type UserMetaData map[string]any

type Web3payResolver

type Web3payResolver struct{}

func (*Web3payResolver) Resolve

func (r *Web3payResolver) Resolve(ctx context.Context, reg *Registry) (*StrategyDecision, error)

Jump to

Keyboard shortcuts

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