rate

package
v1.0.3-mainnet Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultStrategy       = "default"
	LimitKeyCacheSize     = 5000
	LimitKeyExpirationTTL = 75 * time.Second
)
View Source
const (
	// limit types
	LimitTypeByKey = iota
	LimitTypeByIp

	ConfigStrategyPrefix = "ratelimit.strategy."
)
View Source
const (
	LimitKeyLength = 16
)

Variables

View Source
var (
	DefaultRegistryCfx = NewRegistry()
	DefaultRegistryEth = NewRegistry()
)

Functions

func GenerateRandomLimitKey

func GenerateRandomLimitKey(limitType int) (string, error)

func JsonUnmarshalStrategyRules

func JsonUnmarshalStrategyRules(data []byte) (map[string]Option, error)

Types

type Config

type Config struct {
	Strategies map[uint32]*Strategy // limit strategies
}

type IpLimiter

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

IpLimiter limiting by IP address

func NewIpLimiter

func NewIpLimiter(option Option) *IpLimiter

func (*IpLimiter) Allow

func (l *IpLimiter) Allow(vc *VisitContext, n int) bool

func (IpLimiter) GC

func (l IpLimiter) GC(timeout time.Duration)

func (IpLimiter) Update

func (l IpLimiter) Update(option Option) bool

type IpLimiterSet

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

IpLimiterSet limiting by IP address

func NewIpLimiterSet

func NewIpLimiterSet(s *Strategy) *IpLimiterSet

func (IpLimiterSet) GC

func (ls IpLimiterSet) GC(timeout time.Duration)

GC garbage collects limiter stale resources

func (IpLimiterSet) Get

func (ls IpLimiterSet) Get(vc *VisitContext) (Limiter, bool)

Get returns limiter for current visit context

func (IpLimiterSet) Update

func (ls IpLimiterSet) Update(s *Strategy)

Update updates with new strategy

type KeyBasedIpLimiterSet

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

KeyBasedIpLimiterSet limiting by IP address yet grouped by limit key

func NewKeyBasedIpLimiterSet

func NewKeyBasedIpLimiterSet(s *Strategy) *KeyBasedIpLimiterSet

func (KeyBasedIpLimiterSet) GC

func (ls KeyBasedIpLimiterSet) GC(timeout time.Duration)

GC garbage collects limiter stale resources

func (KeyBasedIpLimiterSet) Get

func (ls KeyBasedIpLimiterSet) Get(vc *VisitContext) (Limiter, bool)

Get returns limiter for current visit context

func (KeyBasedIpLimiterSet) Update

func (ls KeyBasedIpLimiterSet) Update(s *Strategy)

Update updates with new strategy

type KeyInfo

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

type KeyLimiter

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

KeyLimiter limiting by limit key

func NewKeyLimiter

func NewKeyLimiter(option Option) *KeyLimiter

func (*KeyLimiter) Allow

func (l *KeyLimiter) Allow(vc *VisitContext, n int) bool

func (KeyLimiter) GC

func (l KeyLimiter) GC(timeout time.Duration)

func (KeyLimiter) Update

func (l KeyLimiter) Update(option Option) bool

type KeyLimiterSet

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

KeyLimiterSet limiting by key

func NewKeyLimiterSet

func NewKeyLimiterSet(s *Strategy) *KeyLimiterSet

func (KeyLimiterSet) GC

func (ls KeyLimiterSet) GC(timeout time.Duration)

GC garbage collects limiter stale resources

func (KeyLimiterSet) Get

func (ls KeyLimiterSet) Get(vc *VisitContext) (Limiter, bool)

Get returns limiter for current visit context

func (KeyLimiterSet) Update

func (ls KeyLimiterSet) Update(s *Strategy)

Update updates with new strategy

type KeysetFilter

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

type KeysetLoader

type KeysetLoader func(filter *KeysetFilter) ([]*KeyInfo, error)

KeysetLoader limit keyset loader

type Limiter

type Limiter interface {
	Allow(vc *VisitContext, n int) bool
	GC(timeout time.Duration)
	Update(option Option) bool
}

type LimiterSet

type LimiterSet interface {
	Get(vc *VisitContext) (Limiter, bool)
	GC(timeout time.Duration)
	Update(s *Strategy)
}

LimiterSet limiter set assembled by strategy

type Option

type Option struct {
	Rate  rate.Limit
	Burst int
}

func NewOption

func NewOption(r int, b int) Option

type Registry

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

func NewRegistry

func NewRegistry() *Registry

func (*Registry) AutoReload

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

func (*Registry) GC

func (m *Registry) GC(timeout time.Duration)

func (*Registry) Get

func (m *Registry) Get(vc *VisitContext) (Limiter, bool)

type Strategy

type Strategy struct {
	ID    uint32            // strategy ID
	Name  string            // strategy name
	Rules map[string]Option // limit rules: rule name => rule option

	MD5 [md5.Size]byte `json:"-"` // config data fingerprint
}

Strategy rate limit strategy

type VisitContext

type VisitContext struct {
	Ip       string // visiter IP
	Key      string // visiter key
	Resource string // visited resource (also used as limit rule)
}

Jump to

Keyboard shortcuts

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