Documentation
¶
Overview ¶
Package quota checks and consumes tenant quotas with concurrency-safe in-memory storage.
包 quota 使用并发安全的内存存储检查并消耗租户配额。
Index ¶
- Constants
- Variables
- type Limit
- type MemoryStore
- func (store *MemoryStore) Add(ctx context.Context, tenantID types.TenantID, resource string, period Period, ...) (int64, error)
- func (store *MemoryStore) Consume(ctx context.Context, limit Limit, amount int64) (int64, error)
- func (store *MemoryStore) Get(ctx context.Context, tenantID types.TenantID, resource string, period Period) (int64, error)
- func (store *MemoryStore) Reset(ctx context.Context, tenantID types.TenantID, resource string, period Period) error
- type Period
- type SQLDialect
- type SQLStore
- func (store *SQLStore) Add(ctx context.Context, tenantID types.TenantID, resource string, period Period, ...) (int64, error)
- func (store *SQLStore) Consume(ctx context.Context, limit Limit, amount int64) (int64, error)
- func (store *SQLStore) Get(ctx context.Context, tenantID types.TenantID, resource string, period Period) (int64, error)
- func (store *SQLStore) Reset(ctx context.Context, tenantID types.TenantID, resource string, period Period) error
- type SQLStoreOption
- type Service
- func (service *Service) Check(ctx context.Context, limit Limit, amount int64) (Usage, error)
- func (service *Service) Consume(ctx context.Context, limit Limit, amount int64) (Usage, error)
- func (service *Service) Reset(ctx context.Context, tenantID types.TenantID, resource string, period Period) error
- type Store
- type Usage
Constants ¶
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 )
const (
// DefaultSQLTableName is the default quota usage table name.
DefaultSQLTableName = "saas_quota_usage"
)
Variables ¶
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 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.
type SQLDialect ¶
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.
type SQLStoreOption ¶
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.
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.