rate

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDriver = "memory"

	DefaultName = "api"
	API         = "api"
	Login       = "login"
	Admin       = "admin"
	SMS         = "sms"
)

Variables

This section is empty.

Functions

func Provider

func Provider(options ...ProviderOption) runaprovider.Provider

Types

type Algorithm

type Algorithm string
const (
	AlgorithmTokenBucket   Algorithm = "token_bucket"
	AlgorithmFixedWindow   Algorithm = "fixed_window"
	AlgorithmSlidingWindow Algorithm = "sliding_window"
)

type Driver

type Driver interface {
	Name() string
	Allow(ctx context.Context, rule Rule, key string) (Result, error)
	Reset(ctx context.Context, rule Rule, key string) error
	Close(ctx context.Context) error
}

Driver handles limit primitives.

func MemoryDriver

func MemoryDriver(options ...DriverOption) Driver

MemoryDriver creates a single-process rate driver.

type DriverOption

type DriverOption interface{ ApplyDriver(*DriverOptions) }

DriverOption configures driver stores.

func DriverMeta

func DriverMeta(key string, value any) DriverOption

DriverMeta stores arbitrary driver metadata.

func Name

func Name(value string) DriverOption

Name sets store name metadata.

func Prefix

func Prefix(value string) DriverOption

Prefix sets store key prefix.

type DriverOptions

type DriverOptions struct {
	Name   string
	Prefix string
	Meta   core.Map
}

DriverOptions stores driver metadata.

type Info

type Info struct {
	Name      string
	Driver    string
	Algorithm Algorithm
	Limit     int
	Window    time.Duration
	Burst     int
	Default   bool
	Meta      core.Map
}

Info describes one configured limiter.

type KeySource

type KeySource interface {
	Name() string
	Value(ctx any) string
}

KeySource resolves one key part outside the core package.

func ByCookie

func ByCookie(name string) KeySource

ByCookie uses a request cookie as a rate key source.

func ByFunc

func ByFunc(fn func(any) string) KeySource

ByFunc uses a custom function as a rate key source.

func ByHeader

func ByHeader(name string) KeySource

ByHeader uses a request header as a rate key source.

func ByIP

func ByIP() KeySource

ByIP uses ctx.IP() as a rate key source.

func ByParam

func ByParam(name string) KeySource

ByParam uses a route param as a rate key source.

func ByQuery

func ByQuery(name string) KeySource

ByQuery uses a query value as a rate key source.

func ByRoute

func ByRoute() KeySource

ByRoute uses route name or method+path as a rate key source.

func BySession

func BySession() KeySource

BySession uses current session id as a rate key source.

func ByUser

func ByUser() KeySource

ByUser uses current auth user id when auth is installed.

type KeySourceFunc

type KeySourceFunc struct {
	SourceName string
	Resolve    func(any) string
}

KeySourceFunc adapts a function to KeySource.

func (KeySourceFunc) Name

func (source KeySourceFunc) Name() string

func (KeySourceFunc) Value

func (source KeySourceFunc) Value(ctx any) string

type Limiter

type Limiter interface {
	Allow(ctx context.Context, keys ...string) (Result, error)
	Reset(ctx context.Context, keys ...string) error
}

Limiter is a named limiter.

type Option

type Option interface{ ApplyRate(*Rule) }

Option configures a named limiter.

func Burst

func Burst(value int) Option

Burst sets token bucket burst size.

func FixedWindow

func FixedWindow(limit int, window time.Duration) Option

FixedWindowRule configures fixed window limit.

func Key

func Key(sources ...KeySource) Option

Key sets rate key sources.

func Meta

func Meta(key string, value any) Option

Meta stores arbitrary limiter metadata.

func SlidingWindow

func SlidingWindow(limit int, window time.Duration) Option

SlidingWindowRule configures sliding window limit.

func TokenBucket

func TokenBucket(limit int, window time.Duration) Option

TokenBucket configures token bucket limit.

func Use

func Use(name string) Option

Driver selects the rate driver.

type ProviderOption

type ProviderOption func(*provider)

func RegisterDriver

func RegisterDriver(name string, driver Driver) ProviderOption

func RegisterKey

func RegisterKey(name string, source KeySource) ProviderOption

func RegisterLimiter

func RegisterLimiter(name string, options ...Option) ProviderOption

type Registry

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

func Default

func Default() *Registry

Default returns the default app rate registry.

func New

func New() *Registry

func (*Registry) Close

func (registry *Registry) Close(ctx context.Context) error

func (*Registry) Config

func (registry *Registry) Config(store *config.Store)

Config applies file/env config to already registered limiters.

func (*Registry) Info

func (registry *Registry) Info() []Info

func (*Registry) Key

func (registry *Registry) Key(name string, source KeySource)

func (*Registry) MustOf

func (registry *Registry) MustOf(name string) Limiter

func (*Registry) Of

func (registry *Registry) Of(name string) (Limiter, error)

func (*Registry) Rate

func (registry *Registry) Rate(name string, options ...Option)

func (*Registry) RegisterDriver

func (registry *Registry) RegisterDriver(name string, driver Driver)

func (*Registry) Rule

func (registry *Registry) Rule(name string) (Rule, bool)

func (*Registry) Shutdown

func (registry *Registry) Shutdown(ctx context.Context) error

Shutdown closes all rate drivers when managed by DI.

type Result

type Result struct {
	Allowed    bool
	Limit      int
	Remaining  int
	ResetAt    time.Time
	RetryAfter time.Duration
}

Result describes one allow decision.

type Rule

type Rule struct {
	Name      string
	Driver    string
	Algorithm Algorithm
	Limit     int
	Window    time.Duration
	Burst     int
	Key       []KeySource
	Meta      core.Map
}

Rule describes one limiter rule.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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