util

package
v0.0.0-...-024101a Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2015 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ContentTypeHeader is the canonical header name for content type.
	ContentTypeHeader = "Content-Type"
	// AcceptHeader is the canonical header name for accept.
	AcceptHeader = "Accept"
	// JSONContentType is the JSON content type.
	JSONContentType = "application/json"
	// AltJSONContentType is the alternate JSON content type.
	AltJSONContentType = "application/x-json"
	// ProtoContentType is the protobuf content type.
	ProtoContentType = "application/x-protobuf"
	// AltProtoContentType is the alternate protobuf content type.
	AltProtoContentType = "application/x-google-protobuf"
	// YAMLContentType is the YAML content type.
	YAMLContentType = "text/yaml"
	// AltYAMLContentType is the alternate YAML content type.
	AltYAMLContentType = "application/x-yaml"
)

Variables

AllEncodings includes all supported encodings.

Functions

func CreateTempDirectory

func CreateTempDirectory() string

CreateTempDirectory creates a temporary directory or fails trying.

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 GetContentType

func GetContentType(request *http.Request) string

GetContentType pulls out the content type from a request header it ignores every value after the first semicolon

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 10 times over the course of the specified time duration.

func MarshalResponse

func MarshalResponse(r *http.Request, value interface{}, allowed []EncodingType) (
	body []byte, contentType string, err error)

MarshalResponse examines the request Accept header to determine the client's preferred response encoding. Supported content types include JSON, protobuf, and YAML. If the Accept header is not available, the Content-Type header specifying the request encoding is used. The value parameter is marshalled using the response encoding and the resulting body and content type are returned. If the encoding could not be determined by either header, the response is marshalled using JSON. An error is returned on marshalling failure.

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. The created object is not safe for concurrent access.

func NewPseudoSeed

func NewPseudoSeed() int64

NewPseudoSeed generates a seed from crypto/rand.

func RandBytes

func RandBytes(r *rand.Rand, size int) []byte

RandBytes returns a byte slice of the given length with random data.

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() (RetryStatus, error)) error

RetryWithBackoff implements retry with exponential backoff using the supplied options as parameters. When fn returns RetryContinue and the number of retry attempts haven't been exhausted, fn is retried. When fn returns RetryBreak, retry ends. As a special case, if fn returns RetryReset, the backoff and retry count are reset to starting values and the next retry occurs immediately. Returns an error if the maximum number of retries is exceeded or if the fn returns an error.

func UnmarshalRequest

func UnmarshalRequest(r *http.Request, body []byte, value interface{}, allowed []EncodingType) error

UnmarshalRequest examines the request Content-Type header in order to determine the encoding of the supplied body. Supported content types include:

JSON     - {"application/json", "application/x-json"}
Protobuf - {"application/x-protobuf", "application/x-google-protobuf"}
YAML     - {"text/yaml", "application/x-yaml"}

The body is unmarshalled into the supplied value parameter. An error is returned on an unmarshalling error or on an unsupported content type.

Types

type BuildInfo

type BuildInfo struct {
	Vers string `json:"goVersion"`
	Tag  string `json:"tag"`
	Time string `json:"time"`
	Deps string `json:"dependencies"`
}

BuildInfo ...

func GetBuildInfo

func GetBuildInfo() BuildInfo

GetBuildInfo ...

type CacheConfig

type CacheConfig struct {
	// Policy is one of the consts listed for EvictionPolicy.
	Policy EvictionPolicy

	// ShouldEvict is a callback function executed each time a new entry
	// is added to the cache. It supplies cache size, and potential
	// evictee's key and value. The function should return true if the
	// entry may be evicted; false otherwise. For example, to support a
	// maximum size for the cache, use a method like:
	//
	//   func(size int, key Key, value interface{}) { return size > maxSize }
	//
	// To support max TTL in the cache, use something like:
	//
	//   func(size int, key Key, value interface{}) {
	//     return time.Now().UnixNano() - value.(int64) > maxTTLNanos
	//   }
	ShouldEvict func(size int, key, value interface{}) bool

	// OnEvicted optionally specifies a callback function to be
	// executed when an entry is purged from the cache.
	OnEvicted func(key, value interface{})
}

