quota

package
v0.0.0-...-5facc9d Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2013 License: BSD-3-Clause Imports: 6 Imported by: 0

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

View Source
var (
	ErrQuotaAlreadyExists = errors.New("Quota already exists")
	ErrQuotaNotFound      = errors.New("Quota not found")
)

Functions

func Create

func Create(owner string, quota uint) error

Create stores a new quota in the database.

func Delete

func Delete(owner string) error

Delete destroys the quota allocated for the owner.

func Items

func Items(owner string) ([]string, uint, error)

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

func Release(owner string, items ...string) error

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

func Reserve(owner string, items ...string) error

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

func Set(owner string, value uint) error

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

type QuotaExceededError struct {
	Requested uint
	Available uint
}

func (*QuotaExceededError) Error

func (err *QuotaExceededError) Error() string

Jump to

Keyboard shortcuts

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