limiter

package
v0.0.0-...-c9c6fa8 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	LimiterConfig
}

Config holds per-request configuration for Allow calls. These values override the defaults set by LimiterConfig at construction time.

type FixedWindowLimiter

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

func NewFixedWindow

func NewFixedWindow(concurrent int) *FixedWindowLimiter

func (*FixedWindowLimiter) Close

func (l *FixedWindowLimiter) Close()

func (*FixedWindowLimiter) ReleaseToken

func (l *FixedWindowLimiter) ReleaseToken()

func (*FixedWindowLimiter) TakeToken

func (l *FixedWindowLimiter) TakeToken()

func (*FixedWindowLimiter) TakeTokenNonBlocking

func (l *FixedWindowLimiter) TakeTokenNonBlocking() bool

func (*FixedWindowLimiter) TakeTokenWithTimeout

func (l *FixedWindowLimiter) TakeTokenWithTimeout(timeout time.Duration) bool

func (*FixedWindowLimiter) Wait

func (l *FixedWindowLimiter) Wait()

type KeyFixedWindowCallConfig

type KeyFixedWindowCallConfig struct {
	MaxConcurrent int64
}

type KeyFixedWindowCallOption

type KeyFixedWindowCallOption func(*KeyFixedWindowCallConfig)

func WithMaxConcurrent

func WithMaxConcurrent(m int64) KeyFixedWindowCallOption

type KeyFixedWindowConfig

type KeyFixedWindowConfig struct {
	MaxConcurrent int64
	Expiration    time.Duration
}

type KeyFixedWindowLimiter

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

func NewKeyFixedWindowLimiter

func NewKeyFixedWindowLimiter(ctx context.Context, r redis.Cmdable, opts ...KeyFixedWindowOption) (*KeyFixedWindowLimiter, error)

func (*KeyFixedWindowLimiter) Acquire

func (*KeyFixedWindowLimiter) CurrentConcurrent

func (kl *KeyFixedWindowLimiter) CurrentConcurrent(ctx context.Context, key string) (int64, error)

CurrentConcurrent returns the approximate current concurrent count for the given key. The value may be stale immediately after reading due to concurrent Acquire/Release operations from other clients. Use this for monitoring and observability only.

func (*KeyFixedWindowLimiter) Release

func (kl *KeyFixedWindowLimiter) Release(ctx context.Context, key string) (int64, error)

type KeyFixedWindowLimiterInterface

type KeyFixedWindowLimiterInterface interface {
	Acquire(ctx context.Context, key string, opts ...KeyFixedWindowCallOption) (bool, int64, error)
	Release(ctx context.Context, key string) (int64, error)
	CurrentConcurrent(ctx context.Context, key string) (int64, error)
}

type KeyFixedWindowOption

type KeyFixedWindowOption func(*KeyFixedWindowConfig)

func WithDefaultExpiration

func WithDefaultExpiration(d time.Duration) KeyFixedWindowOption

func WithDefaultMaxConcurrent

func WithDefaultMaxConcurrent(m int64) KeyFixedWindowOption

type Limiter

type Limiter interface {
	TakeToken()
	TakeTokenNonBlocking() bool
	TakeTokenWithTimeout(timeout time.Duration) bool
}

type LimiterConfig

type LimiterConfig struct {
	Capacity  int64
	Rate      int64
	Requested int64
}

LimiterConfig holds the default configuration for RedisLimiter construction. These values are used when Allow is called without explicit Option overrides.

type LimiterOption

type LimiterOption func(*LimiterConfig)

LimiterOption configures default values at construction time.

func WithDefaultCapacity

func WithDefaultCapacity(c int64) LimiterOption

func WithDefaultRate

func WithDefaultRate(r int64) LimiterOption

func WithDefaultRequested

func WithDefaultRequested(n int64) LimiterOption

type Option

type Option func(*Config)

Option overrides parameters on a per-request basis.

func WithCapacity

func WithCapacity(c int64) Option

func WithRate

func WithRate(r int64) Option

func WithRequested

func WithRequested(n int64) Option

type PlanConfig

type PlanConfig struct {
	Budget     int64
	Window     time.Duration
	BucketSize time.Duration
	Weight     float64
}

type PlanLimiter

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

func NewPlanLimiter

func NewPlanLimiter(ctx context.Context, r redis.Cmdable, opts ...PlanOption) (*PlanLimiter, error)

func (*PlanLimiter) Allow

func (pl *PlanLimiter) Allow(ctx context.Context, key string, model string, tokens int64, opts ...PlanOption) (bool, *PlanUsage, error)

func (*PlanLimiter) Refund

func (pl *PlanLimiter) Refund(ctx context.Context, key string, model string, tokens int64, opts ...PlanOption) (*PlanUsage, error)

func (*PlanLimiter) Usage

func (pl *PlanLimiter) Usage(ctx context.Context, key string, opts ...PlanOption) (*PlanUsage, error)

type PlanLimiterInterface

type PlanLimiterInterface interface {
	Allow(ctx context.Context, key string, model string, tokens int64, opts ...PlanOption) (bool, *PlanUsage, error)
	Refund(ctx context.Context, key string, model string, tokens int64, opts ...PlanOption) (*PlanUsage, error)
	Usage(ctx context.Context, key string, opts ...PlanOption) (*PlanUsage, error)
}

type PlanOption

type PlanOption func(*PlanConfig)

func WithPlanBucketSize

func WithPlanBucketSize(bs time.Duration) PlanOption

func WithPlanBudget

func WithPlanBudget(b int64) PlanOption

func WithPlanWeight

func WithPlanWeight(w float64) PlanOption

func WithPlanWindow

func WithPlanWindow(w time.Duration) PlanOption

type PlanUsage

type PlanUsage struct {
	TotalUsed int64
	Budget    int64
	Remaining int64
	WindowEnd time.Time
	ByModel   map[string]int64
}

type RedisLimiter

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

func NewRedisLimiter

func NewRedisLimiter(ctx context.Context, r redis.Cmdable, opts ...LimiterOption) (*RedisLimiter, error)

NewRedisLimiter creates a new RedisLimiter instance. LimiterOption configures the default values used when Allow is called without explicit options. This is distinct from Option, which overrides parameters on a per-request basis.

func (*RedisLimiter) Allow

func (rl *RedisLimiter) Allow(ctx context.Context, key string, opts ...Option) (bool, error)

Allow checks whether a request is allowed under the token bucket rate limit. Option overrides the default capacity/rate/requested for this specific call. This is distinct from LimiterOption, which sets the default values at construction time.

type RedisLimiterInterface

type RedisLimiterInterface interface {
	Allow(ctx context.Context, key string, opts ...Option) (bool, error)
}

RedisLimiterInterface defines the interface for token bucket rate limiting.

type XTokenBucket

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

func NewXTokenBucket

func NewXTokenBucket(qos, burst int) *XTokenBucket

func (*XTokenBucket) TakeToken

func (x *XTokenBucket) TakeToken()

func (*XTokenBucket) TakeTokenNonBlocking

func (x *XTokenBucket) TakeTokenNonBlocking() bool

func (*XTokenBucket) TakeTokenWithTimeout

func (x *XTokenBucket) TakeTokenWithTimeout(timeout time.Duration) bool

Jump to

Keyboard shortcuts

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