usage

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 5 Imported by: 1 module, 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 usage meters bounded consumption per owner and period.

It is not a billing ledger or Stripe meter. Callers choose period keys (e.g. "2026-09") and limits. Exceeding a limit returns ErrDenied.

Status: CANDIDATE. Capability: billing.usage-meter.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalid  = errors.New("usage: invalid input")
	ErrNotFound = errors.New("usage: not found")
	ErrDenied   = errors.New("usage: limit exceeded")
	ErrConflict = errors.New("usage: conflict")
)

Functions

This section is empty.

Types

type Counter

type Counter struct {
	Key
	Used      int64
	UpdatedAt time.Time
	Epoch     uint64
}

Counter is durable usage state.

type Key

type Key struct {
	Owner  string // account or tenant id
	Meter  string // e.g. "model_steps"
	Period string // caller-defined bucket, e.g. "2026-09"
}

Key identifies one counter.

type Service

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

Service increments and checks quotas.

func New

func New(store Store) (*Service, error)

New constructs a Service.

func (*Service) Check

func (s *Service) Check(ctx context.Context, key Key) (int64, error)

Check returns the current used amount without mutating.

func (*Service) Consume

func (s *Service) Consume(ctx context.Context, key Key, n, limit int64) (Counter, error)

Consume adds n to the counter if used+n <= limit. limit must be > 0.

type Store

type Store interface {
	Lookup(context.Context, Key) (Counter, error)
	Apply(ctx context.Context, expectedEpoch uint64, next Counter) error
}

Store persists counters with CAS on Epoch.

Directories

Path Synopsis
Package sqlstore persists usage counters via database/sql.
Package sqlstore persists usage counters via database/sql.

Jump to

Keyboard shortcuts

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