util

package
v0.0.0-...-02ea31e Latest Latest
Warning

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

Go to latest
Published: May 30, 2015 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Examples

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"
)
View Source
const (
	// UUIDSize is the size in bytes of a UUID.
	UUIDSize = 16
)

Variables

AllEncodings includes all supported encodings.

Functions

func CleanupDir

func CleanupDir(dir string)

CleanupDir removes the passed-in directory and all contents. Errors are ignored.

func CleanupDirs

func CleanupDirs(dirs []string)

CleanupDirs removes all passed-in directories and their contents. Errors are ignored.

func CreateNTempDirs

func CreateNTempDirs(t Tester, prefix string, n int) []string

CreateNTempDirs creates N temporary directories and returns a slice of paths. You should usually call defer CleanupDirs(dirs) right after.

func CreateTempDir

func CreateTempDir(t Tester, prefix string) string

CreateTempDir creates a temporary directory and returns its path. You should usually call defer CleanupDir(dir) right after.

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 EnsureHost

func EnsureHost(addr string) string

EnsureHost takes a host:port pair, where the host portion is optional. If a host is present, the output is equal to the input. Otherwise, the output will contain a host portion equal to the hostname (or "127.0.0.1" as a fallback).

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 immediately at first and then successively with an exponential backoff starting at 1ns and ending at the specified duration.

This method is deprecated; use SucceedsWithin instead. TODO(bdarnell): convert existing uses of IsTrueWithin to SucceedsWithin.

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. Falls back to JSON when the protobuf format cannot be used for the given value.

func NewPseudoRand

func NewPseudoRand() (*rand.Rand, int64)

NewPseudoRand returns an instance of math/rand.Rand seeded from crypto/rand and its seed 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 SucceedsWithin

func SucceedsWithin(t Tester, duration time.Duration, fn func() error)

SucceedsWithin fails the test (with t.Fatal) unless the supplied function runs without error within the specified duration. The function is invoked immediately at first and then successively with an exponential backoff starting at 1ns and ending at the specified duration.

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 Closer

type Closer interface {
	Close()
}

Closer is an interface for objects to attach to the stopper to be closed once the stopper completes.

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 Feed

type Feed struct {
	sync.Mutex
	// contains filtered or unexported fields
}

A Feed is used to publish a stream of events to a set of Subscribers. Events are values of arbitrary type, and are received and published as an empty interface. Each Subscriber will receive, in order, each entry published to the Feed.

Entries are published by via the Publish method of the Feed; this cannot be blocked by Subscriber activities and will always return quickly. Subscribers receive events by reading from their Events channel.

A Feed can be initialized by simply instantiating an empty feed object:

feed := &Feed{}
subscriber := feed.Subscribe()
feed.Publish(someEvent())

The Feed does not keep historical events; individual Subscribers will only receive events published after they Subscribe. Events can be published to a Feed until its Close() method is called.

The non-blocking property of the feed is achieved by giving each Subscriber's Events channel a very large buffer. If a Subscriber does not read from its channel, then its buffer will fill; if a call to Publish() attempts to write to a full Subscriber channel, it will panic.

Example
stopper := NewStopper()
feed := Feed{}

output := make([][]string, 5)
for i := 0; i < len(output); i++ {
	sub := feed.Subscribe()
	index := i
	stopper.RunWorker(func() {
		for event := range sub.Events() {
			// events must be cast from interface{}
			output[index] = append(output[index], event.(string))
		}
	})
}

feed.Publish("Event 1")
feed.Publish("Event 2")
feed.Publish("Event 3")
feed.Close()
stopper.Stop()

<-stopper.IsStopped()
for i, out := range output {
	fmt.Printf("subscriber %d got output %v\n", i+1, out)
}
Output:

subscriber 1 got output [Event 1 Event 2 Event 3]
subscriber 2 got output [Event 1 Event 2 Event 3]
subscriber 3 got output [Event 1 Event 2 Event 3]
subscriber 4 got output [Event 1 Event 2 Event 3]
subscriber 5 got output [Event 1 Event 2 Event 3]

func (*Feed) Close

func (f *Feed) Close()

Close closes the given Feed. All existing Subscribers will be closed immediately when the Feed is closed. After closure, any new Subscribers will be closed immediately and attempts to Publish will be ignored.

func (*Feed) Publish

func (f *Feed) Publish(event interface{})

Publish publishes a event into the Feed, which will eventually be received by all Subscribers to the feed. Events published to a closed feed, or to a feed with no Subscribers, will be ignored.

func (*Feed) Subscribe

func (f *Feed) Subscribe() *Subscription

Subscribe returns a Subscription object which can immediately recieve events which were published to this feed. An event is an arbitrary interface.

Events are read from the Subscription's Events channel. Subscribers cannot block each other from receiving events, but should still attempt to consume events in a timely fashion; if a Subscriber's (very large) Events channel fills up, a panic may result.

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 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 an arbitrary array of workers. Each worker is registered with the stopper via the AddWorker() method. The system further tracks each task which is outstanding by calling StartTask() when a task is started and FinishTask() when completed.

