util

package
v0.0.0-...-2b2087e Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2014 License: Apache-2.0 Imports: 13 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 ContainsSameElements

func ContainsSameElements(s1, s2 interface{}) bool

ContainsSameElements compares, without taking order on the first level, the contents of two slices of the same type. The elements of the slice must have comparisons defined, otherwise a panic will result.

func CreateTestAddr

func CreateTestAddr(network string) net.Addr

CreateTestAddr creates an unused address for testing. The "network" parameter should be one of "tcp" or "unix".

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 ErrorSkipFrames

func ErrorSkipFrames(skip int, a ...interface{}) error

ErrorSkipFrames allows the skip count for stack frames to be specified. See the comments for ErrorfSkip.

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 ErrorfSkipFrames

func ErrorfSkipFrames(skip int, format string, a ...interface{}) error

ErrorfSkipFrames allows the skip count for stack frames to be specified. This is useful when generating errors via helper methods. Skip should be specified as the number of additional stack frames between the location at which the error is caused and the location at which the error is generated.

func IsTrueWithin

func IsTrueWithin(trueFunc func() bool, duration time.Duration) error

IsTrueWithin returns an error if the supplied function fails to evaluate to true within the specified duration. The function is invoked at most 50 times over the course of the specified time duration.

func MapKeys

func MapKeys(m interface{}) interface{}

MapKeys takes a map[KeyType]ValueType and returns a slice []KeyType containing the keys of the input map as an interface{} which may be manipulated freely. The implementation relies on reflection and internally requires an extra array, and thus should not be used where a) large maps are expected or b) performance is critical. Example usage: keys := util.MapKeys(map[string]struct{}{"a": {}}).([]string)

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(opts RetryOptions, fn func() (bool, error)) error

RetryWithBackoff implements retry with exponential backoff using the supplied options as parameters. When fn returns false and the number of retry attempts haven't been exhausted, fn is retried. When fn returns true, retry ends. Returns an error if the maximum number of retries is exceeded or if the fn returns an error.

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 Ordered

type Ordered interface {
	// Returns true if the supplied Ordered value
	// is less than this object.
	Less(b Ordered) bool
}

Ordered values can be compared against each other.

type RetryOptions

type RetryOptions struct {
	Tag         string        // Tag for helpful logging of backoffs
	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)
}

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

type Retryable

type Retryable interface {
	CanRetry() bool
}

Retryable is an interface for conditions which may be retried.

Jump to

Keyboard shortcuts

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