A CacheConfig specifies the eviction policy, eviction trigger callback, and eviction listener callback.

type EncodingType

type EncodingType int

EncodingType is an enum describing available encodings.

const (
	// JSONEncoding includes application/json and application/x-json.
	JSONEncoding EncodingType = iota
	// ProtoEncoding includes application/x-protobuf and application/x-google-protobuf.
	ProtoEncoding
	// YAMLEncoding includes text/yaml and application/x-yaml.
	YAMLEncoding
)

type EvictionPolicy

type EvictionPolicy int

EvictionPolicy is the cache eviction policy enum.

const (
	CacheLRU  EvictionPolicy = iota // Least recently used
	CacheFIFO                       // First in, first out
	CacheNone                       // No evictions; don't maintain ordering list
)

Constants describing LRU and FIFO, and None cache eviction policies respectively.

type IntervalCache

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

IntervalCache is a cache which supports querying of intervals which match a key or range of keys. It is backed by an interval tree. See comments in UnorderedCache for more details on cache functionality.

Note that the IntervalCache allow multiple identical segments, as specified by start and end keys.

Keys supplied to the IntervalCache's Get, Add & Del methods must be constructed from IntervalCache.NewKey().

IntervalCache is not safe for concurrent access.

func NewIntervalCache

func NewIntervalCache(config CacheConfig) *IntervalCache

NewIntervalCache creates a new Cache backed by an interval tree. See NewCache() for details on parameters.

func (IntervalCache) Add

func (bc IntervalCache) Add(key, value interface{})

Add adds a value to the cache.

func (IntervalCache) Clear

func (bc IntervalCache) Clear()

Clear clears all entries from the cache.

func (IntervalCache) Del

func (bc IntervalCache) Del(key interface{})

Del removes the provided key from the cache.

func (IntervalCache) Get

func (bc IntervalCache) Get(key interface{}) (value interface{}, ok bool)

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

func (*IntervalCache) GetOverlaps

func (ic *IntervalCache) GetOverlaps(start, end interval.Comparable) []Overlap

GetOverlaps returns a slice of values which overlap the specified interval.

func (IntervalCache) Len

func (bc IntervalCache) Len() int

Len returns the number of items in the cache.

func (*IntervalCache) NewKey

func (ic *IntervalCache) NewKey(start, end interval.Comparable) *IntervalKey

NewKey creates a new interval key defined by start and end values.

type IntervalKey

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

func (*IntervalKey) Contains

func (ik *IntervalKey) Contains(lk *IntervalKey) bool

Contains returns true if the specified IntervalKey is contained within this IntervalKey.

func (*IntervalKey) End

func (ik *IntervalKey) End() interval.Comparable

func (*IntervalKey) Overlap

func (ik *IntervalKey) Overlap(r interval.Range) bool

func (*IntervalKey) SetEnd

func (ik *IntervalKey) SetEnd(c interval.Comparable)

func (*IntervalKey) SetStart

func (ik *IntervalKey) SetStart(c interval.Comparable)

func (*IntervalKey) Start

func (ik *IntervalKey) Start() interval.Comparable

Implementation of the interval.Range & interval.Mutable interfaces.

func (IntervalKey) String

func (ik IntervalKey) String() string

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 OrderedCache

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

OrderedCache is a cache which supports binary searches using Ceil and Floor methods. It is backed by a left-leaning red black tree. See comments in UnorderedCache for more details on cache functionality.

OrderedCache requires that keys implement llrb.Comparable.

OrderedCache is not safe for concurrent access.

func NewOrderedCache

func NewOrderedCache(config CacheConfig) *OrderedCache

NewOrderedCache creates a new Cache backed by a left-leaning red black binary tree which supports binary searches via the Ceil() and Floor() methods. See NewUnorderedCache() for details on parameters.

