util

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2020 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base64To256BitKey

func Base64To256BitKey(ks string) (*[32]byte, error)

Base64To256BitKey converts an encryption key []byte to *[32]byte. Used for importing encryption keys from configs.

func GUID

func GUID() string

GUID returns a new GUID.

func GetEnvironment

func GetEnvironment() map[string]string

GetEnvironment variables.

func GetMonitoringLabels

func GetMonitoringLabels() map[string]string

GetMonitoringLabels returns all static monitoring labels.

func GetPortNumber

func GetPortNumber(address string) (int, error)

GetPortNumber from an address string. Example: localhost:123 returns 123, [::]:456 returns 456.

func LogError

func LogError(err error, msg string)

LogError logs an error if there is one.

func LogErrorf

func LogErrorf(err error, msg string, v ...interface{})

LogErrorf logs an error if there is one.

func LogFatal

func LogFatal(err error, msg string)

LogFatal logs an error if there is one and terminates the program.

func LogFatalf

func LogFatalf(err error, msg string, v ...interface{})

LogFatalf logs an error if there is one.

func To256BitKey

func To256BitKey(key []byte) (*[32]byte, error)

To256BitKey converts an encryption key []byte to *[32]byte. Used for importing encryption keys from configs.

func UpdateBaseDirectory

func UpdateBaseDirectory(cfg ConfigView, baseDir string)

UpdateBaseDirectory updates configuration defaults with the new base directory.

func UpdateServerDefaults

func UpdateServerDefaults(cfg ConfigView, grpcAddress string, httpAddress string)

UpdateServerDefaults updates the address of each server's defaults.

Types

type Config

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

Config is a mutable configuration.

func EmptyConfig

func EmptyConfig() *Config

EmptyConfig creates an empty configuration.

func (*Config) AllSettings

func (c *Config) AllSettings() map[string]interface{}

AllSettings gets all configuration values.

func (*Config) GetBool

func (c *Config) GetBool(k string) bool

GetBool returns a configuration entry as a boolean.

func (*Config) GetDuration

func (c *Config) GetDuration(k string) time.Duration

GetDuration returns a configuration entry as a time.Duration.

func (*Config) GetFloat64

func (c *Config) GetFloat64(k string) float64

GetFloat64 returns a configuration entry as a float64.

func (*Config) GetInt

func (c *Config) GetInt(k string) int

GetInt returns a configuration entry as a integer.

func (*Config) GetString

func (c *Config) GetString(k string) string

GetString returns a configuration entry as a string.

func (*Config) Set

func (c *Config) Set(k string, v interface{})

Set the value of a configuration.

func (*Config) SetDefault

func (c *Config) SetDefault(k string, v interface{})

SetDefault sets the default value of a configuration

type ConfigView

type ConfigView interface {
	// GetString returns a configuration entry as a string.
	GetString(string) string
	// GetInt returns a configuration entry as a integer.
	GetInt(string) int
	// GetBool returns a configuration entry as a boolean.
	GetBool(string) bool
	// GetFloat64 returns a configuration entry as a float64.
	GetFloat64(string) float64
	// GetDuration returns a configuration entry as a time.Duration.
	GetDuration(string) time.Duration
}

ConfigView is a view of the configuration.

func DefaultConfig

func DefaultConfig() ConfigView

DefaultConfig returns a configuration view with internal defaults set. This should only be used for internal testing.

func MustLoadConfig

func MustLoadConfig() ConfigView

MustLoadConfig loads configuration for the application.

type Counter

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

Counter can be incremented atomically.

func NewCounter

func NewCounter() *Counter

NewCounter creates a new counter.

func (*Counter) Get

func (c *Counter) Get() int64

Get the value of the counter.

func (*Counter) Inc

func (c *Counter) Inc() int64

Inc increments the counter.

func (*Counter) IncBy

func (c *Counter) IncBy(by int64) int64

IncBy increments the counter by the input.

type Crypter

type Crypter interface {
	Encrypt(plainText []byte) ([]byte, error)
	Decrypt(cipherText []byte) ([]byte, error)
}

