quota

package
v0.28.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package quota implements a persisted, long-term traffic-volume limiter that can be shared across services by name. Unlike the rate limiters in limiter/traffic and limiter/rate, it accumulates total bytes within a window [startsAt, expiresAt) and stops the referencing service(s) once a byte limit is reached. Enforcement is fail-open outside the window.

Index

Constants

This section is empty.

Variables

View Source
var ErrQuotaExceeded = errors.New("quota: traffic limit reached")

Functions

This section is empty.

Types

type Direction

type Direction int
const (
	DirectionTotal Direction = iota
	DirectionIn
	DirectionOut
)

func (Direction) String

func (d Direction) String() string

type Limiter

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

func NewLimiter

func NewLimiter(name string, opts Options) *Limiter

func (*Limiter) AddIn

func (l *Limiter) AddIn(n int)

func (*Limiter) AddOut

func (l *Limiter) AddOut(n int)

func (*Limiter) Blocked

func (l *Limiter) Blocked() bool

func (*Limiter) Close

func (l *Limiter) Close() error

Close makes the limiter inert (Blocked false, counting a no-op) so deleting a shared quota releases the referencing services instead of tearing them down. Called by the registry on Unregister.

func (*Limiter) Snapshot

func (l *Limiter) Snapshot() Snapshot

func (*Limiter) Update

func (l *Limiter) Update(u Update)

func (*Limiter) WaitChan

func (l *Limiter) WaitChan() <-chan struct{}

WaitChan must be fetched before checking Blocked to avoid a missed wakeup.

type Options

type Options struct {
	Limit     uint64
	StartsAt  time.Time
	ExpiresAt time.Time
	Direction Direction
	Flush     time.Duration
	Store     Store
	Logger    logger.Logger
}

Options seeds a Limiter. Limit and the window are config-authoritative; a persisted counter is restored only for a matching window (see NewLimiter).

type Record

type Record struct {
	Used      uint64 `json:"used"`
	Limit     uint64 `json:"limit"`
	StartsAt  int64  `json:"startsAt"`  // unixnano; 0 = unset
	ExpiresAt int64  `json:"expiresAt"` // unixnano; 0 = unset
	UpdatedAt int64  `json:"updatedAt"`
}

type RedisConfig

type RedisConfig struct {
	Addr     string
	Username string
	Password string
	DB       int
	Key      string
}

type Snapshot

type Snapshot struct {
	Used          uint64
	Limit         uint64
	StartsAtUnix  int64
	ExpiresAtUnix int64
	Active        bool
	Expired       bool
	Blocked       bool
	Direction     string
}

type Store

type Store interface {
	// Load returns ok=false when no record exists yet.
	Load(name string) (rec Record, ok bool, err error)
	Save(name string, rec Record) error
}

Store persists per-name quota records. Implementations must be concurrency-safe.

func NewFileStore

func NewFileStore(path string) Store

func NewRedisStore

func NewRedisStore(cfg RedisConfig) Store

type Update

type Update struct {
	Used      *uint64
	Limit     *uint64
	StartsAt  *time.Time
	ExpiresAt *time.Time
}

Update overwrites runtime state; a nil field is left unchanged.

Directories

Path Synopsis
Package wrapper adapts a named quota onto a listener and its connections, resolving the quota.Limiter by name from the registry on every call so create/update/delete takes effect live and several services can share one.
Package wrapper adapts a named quota onto a listener and its connections, resolving the quota.Limiter by name from the registry on every call so create/update/delete takes effect live and several services can share one.

Jump to

Keyboard shortcuts

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