util

package
v0.0.0-...-f27897a Latest Latest
Warning

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

Go to latest
Published: May 27, 2014 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CachedRand = NewPseudoRand()

CachedRand is a global singleton rand.Rand object cached for one-off purposes to generate random numbers.

Functions

func Error

func Error(a ...interface{}) error

Error is a passthrough to fmt.Error, with an additional prefix containing the filename and line number.

func Errorf

func Errorf(format string, a ...interface{}) error

Errorf is a passthrough to fmt.Errorf, with an additional prefix containing the filename and line number.

func NewPseudoRand

func NewPseudoRand() *rand.Rand

NewPseudoRand returns an instance of math/rand.Rand seeded from crypto/rand so we can easily and cheaply generate unique streams of numbers.

func RandIntInRange

func RandIntInRange(r *rand.Rand, min, max int) int

RandIntInRange returns a value in [min, max)

func RetryWithBackoff

func RetryWithBackoff(fn func() bool) error

RetryWithBackoff is uses default retry constants to implement an exponential backoff. When fn returns true, retry ends. Returns an error if the maximum number of retries is exceeded.

func RetryWithBackoffOptions

func RetryWithBackoffOptions(opts Options, fn func() bool) error

RetryWithBackoffOptions implements retry with exponential backoff using the supplied options as parameters.

Types

type Key

type Key interface{}

A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators

type LRUCache

type LRUCache struct {
	// MaxEntries is the maximum number of cache entries before
	// an item is evicted. Zero means no limit.
	MaxEntries int

	// OnEvicted optionally specificies a callback function to be
	// executed when an entry is purged from the cache.
	OnEvicted func(key Key, value interface{})
	// contains filtered or unexported fields
}

LRUCache is an LRU cache. It is not safe for concurrent access.

func NewLRUCache

func NewLRUCache(maxEntries int) *LRUCache

NewLRUCache creates a new LRUCache. If maxEntries is zero, the cache has no limit and it's assumed that eviction is done by the caller.

func (*LRUCache) Add

func (c *LRUCache) Add(key Key, value interface{})

Add adds a value to the cache.

func (*LRUCache) Get

func (c *LRUCache) Get(key Key) (value interface{}, ok bool)

Get looks up a key's value from the cache.

func (*LRUCache) Len

func (c *LRUCache) Len() int

Len returns the number of items in the cache.

func (*LRUCache) Remove

func (c *LRUCache) Remove(key Key)

Remove removes the provided key from the cache.

func (*LRUCache) RemoveOldest

func (c *LRUCache) RemoveOldest()

RemoveOldest removes the oldest item from the cache.

type Options

type Options struct {
	Backoff     time.Duration // Default retry backoff interval
	MaxBackoff  time.Duration // Maximum retry backoff interval
	Constant    float64       // Default backoff constant
	MaxAttempts int           // Maximum number of attempts (0 for infinite)
}

Options provides control of retry loop logic via the RetryWithBackoffOptions method.

Jump to

Keyboard shortcuts

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