leverutil

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 20, 2016 License: Apache-2.0 Imports: 17 Imported by: 80

Documentation

Overview

Package leverutil provides general utility functions that are not specific to Lever.

Index

Constants

View Source
const PackageName = "leverutil"

PackageName is the name of this package.

Variables

View Source
var (
	// ErrNotYetConstructed is returned when the cache element has not yet been
	// initialized.
	ErrNotYetConstructed = fmt.Errorf("Not yet constructed")
	// ErrWasDestructed is returned if the element has just been destructed.
	ErrWasDestructed = fmt.Errorf("Was destructed")
)
View Source
var (
	// ServiceFlag is the service name that will be attached to log entries.
	ServiceFlag = config.DeclareString("", "service", "")
	// InstanceIDFlag is the instance ID that will be attached to log entries.
	InstanceIDFlag = config.DeclareString("", "instanceID", RandomID())

	// ContainerNameFlag is the name of the container this is running as (needs
	// to be set correctly from command line arg). This is used to determine
	// the containerID.
	ContainerNameFlag = config.DeclareString("", "containerName", "")
)
View Source
var (
	// LoggingFormatFlag is the format of log lines.
	LoggingFormatFlag = config.DeclareString(
		PackageName, "loggingFormat", "colortext")
	// LoggingLevelFlag is the minimum level to log.
	LoggingLevelFlag = config.DeclareString(
		PackageName, "loggingLevel", "info")
	// LogInstanceAndServiceFlag causes all loggers to also log the instance ID
	// and service name of the process. Useful if logs from multiple sources
	// are merged and need to be filtered afterwards.
	LogInstanceAndServiceFlag = config.DeclareBool(
		PackageName, "logInstanceAndService")
)

Functions

func ExpBackoff

func ExpBackoff(
	tryFun TryFun, startBackoff time.Duration,
	maxTriesDuration time.Duration) (result interface{}, err error)

ExpBackoff retries tryFun with exponential backoff, until error is nil or finalErr is not nil, or the total time spent trying exceeds maxTriesDuration.

func GetRand

func GetRand() (tmpRand *rand.Rand)

GetRand returns a readily seeded rand from a pool.

func PutRand

func PutRand(tmpRand *rand.Rand)

PutRand returns the rand to the pool.

func RandomHostName

func RandomHostName() string

RandomHostName returns a random name that can be used as part of a host name.

func RandomID

func RandomID() string

RandomID returns a random string identifier.

func Tar

func Tar(writer io.Writer, sourceDir string) error

Tar archives a source directory to a .tar.gz archive.

func Untar

func Untar(reader io.Reader, targetDir string) error

Untar extracts a .tar.gz archive to the provided target directory.

func UpdateLoggingSettings

func UpdateLoggingSettings()

UpdateLoggingSettings initializes logging based on config params.

Types

type Cache

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

Cache is a data structure that keeps in-memory instances of objects for a specified amount of time after their last use.

func NewCache

func NewCache(
	expiry time.Duration,
	constructor func(string) (interface{}, error),
	destructor func(interface{})) *Cache

NewCache creates a new Cache object.

func (*Cache) Destroy

func (cache *Cache) Destroy(key string) bool

Destroy removes an instance from the Cache and calls the destructor for it.

func (*Cache) Get

func (cache *Cache) Get(key string) (interface{}, error)

Get returns either a cached instance with provided key or a newly constructed one, if an instance is not found.

func (*Cache) GetExisting

func (cache *Cache) GetExisting(key string) (interface{}, bool)

GetExisting returns a cached instance with provided key, if it exists.

func (*Cache) KeepAlive

func (cache *Cache) KeepAlive(key string) bool

KeepAlive can be used to prevent expiry of instance with provided key. Returns false if instance does not exist.

type Logger

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

Logger represents a logger with some metadata associated.

func GetLogger

func GetLogger(pack, name string) *Logger

GetLogger returns a logger with provided package and name.

func (*Logger) Debug

func (logger *Logger) Debug(msg string)

Debug logs debug message.

func (*Logger) Entry

func (logger *Logger) Entry() *logrus.Entry

Entry returns the logrus.Entry which represents the logger.

func (*Logger) Error

func (logger *Logger) Error(msg string)

Error logs error message.

func (*Logger) Fatal

func (logger *Logger) Fatal(msg string)

Fatal logs fatal message and calls os.Exit(1).

func (*Logger) Info

func (logger *Logger) Info(msg string)

Info logs info message.

func (*Logger) Panic

func (logger *Logger) Panic(msg string)

Panic logs panic message and calls panic.

func (*Logger) Warning

func (logger *Logger) Warning(msg string)

Warning logs warning message.

func (*Logger) WithFields

func (logger *Logger) WithFields(args ...interface{}) (ret *Logger)

WithFields returns a new logger with provided metadata fields attached.

type TryFun

type TryFun func() (result interface{}, err error, finalErr error)

TryFun is the signature of the function that can be passed to ExpBackoff.

type UnboundedChannel

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

UnboundedChannel is a channel with unlimited capacity.

func NewUnboundedChannel

func NewUnboundedChannel() *UnboundedChannel

NewUnboundedChannel returns a new UnboundedChannel.

func (*UnboundedChannel) Get

func (uc *UnboundedChannel) Get() <-chan interface{}

Get returns the channel that can be listened to for the next available element. Call Load right after receiving anything from the channel in order to load the next element for receiving.

func (*UnboundedChannel) Load

func (uc *UnboundedChannel) Load()

Load loads the next item to be received via Get.

func (*UnboundedChannel) Put

func (uc *UnboundedChannel) Put(element interface{})

Put appends the element to the channel.

Jump to

Keyboard shortcuts

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