Stopping occurs in two phases: the first is the request to stop, which moves the stopper into a draining phase. While draining, calls to StartTask() return false, meaning the system is draining and new tasks should not be accepted. When all outstanding tasks have been completed via calls to FinishTask(), the stopper closes its stopper channel, which signals all live workers that it's safe to shut down. Once shutdown, each worker invokes SetStopped(). When all workers have shutdown, the stopper is complete.

An arbitrary list of objects implementing the Closer interface may be added to the stopper via AddCloser(), to be closed after the stopper has stopped.

func NewStopper

func NewStopper() *Stopper

NewStopper returns an instance of Stopper.

func (*Stopper) AddCloser

func (s *Stopper) AddCloser(c Closer)

AddCloser adds an object to close after the stopper has been stopped.

func (*Stopper) AddWorker

func (s *Stopper) AddWorker()

AddWorker adds a worker to the stopper.

func (*Stopper) FinishTask

func (s *Stopper) FinishTask()

FinishTask removes one from the count of tasks left to drain in the system. This function must be invoked for every call to StartTask().

func (*Stopper) IsStopped

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

IsStopped returns a channel which will be closed after Stop() has been invoked to full completion, meaning all workers have completed and all closers have been closed.

func (*Stopper) Quiesce

func (s *Stopper) Quiesce()

Quiesce moves the stopper to state draining and waits until all tasks complete. This is used from Stop() and unittests.

func (*Stopper) RunWorker

func (s *Stopper) RunWorker(f func())

RunWorker runs the supplied function as a "worker" to be stopped by the stopper. The function <f> is run in a goroutine.

func (*Stopper) SetStopped

func (s *Stopper) SetStopped()

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

func (*Stopper) ShouldStop

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

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

func (*Stopper) StartTask

func (s *Stopper) StartTask() bool

StartTask adds one to the count of tasks left to drain in the system. Any worker which is a "first mover" when starting tasks must call this method before starting work on a new task and must subsequently invoke FinishTask() when the task is complete. First movers include goroutines launched to do periodic work and the kv/db.go gateway which accepts external client requests.

Returns true if the task can be launched or false to indicate the system is currently draining and the task should be refused.

func (*Stopper) Stop

func (s *Stopper) Stop()

Stop signals all live workers to stop and then waits for each to confirm it has stopped (workers do this by calling SetStopped()).

type Subscription

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

A Subscription is used to receive events from a specific Feed. A Subscription should only be instantiated via a call to a Feed's Subscribe() method. Once created, events can be read directly from the Events channel provided by this structure.

An example of a typical usage of a Subscription:

subscriber := feed.Subscribe()
for event := range subscriber.Events() {
	// Process event...
}

A Subscription cannot block other Subscriptions to the same feed, and each Subscription will receive all events published by the Feed. The user of a Subscription should not modify Events received over the channel.

A Subscription can be closed via the Unsubscribe() method, which will result in the Events channel being closed. The Events channel will also be closed if the Feed itself is closed.

func (*Subscription) Events

func (s *Subscription) Events() <-chan interface{}

Events returns a recieve only channel for reading events from this Subscriber.

func (*Subscription) Unsubscribe

func (s *Subscription) Unsubscribe()

Unsubscribe stops the Subscriber. This will close the Subscriber's Events channel; however, there may still be unprocessed Events remaining in the channel.

type Tester

type Tester interface {
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})
}

Tester is a proxy for e.g. testing.T which does not introduce a dependency on "testing".

type UUID

type UUID []byte

UUID is a 16 byte UUID.

func NewUUID4

func NewUUID4() UUID

NewUUID4 returns a new UUID (Version 4) using 16 random bytes or panics.

The uniqueness depends on the strength of crypto/rand. Version 4 UUIDs have 122 random bits.

func (UUID) String

func (u UUID) String() string

String formats as hex xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if u is invalid.

type UnresolvedAddr

type UnresolvedAddr struct {
	// These fields are only exported so that gob can see them.
	NetworkField string `json:"network"`
	StringField  string `json:"string"`
}

UnresolvedAddr is an unresolved version of net.Addr.

func MakeUnresolvedAddr

func MakeUnresolvedAddr(network string, str string) UnresolvedAddr

MakeUnresolvedAddr creates a new UnresolvedAddr from a network and raw address string.

func (UnresolvedAddr) Network

func (a UnresolvedAddr) Network() string

Network returns the address's network name.

func (UnresolvedAddr) String

func (a UnresolvedAddr) String() string

String returns the address's string form.

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.
Package log implements logging analogous to the Google-internal C++ INFO/ERROR/V setup.
Package log implements logging analogous to the Google-internal C++ INFO/ERROR/V setup.

Jump to

Keyboard shortcuts

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