func (OrderedCache) Add

func (bc OrderedCache) Add(key, value interface{})

Add adds a value to the cache.

func (*OrderedCache) Ceil

func (oc *OrderedCache) Ceil(key interface{}) (k, v interface{}, ok bool)

Ceil returns the smallest cache entry greater than or equal to key.

func (OrderedCache) Clear

func (bc OrderedCache) Clear()

Clear clears all entries from the cache.

func (OrderedCache) Del

func (bc OrderedCache) Del(key interface{})

Del removes the provided key from the cache.

func (*OrderedCache) Floor

func (oc *OrderedCache) Floor(key interface{}) (k, v interface{}, ok bool)

Floor returns the greatest cache entry less than or equal to key.

func (OrderedCache) Get

func (bc OrderedCache) Get(key interface{}) (value interface{}, ok bool)

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

func (OrderedCache) Len

func (bc OrderedCache) Len() int

Len returns the number of items in the cache.

type Overlap

type Overlap struct {
	Key   *IntervalKey
	Value interface{}
}

Overlap contains the key/value pair for one overlap instance.

type RawAddr

type RawAddr struct {
	// These fields are only exported so that gob can see them.
	NetworkField string
	StringField  string
}

RawAddr is a super-simple implementation of net.Addr.

func MakeRawAddr

func MakeRawAddr(network string, str string) RawAddr

MakeRawAddr creates a new RawAddr from a network and raw address string.

func (RawAddr) Network

func (a RawAddr) Network() string

Network returns the address's network name.

func (RawAddr) String

func (a RawAddr) String() string

String returns the address's string form.

type RetryMaxAttemptsError

type RetryMaxAttemptsError struct {
	MaxAttempts int
}

RetryMaxAttemptsError indicates max attempts were exceeded.

func (*RetryMaxAttemptsError) Error

func (re *RetryMaxAttemptsError) Error() string

Error implements error interface.

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)
	UseV1Info   bool          // Use verbose V(1) level for log messages
}

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

type RetryStatus

type RetryStatus int32

RetryStatus is an enum describing the possible statuses of a backoff / retry worker function.

const (
	// RetryBreak indicates the retry loop is finished and should return
	// the result of the retry worker function.
	RetryBreak RetryStatus = iota
	// RetryReset indicates that the retry loop should be reset with
	// no backoff for an immediate retry.
	RetryReset
	// RetryContinue indicates that the retry loop should continue with
	// another iteration of backoff / retry.
	RetryContinue
)

type Retryable

type Retryable interface {
	CanRetry() bool
}

Retryable is an interface for conditions which may be retried.

type Stopper

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

A Stopper provides a channel-based mechanism to stop a running goroutine. Stopping occurs in two phases: the first is the request to stop. The second is the confirmation by the goroutine that it has stopped. Multiple goroutines can be stopped using the same Stopper instance.

func NewStopper

func NewStopper(count int) *Stopper

NewStopper returns an instance of Stopper. Count specifies how many goroutines this stopper will stop.

func (*Stopper) Add

func (s *Stopper) Add(count int)

Add a new goroutine to the stopper.

func (*Stopper) SetStopped

func (s *Stopper) SetStopped()

SetStopped should be called after the ShouldStop() channel has been closed to confirm the goroutine has stopped.

func (*Stopper) ShouldStop

func (s *Stopper) ShouldStop() <-chan struct{}

ShouldStop returns a channel which will be closed when Stop() has been invoked. SetStopped() should be called to confirm.

func (*Stopper) Stop

func (s *Stopper) Stop()

Stop signals the waiting goroutine to stop and then waits for it to confirm it has stopped (it does this by calling SetStopped).

type UnorderedCache

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

UnorderedCache is a cache which supports custom eviction triggers and two eviction policies: LRU and FIFO. A listener pattern is available for eviction events. This cache uses a hashmap for storing elements, making it the most performant. Only exact lookups are supported.

