quota

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package quota checks and consumes tenant quotas with concurrency-safe in-memory storage.

包 quota 使用并发安全的内存存储检查并消耗租户配额。

Index

Constants

View Source
const (
	// SQLDialectMySQL uses question-mark placeholders and is the default.
	SQLDialectMySQL = sqlutil.DialectMySQL

	// SQLDialectSQLite uses question-mark placeholders.
	SQLDialectSQLite = sqlutil.DialectSQLite

	// SQLDialectPostgres uses numbered placeholders.
	SQLDialectPostgres = sqlutil.DialectPostgres
)
View Source
const (
	// DefaultSQLTableName is the default quota usage table name.
	DefaultSQLTableName = "saas_quota_usage"
)

Variables

View Source
var (
	// ErrInvalidQuota reports invalid quota input.
	ErrInvalidQuota = errors.New("saas/quota: invalid quota")

	// ErrQuotaExceeded reports a quota limit violation.
	ErrQuotaExceeded = errors.New("saas/quota: quota exceeded")

	// ErrNilStore reports that a quota service was created with a nil store.
	ErrNilStore = errors.New("saas/quota: nil store")

	// ErrNilDB reports that a SQL store was created with a nil database handle.
	ErrNilDB = errors.New("saas/quota: nil db")

	// ErrInvalidTableName reports an unsafe SQL table name.
	ErrInvalidTableName = errors.New("saas/quota: invalid table name")

	// ErrUnsupportedSQLDialect reports an unsupported SQLStore dialect.
	ErrUnsupportedSQLDialect = errors.New("saas/quota: unsupported sql dialect")
)

Functions

This section is empty.

Types

type Limit

type Limit struct {
	TenantID types.TenantID
	Resource string
	Limit    int64
	Period   Period
}

Limit describes a tenant resource limit.

type MemoryStore

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

MemoryStore tracks usage in memory.

func NewMemoryStore

func NewMemoryStore() *MemoryStore

NewMemoryStore creates an empty usage store.

func (*MemoryStore) Add

func (store *MemoryStore) Add(ctx context.Context, tenantID types.TenantID, resource string, period Period, amount int64) (int64, error)

Add increments usage.

func (*MemoryStore) Consume

func (store *MemoryStore) Consume(ctx context.Context, limit Limit, amount int64) (int64, error)

Consume increments usage only when the configured limit allows it.

func (*MemoryStore) Get

func (store *MemoryStore) Get(ctx context.Context, tenantID types.TenantID, resource string, period Period) (int64, error)

Get returns current usage.

func (*MemoryStore) Reset

func (store *MemoryStore) Reset(ctx context.Context, tenantID types.TenantID, resource string, period Period) error

Reset clears usage.

type Period

type Period string

Period describes quota reset cadence.

const (
	PeriodNone  Period = "none"
	PeriodDay   Period = "day"
	PeriodMonth Period = "month"
)

type SQLDialect

type SQLDialect = sqlutil.Dialect

SQLDialect controls SQL placeholder rendering for SQLStore.

type SQLStore

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

SQLStore tracks quota usage through database/sql.

The table is expected to contain these columns: tenant_id, resource, period, used. Use a unique key on (tenant_id, resource, period).

func NewSQLStore

func NewSQLStore(db *sql.DB, opts ...SQLStoreOption) (*SQLStore, error)

NewSQLStore creates a SQL-backed quota store.

func (*SQLStore) Add

func (store *SQLStore) Add(ctx context.Context, tenantID types.TenantID, resource string, period Period, amount int64) (int64, error)

Add increments usage.

func (*SQLStore) Consume

func (store *SQLStore) Consume(ctx context.Context, limit Limit, amount int64) (int64, error)

Consume increments usage only when the configured limit allows it.

func (*SQLStore) Get

func (store *SQLStore) Get(ctx context.Context, tenantID types.TenantID, resource string, period Period) (int64, error)

Get returns current usage.

func (*SQLStore) Reset

func (store *SQLStore) Reset(ctx context.Context, tenantID types.TenantID, resource string, period Period) error

Reset clears usage.

type SQLStoreOption

type SQLStoreOption func(*SQLStore) error

SQLStoreOption configures SQLStore.

func WithSQLDialect

func WithSQLDialect(dialect SQLDialect) SQLStoreOption

WithSQLDialect configures SQL placeholder rendering.

func WithTableName

func WithTableName(table string) SQLStoreOption

WithTableName overrides the default quota usage table name.

type Service

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

Service checks and consumes tenant quotas.

func NewService

func NewService(store Store) *Service

NewService creates a quota service.

func (*Service) Check

func (service *Service) Check(ctx context.Context, limit Limit, amount int64) (Usage, error)

Check returns current usage and whether amount can be consumed.

func (*Service) Consume

func (service *Service) Consume(ctx context.Context, limit Limit, amount int64) (Usage, error)

Consume increments usage when the limit allows it.

func (*Service) Reset

func (service *Service) Reset(ctx context.Context, tenantID types.TenantID, resource string, period Period) error

Reset clears usage for a tenant resource.

type Store

type Store interface {
	Add(ctx context.Context, tenantID types.TenantID, resource string, period Period, amount int64) (int64, error)
	Consume(ctx context.Context, limit Limit, amount int64) (int64, error)
	Get(ctx context.Context, tenantID types.TenantID, resource string, period Period) (int64, error)
	Reset(ctx context.Context, tenantID types.TenantID, resource string, period Period) error
}

Store tracks quota usage.

type Usage

type Usage struct {
	TenantID types.TenantID
	Resource string
	Period   Period
	Used     int64
	Limit    int64
}

Usage describes current resource usage.

Jump to

Keyboard shortcuts

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