quota

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCollectionRequired = fmt.Errorf("quota collection not specified")
	ErrQuotaExceeded      = fmt.Errorf("quota exceeded")
	ErrInvalidTableName   = fmt.Errorf("invalid table name")
)

Functions

This section is empty.

Types

type Cycle

type Cycle string

Cycle represent the quota refresh cycle

const (
	None  Cycle = ""
	Day   Cycle = "day"
	Month Cycle = "month"
	Year  Cycle = "year"
	Hour  Cycle = "hour"
	// minute is not supported to use, it may cause performance issue
	Minute Cycle = "minute"
	// second is not supported to use, it may cause performance issue
	Second Cycle = "second"
)

func (Cycle) Current

func (c Cycle) Current() string

Current return the current quota cycle identity in string

type Manager

type Manager interface {
	Acquire(ctx context.Context, tx *sql.Tx, account, region, resource string, cycle Cycle, max, n int64) error
	Release(ctx context.Context, tx *sql.Tx, account, region, resource string, cycle Cycle, capacity, n int64) error
	Set(ctx context.Context, tx *sql.Tx, account, region, resource string, cycle Cycle, capacity, used int64) error
	List(ctx context.Context, account, region string, cycle Cycle, resource []string) ([]*Quota, error)
}

func NewManagerFromDB

func NewManagerFromDB(db *sql.DB, table string) (Manager, error)

type Quota

type Quota struct {
	Account string `bson:"account,omitempty" form:"Account" json:"Account,omitempty" query:"Account"`
	// 资源名称
	Resource string `bson:"resource,omitempty" form:"Resource" json:"Resource,omitempty" query:"Resource"`
	// 资源所属 region
	Region string `bson:"region,omitempty" form:"Region" json:"Region,omitempty" query:"Region"`
	// 资源刷新周期
	Cycle string `bson:"cycle,omitempty" form:"Cycle" json:"Cycle,omitempty" query:"Cycle"`
	// 资源总数量
	Capacity int64 `bson:"capacity,omitempty" form:"Capacity" json:"Capacity,omitempty" query:"Capacity"`
	// 资源已使用数量
	Used int64 `bson:"used,omitempty" form:"Used" json:"Used,omitempty" query:"Used"`
}

Quota represents the resource quota data.

The corresponding table can be created with the following SQL:

CREATE TABLE `quota` (

`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`account`  VARCHAR(32)  NOT NULL DEFAULT '' COMMENT 'account identifier',
`region`   VARCHAR(32)  NOT NULL DEFAULT '' COMMENT 'region the resource belongs to',
`resource` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'resource name',
`cycle`    VARCHAR(8)  NOT NULL DEFAULT '' COMMENT 'current cycle identity, e.g. 20060102',
`capacity` BIGINT       NOT NULL DEFAULT 0  COMMENT 'total capacity of the resource',
`used`     BIGINT       NOT NULL DEFAULT 0  COMMENT 'used amount of the resource',
`created_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'created time',
`updated_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'updated time',
PRIMARY KEY (`account`, `region`, `resource`, `cycle`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='resource quota records';

Jump to

Keyboard shortcuts

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