UnorderedCache requires that keys are comparable, according to the go specification (http://golang.org/ref/spec#Comparison_operators).

UnorderedCache is not safe for concurrent access.

func NewUnorderedCache

func NewUnorderedCache(config CacheConfig) *UnorderedCache

NewUnorderedCache creates a new UnorderedCache backed by a hash map.

func (UnorderedCache) Add

func (bc UnorderedCache) Add(key, value interface{})

Add adds a value to the cache.

func (UnorderedCache) Clear

func (bc UnorderedCache) Clear()

Clear clears all entries from the cache.

func (UnorderedCache) Del

func (bc UnorderedCache) Del(key interface{})

Del removes the provided key from the cache.

func (UnorderedCache) Get

func (bc UnorderedCache) Get(key interface{}) (value interface{}, ok bool)

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

func (UnorderedCache) Len

func (bc UnorderedCache) Len() int

Len returns the number of items in the cache.

type WeightedReservoirSample

type WeightedReservoirSample struct {
	Heap heap.Interface
	// contains filtered or unexported fields
}

A WeightedReservoirSample implements the weighted reservoir sampling algorithm as proposed by Efraimidis-Spirakis (2005).

func NewWeightedReservoirSample

func NewWeightedReservoirSample(size int, minHeap heap.Interface) *WeightedReservoirSample

NewWeightedReservoirSample creates a new reservoir sample on the given heap. If minHeap is nil, an in-memory heap is created and used.

func (*WeightedReservoirSample) Consider

func (rs *WeightedReservoirSample) Consider(value interface{})

Consider offers a new value to the underlying reservoir using weight one. Using the same weight for all values considered throughout the lifetime of a sample is equivalent to a non-weighted reservoir sampling algorithm.

func (*WeightedReservoirSample) ConsiderWeighted

func (rs *WeightedReservoirSample) ConsiderWeighted(value interface{}, weight float64)

ConsiderWeighted lets the sample inspect a new value with a positive given weight. A weight of one corresponds to the unweighted reservoir sampling algorithm. A nonpositive weight will lead to the item being rejected without having been observed. To avoid numerical instabilities, it is advisable to stay away from zero and infinity, or more generally from regions in which computing x**1/weight may be ill-behaved.

type WeightedValue

type WeightedValue struct {
	Value interface{}
	// contains filtered or unexported fields
}

A WeightedValue is used to represent the items sampled by WeightedReservoirSample.

func (WeightedValue) Less

func (wv WeightedValue) Less(zv WeightedValue) bool

Less implements the Ordered interface.

type WeightedValueHeap

type WeightedValueHeap []WeightedValue

A WeightedValueHeap implements a heap structure on a slice of weighted values for use in in-memory weighted reservoir sampling.

func (WeightedValueHeap) Len

func (h WeightedValueHeap) Len() int

Len, Less and Swap implement sort.Interface.

func (WeightedValueHeap) Less

func (h WeightedValueHeap) Less(i, j int) bool

func (*WeightedValueHeap) Pop

func (h *WeightedValueHeap) Pop() interface{}

Pop removes the last element of the slice.

func (*WeightedValueHeap) Push

func (h *WeightedValueHeap) Push(x interface{})

Push appends an element to the slice.

func (WeightedValueHeap) Swap

func (h WeightedValueHeap) Swap(i, j int)

Directories

Path Synopsis
Package hlc implements the Hybrid Logical Clock outlined in "Logical Physical Clocks and Consistent Snapshots in Globally Distributed Databases", available online at http://www.cse.buffalo.edu/tech-reports/2014-04.pdf.
Package hlc implements the Hybrid Logical Clock outlined in "Logical Physical Clocks and Consistent Snapshots in Globally Distributed Databases", available online at http://www.cse.buffalo.edu/tech-reports/2014-04.pdf.
Package leaktest provides tools to detect leaked goroutines in tests.
Package leaktest provides tools to detect leaked goroutines in tests.

Jump to

Keyboard shortcuts

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