Documentation
¶
Index ¶
- Constants
- func Period(now time.Time) string
- type ConsumeOpts
- type ConsumeResult
- type GrantIssued
- type Limits
- type QuotaHooks
- type ReleaseResult
- type Reservation
- type ReservationBrief
- type ReserveOpts
- type ReserveResult
- type Service
- func (s *Service) ActiveReservations(ctx context.Context, workspaceID, period string) (map[string][]ReservationBrief, error)
- func (s *Service) Check(ctx context.Context, workspaceID, key string, now time.Time) (Status, error)
- func (s *Service) Consume(ctx context.Context, workspaceID, key string, n int64, now time.Time) error
- func (s *Service) ConsumeWith(ctx context.Context, workspaceID, key string, n int64, now time.Time, ...) (ConsumeResult, error)
- func (s *Service) ExpireDueReservations(ctx context.Context, now time.Time, limit int) ([]Reservation, error)
- func (s *Service) Grant(ctx context.Context, workspaceID, key, reason, refID string, amount int, ...) (bool, error)
- func (s *Service) GrantPeriodic(ctx context.Context, workspaceID, key, reason, refID string, amount int, ...) (bool, error)
- func (s *Service) GrantTx(ctx context.Context, tx pgx.Tx, workspaceID, key, reason, refID string, ...) (bool, error)
- func (s *Service) LimitsFor(ctx context.Context, workspaceID string) (Limits, string, error)
- func (s *Service) RefreshPeriodicGrants(ctx context.Context, now time.Time) ([]GrantIssued, error)
- func (s *Service) Release(ctx context.Context, workspaceID, key string, n int64, now time.Time) error
- func (s *Service) ReleaseReservation(ctx context.Context, reservationID string, amount int64, idemKey string) (ReleaseResult, error)
- func (s *Service) Reserve(ctx context.Context, workspaceID, key string, amount int64, idemKey string, ...) (ReserveResult, error)
- func (s *Service) Settle(ctx context.Context, reservationID string, actual int64, idemKey string) (SettleResult, error)
- func (s *Service) Usage(ctx context.Context, workspaceID, key, period string) (int64, int64, error)
- func (s *Service) WithHooks(h QuotaHooks) *Service
- type SettleResult
- type Status
Constants ¶
View Source
const ( LimitModeSoft = "soft" LimitModeHard = "hard" )
View Source
const ( CodeQuotaExhausted = "E_QUOTA_EXHAUSTED" )
View Source
const ( ReservationReserved = "reserved" ReservationSettled = "settled" ReservationReleased = "released" ReservationExpired = "expired" )
View Source
const GrantCycleMonthly = "monthly"
View Source
const ReservationTTL = 24 * time.Hour
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ConsumeOpts ¶
type ConsumeOpts struct {
IdemKey string
}
type ConsumeResult ¶
type ConsumeResult struct {
Replay bool
}
type GrantIssued ¶
type QuotaHooks ¶
type QuotaHooks struct {
OnNearLimit func(ctx context.Context, workspaceID, key string, used, limit int64) error
OnExhausted func(ctx context.Context, workspaceID, key string) error
OnGrant func(ctx context.Context, workspaceID, key, reason, refID string, amount int) error
OnReservationExpired func(ctx context.Context, reservationID, workspaceID, quotaKey string, amount int64) error
}
type ReleaseResult ¶
type Reservation ¶
type Reservation struct {
ID string `json:"id"`
WorkspaceID string `json:"workspace_id"`
QuotaKey string `json:"quota_key"`
PeriodKey string `json:"period_key"`
Amount int64 `json:"amount"`
SettledAmount int64 `json:"settled_amount"`
ReleasedAmount int64 `json:"released_amount"`
Status string `json:"status"`
ExpiresAt time.Time `json:"expires_at"`
CreatedAt time.Time `json:"created_at"`
SettledAt *time.Time `json:"settled_at,omitempty"`
ReleasedAt *time.Time `json:"released_at,omitempty"`
}
func (Reservation) Outstanding ¶
func (r Reservation) Outstanding() int64
type ReservationBrief ¶
type ReserveOpts ¶
type ReserveResult ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
func NewService ¶
func (*Service) ActiveReservations ¶
func (*Service) ConsumeWith ¶
func (s *Service) ConsumeWith(ctx context.Context, workspaceID, key string, n int64, now time.Time, opts ConsumeOpts) (ConsumeResult, error)
func (*Service) ExpireDueReservations ¶
func (*Service) GrantPeriodic ¶
func (*Service) GrantTx ¶ added in v0.9.0
func (s *Service) GrantTx(ctx context.Context, tx pgx.Tx, workspaceID, key, reason, refID string, amount int, now time.Time) (bool, error)
GrantTx runs the same idempotent insert as Grant on the CALLER's transaction (ADR 0016: provisioning grants credits inside the same transaction that inserts the personal workspace). It deliberately does NOT fire the OnGrant hook — hook timing inside a foreign transaction is not definable: the caller may still roll the grant back, and a hook side-fired here would have observed a grant that never committed. Callers that need the hook side-effect must fire it themselves after their transaction commits.
func (*Service) RefreshPeriodicGrants ¶
func (*Service) ReleaseReservation ¶
func (*Service) Reserve ¶
func (s *Service) Reserve(ctx context.Context, workspaceID, key string, amount int64, idemKey string, opts ReserveOpts) (ReserveResult, error)
func (*Service) WithHooks ¶
func (s *Service) WithHooks(h QuotaHooks) *Service
type SettleResult ¶
Click to show internal directories.
Click to hide internal directories.