Documentation
¶
Index ¶
- Variables
- type CostReporter
- type Currency
- type Event
- type Ledger
- func (l *Ledger) Currency() Currency
- func (l *Ledger) Entries() []Event
- func (l *Ledger) LimitMicros() int64
- func (l *Ledger) Record(ctx context.Context, event Event) error
- func (l *Ledger) Reserve(ctx context.Context, estimateMicros int64) (Reservation, error)
- func (l *Ledger) SpentMicros() int64
- func (l *Ledger) UsedMicros() int64
- type Micro
- type Reservation
- type Reserver
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
var ( USD = Currency{'U', 'S', 'D'} EUR = Currency{'E', 'U', 'R'} GBP = Currency{'G', 'B', 'P'} PLN = Currency{'P', 'L', 'N'} TOK = Currency{'T', 'O', 'K'} )
var ( ErrNilContext = errors.New("cost: nil context") ErrBudgetExceeded = errors.New("cost: budget exceeded") ErrValidation = errors.New("cost: validation error") ErrInvalidEstimate = errors.New("cost: invalid estimate") ErrInvalidCost = errors.New("cost: invalid cost") ErrReservationNotFound = errors.New("cost: reservation not found or expired") )
Functions ¶
This section is empty.
Types ¶
type CostReporter ¶
type CostReporter interface {
CostMicros() int64
}
CostReporter lets an action result report actual usage.
type Currency ¶
type Currency [3]byte
Currency is a 3-byte ISO-4217 code (USD, EUR) or custom credit code (TOK). Packed into 3 bytes for register-passing and zero heap allocations.
func (Currency) MarshalJSON ¶
func (Currency) MarshalText ¶
func (*Currency) UnmarshalJSON ¶
func (*Currency) UnmarshalText ¶
type Event ¶
type Event struct {
ID string `json:"id,omitempty"`
Domain string `json:"domain"`
Operation string `json:"operation"`
CostMicros int64 `json:"cost_micros"`
Currency Currency `json:"currency"`
Timestamp time.Time `json:"timestamp,omitempty"`
}
Event is an immutable accounting/audit record. ID should be stable across retries.
type Ledger ¶
type Ledger struct {
// contains filtered or unexported fields
}
Ledger is the bounded-memory, in-process implementation. Reserve/commit/ release use atomics; only audit snapshots take a short read lock.
func (*Ledger) LimitMicros ¶
func (*Ledger) SpentMicros ¶
func (*Ledger) UsedMicros ¶
type Micro ¶
type Micro int64
Micro represents fixed-point 1/1,000,000th of a currency unit ($0.000001).
type Reservation ¶
type Reservation interface {
Commit(ctx context.Context, actualMicros int64) error
Release(ctx context.Context) error
}
Reservation is a terminal, idempotent lease. Terminal operations return backend failures; callers must not claim success when an error is returned.
type Reserver ¶
type Reserver interface {
Reserve(ctx context.Context, estimateMicros int64) (Reservation, error)
Record(ctx context.Context, event Event) error
}
Reserver is the backend-neutral budget contract.
type ValidationError ¶
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error