Crypter can encrypt and decrypt data.

func MustCrypter

func MustCrypter(base64EncryptionKey string) Crypter

MustCrypter returns a new crypter based on the input.

func NewAES256Crypter

func NewAES256Crypter(key *[32]byte) Crypter

NewAES256Crypter creates a new AES-256-GCM crypter.

func NewCrypter

func NewCrypter(base64EncryptionKey string) (Crypter, error)

NewCrypter returns a new crypter based on the input.

func NewNullEncrypter

func NewNullEncrypter() Crypter

NewNullEncrypter creates a crypter that does nothing. Useful for testing but should not be used in production.

type ListenSpec

type ListenSpec struct {
	Port     int
	Address  string
	Protocol string
	// contains filtered or unexported fields
}

ListenSpec is a holder class for an opened port.

func OpenWithAddress

func OpenWithAddress(protocol string, address string) (*ListenSpec, error)

OpenWithAddress opens a port via an address.

func (*ListenSpec) Acquire

func (ls *ListenSpec) Acquire() net.Listener

Acquire returns the listener only once.

func (*ListenSpec) Close

func (ls *ListenSpec) Close() error

Close the port that is being listened to.

func (*ListenSpec) IsOpen

func (ls *ListenSpec) IsOpen() bool

IsOpen indicates that the TCP port is open.

type Pubsub

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

Pubsub relays messages to subscribers of a topic.

func NewPubsub

func NewPubsub() *Pubsub

NewPubsub creates a new Pubsub.

func (*Pubsub) Publish

func (p *Pubsub) Publish(topic string, msg interface{})

Publish a message on the topic.

func (*Pubsub) Subscribe

func (p *Pubsub) Subscribe(topic string) (chan interface{}, func())

Subscribe to a topic.

type Stopwatch

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

Stopwatch is used to count difference in time.

func NewStopwatch

func NewStopwatch() *Stopwatch

NewStopwatch creates a stop watch at the timestamp it was created.

func NewStopwatchAt

func NewStopwatchAt(start time.Time) *Stopwatch

NewStopwatchAt creates a stop watch at a specific timestamp.

func (*Stopwatch) TickMillis

func (s *Stopwatch) TickMillis() int64

TickMillis returns the difference in time as milliseconds.

func (*Stopwatch) TickNanos

func (s *Stopwatch) TickNanos() int64

TickNanos returns the difference in time as milliseconds.

type Switch

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

Switch can be toggled on or off.

func NewSwitch

func NewSwitch(name string) *Switch

NewSwitch creates a new switch.

func (*Switch) Get

func (s *Switch) Get() bool

Get returns true if the switch is activated.

func (*Switch) Off

func (s *Switch) Off()

Off deactivates the switch.

func (*Switch) On

func (s *Switch) On()

On activates the switch.

func (*Switch) OnIfOff

func (s *Switch) OnIfOff() bool

OnIfOff activates the switch, returns true if the switch was not already activated.

func (*Switch) Toggle

func (s *Switch) Toggle() bool

Toggle increments the counter.

type ThrottledCall

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

ThrottledCall throttles method invocations.

func NewThrottledCall

func NewThrottledCall(name string, delay time.Duration) *ThrottledCall

NewThrottledCall returns a throttler

func (*ThrottledCall) Close

func (tc *ThrottledCall) Close()

Close the timer.

func (*ThrottledCall) Invoke

func (tc *ThrottledCall) Invoke(f func())

Invoke a throttled method.

type UnitOfWork

type UnitOfWork func()

UnitOfWork is a function that is called asynchronously.

type WorkQueue

type WorkQueue interface {
	Enqueue(f UnitOfWork) error
	Drain()
}

WorkQueue is a queue that performs work asynchronously.

func NewWorkQueue

func NewWorkQueue() WorkQueue

NewWorkQueue returns a work queue with the default size.

func NewWorkQueueWithSize

func NewWorkQueueWithSize(size int) WorkQueue

NewWorkQueueWithSize returns a work queue with the specified size.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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