ratelimit

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeWindow

func NormalizeWindow(window time.Duration) time.Duration

NormalizeWindow enforces the minimum one-minute bucket granularity.

func RetryAfter

func RetryAfter(now time.Time, window time.Duration) time.Duration

RetryAfter returns the remaining duration before the current window rolls.

Types

type Limiter

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

Limiter atomically consumes resource units from durable fixed windows.

func NewLimiter

func NewLimiter(db *gorm.DB, opts ...Option) *Limiter

NewLimiter creates a database-backed limiter.

func (*Limiter) Acquire

func (l *Limiter) Acquire(ctx context.Context, resource string, units, limit int, window time.Duration) (bool, error)

Acquire consumes units for resource if the current fixed window has capacity. It is intentionally one guarded raw upsert; GORM's clause.OnConflict cannot express the WHERE guard needed for atomic cross-node rejection.

type Option

type Option func(*Limiter)

Option configures a Limiter.

func WithClock

func WithClock(now clockFunc) Option

WithClock overrides the limiter clock for tests.

type Pruner

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

Pruner deletes expired rate-limit windows.

func NewPruner

func NewPruner(db *gorm.DB, opts ...Option) *Pruner

NewPruner creates a rate-limit token pruner.

func (*Pruner) Prune

func (p *Pruner) Prune(ctx context.Context) (int, error)

Prune removes expired windows and returns the number of deleted rows.

func (*Pruner) Run

func (p *Pruner) Run(ctx context.Context, interval time.Duration)

Run starts the pruning loop and returns when ctx is cancelled.

type Rule

type Rule struct {
	Resource string
	Units    int
	Limit    int
	Window   time.Duration
	JobAlias string
}

Rule is the resolved rate-limit contract for one task.

func RuleForTask

func RuleForTask(ctx context.Context, db *gorm.DB, runID, taskID uuid.UUID) (*Rule, bool, error)

RuleForTask resolves the persisted task-level rate-limit fields and owning job-level declaration for a task run.

func RuleFromDeclarations

func RuleFromDeclarations(jobAlias string, raw datatypes.JSON, resource string, units int) (*Rule, bool, error)

RuleFromDeclarations resolves a task resource/units pair against serialized job-level metadata.rateLimits declarations.

func RuleFromRateLimits

func RuleFromRateLimits(jobAlias string, declarations []jobdefschema.RateLimit, resource string, units int) (*Rule, bool, error)

RuleFromRateLimits resolves a task resource/units pair against parsed job-level metadata.rateLimits declarations.

Jump to

Keyboard shortcuts

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