Documentation ¶
Overview ¶
Package quota implements per-user/app quota management.
It has a Usage type, that is used to manage generic quotas, and functions and methods to interact with the Usage type.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrQuotaAlreadyExists = errors.New("Quota already exists") ErrQuotaNotFound = errors.New("Quota not found") )
Functions ¶
func Items ¶
Items returns a slice containing all items allocated to the given owner, and an unsigned integer indicating how many items are still available.
func Release ¶
Release releases the given items from the owner.
It returns an error when the given owner does not exist, but returns nil when any of the given items do not not belong to the owner.
func Reserve ¶
Reserve reserves the given items to the owner.
It may allocate part of the items before exceeding the quota. See the example for more details.
Example ¶
package main import ( "github.com/globocom/tsuru/quota" ) func main() { err := quota.Create("me@tsuru.io", 4) if err != nil { panic(err) } quota.Reserve("me@tsuru.io", "me/0", "me/1", "me/2") // ok quota.Reserve("me@tsuru.io", "me/3", "me/4", "me/5") // ErrQuotaExceeded }
Output:
func Set ¶
Set defines a new value for the quota of the given owner.
It allows the database to become in an inconsistent state: a owner may be able to have 8 items, and a limit of 7. See the example for more details.
Example ¶
package main import ( "github.com/globocom/tsuru/quota" ) func main() { err := quota.Create("me@tsuru.io", 3) if err != nil { panic(err) } quota.Reserve("me@tsuru.io", "me/0") quota.Reserve("me@tsuru.io", "me/1") quota.Reserve("me@tsuru.io", "me/2") quota.Set("me@tsuru.io", 2) quota.Reserve("me@tsuru.io", "me/3") // ErrQuotaExceeded quota.Release("me@tsuru.io", "me/2") quota.Reserve("me@tsuru.io", "me/3") // ErrQuotaExceeded quota.Release("me@tsuru.io", "me/1") quota.Reserve("me@tsuru.io", "me/3") // Everything is ok now }
Output:
Types ¶
type QuotaExceededError ¶
func (*QuotaExceededError) Error ¶
func (err *QuotaExceededError) Error() string