storage

package
v0.0.0-...-28e9d7c Latest Latest
Warning

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

Go to latest
Published: May 13, 2017 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MemoryStorage

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

Implement an in-memory datastore with a concurrent safe map protected by a RWmutex

func (*MemoryStorage) Count

func (ms *MemoryStorage) Count(bucketName string) (int, error)

We only need a read lock here because we are only reading it.

func (*MemoryStorage) Create

func (ms *MemoryStorage) Create(name string, capacity int) error

Create an entry in the map if one does not exist for the given name. If an entry already exists return nil so that the bucket will share the entry.

func (*MemoryStorage) Ping

func (ms *MemoryStorage) Ping() error

func (*MemoryStorage) Put

func (ms *MemoryStorage) Put(bucketName string, tokens int) error

Increment the entry value by the given tokens integer

func (*MemoryStorage) Set

func (ms *MemoryStorage) Set(bucketName string, tokens int) error

func (*MemoryStorage) Take

func (ms *MemoryStorage) Take(bucketName string, tokens int) error

Decrement the entry value unless the value < tokens. If value < tokens return an error else return nil. Note that we don't check if name exists as a key of the map, this is because storage.Take is called directly from it's parent method on Bucket where the name is derived from. A bucket can't exist to call if it doesn't already have a name...

func (*MemoryStorage) TakeAll

func (ms *MemoryStorage) TakeAll(bucketName string) (int, error)

get and return the token value, set the token value to zero

type RedisStorage

type RedisStorage struct {
	Client *redis.Client
}

func (*RedisStorage) Count

func (rs *RedisStorage) Count(bucketName string) (int, error)

Return the token value of a given bucket.

func (*RedisStorage) Create

func (rs *RedisStorage) Create(name string, capacity int) error

bucket.Create will create a new bucket with the given parameters if one does not exist, if no bucket can be created it will return an error

func (*RedisStorage) Ping

func (rs *RedisStorage) Ping() error

func (*RedisStorage) Put

func (rs *RedisStorage) Put(bucketName string, tokens int) error

Increment the token value by a given amount.

func (*RedisStorage) Set

func (rs *RedisStorage) Set(bucketName string, tokens int) error

func (*RedisStorage) Take

func (rs *RedisStorage) Take(bucketName string, tokens int) error

Executes a lua script which decrements the token value by tokensDesired if tokensDesired >= the token value.

func (*RedisStorage) TakeAll

func (rs *RedisStorage) TakeAll(bucketName string) (int, error)

returns a conditional amount of tokens representing all the tokens

type Storage

type Storage interface {
	Ping() error
	Create(name string, tokens int) error
	Take(bucketName string, tokens int) error
	TakeAll(bucketName string) (int, error)
	Set(bucketName string, tokens int) error
	Put(bucketName string, tokens int) error
	Count(bucketName string) (int, error)
}

Interface for storage providers. I know the 'I' prefix isn't Golang convention but I prefer it.

Jump to

Keyboard shortcuts

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