util

package
v1.0.0-pre.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FreePort

func FreePort() (int, error)

FreePort returns a free port number

func NewChannelWithWorkerPool

func NewChannelWithWorkerPool(ctx context.Context, numWorkers, channelLen int, errHandler ErrorFunc, backoff Backoff) chan WorkFunc

NewChannelWithWorkerPool returns an unbuffered channel to send work to the set of workers. Close the returned channel or cancel the Context and the workers will exit. Passed ctx is passed on to the work function and work should check for cancel if long-running. Beware: If errHandler itself returns an error, the worker will exit.

func NewReservoir

func NewReservoir(limit int) (chan<- interface{}, <-chan interface{}, <-chan interface{})

NewReservoir sends from one channel to another without blocking until closed. Once "in" channel is closed, "out" will continue to drain before closing. if buffer limit is reached, new messages (LIFO) are sent to overflow: non-blocking, can be overrun.

func ReadPropertiesFile

func ReadPropertiesFile(fileName string) (map[string]string, error)

ReadPropertiesFile Parses a simple properties file (xx=xx format)

func SprintfRedacts

func SprintfRedacts(redacts []interface{}, format string, a ...interface{}) string

SprintfRedacts truncates secret strings to len(5)

func Truncate

func Truncate(in string, end int) string

Truncate truncates secret strings to arbitrary length and adds "..." as indication

Types

type AtomicBool

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

AtomicBool is a threadsafe bool

func NewAtomicBool

func NewAtomicBool(flag bool) *AtomicBool

NewAtomicBool creates an AtomicBool

func (*AtomicBool) IsFalse

func (a *AtomicBool) IsFalse() bool

IsFalse returns false if false

func (*AtomicBool) IsTrue

func (a *AtomicBool) IsTrue() bool

IsTrue returns true if true

func (*AtomicBool) SetFalse

func (a *AtomicBool) SetFalse() bool

SetFalse sets the bool to false, returns true if unchanged

func (*AtomicBool) SetTrue

func (a *AtomicBool) SetTrue() bool

SetTrue sets the bool to true, returns true if unchanged

type Backoff

type Backoff interface {
	Duration() time.Duration
	Attempt() int
	Reset()
	Clone() Backoff
}

Backoff defines functions for RPC Backoff strategy.

func DefaultExponentialBackoff

func DefaultExponentialBackoff() Backoff

DefaultExponentialBackoff constructs a new ExponentialBackoff with defaults.

func NewExponentialBackoff

func NewExponentialBackoff(initial, max time.Duration, factor float64, jitter bool) Backoff

NewExponentialBackoff constructs a new ExponentialBackoff.

type ErrorFunc

type ErrorFunc = func(error) error

ErrorFunc handles errors

func LogErrorsHandler

func LogErrorsHandler() ErrorFunc

LogErrorsHandler just logs errors and continues

type ExponentialBackoff

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

ExponentialBackoff is a backoff strategy that backs off exponentially.

func (*ExponentialBackoff) Attempt

func (b *ExponentialBackoff) Attempt() int

Attempt returns how many attempts have been made.

func (*ExponentialBackoff) Clone

func (b *ExponentialBackoff) Clone() Backoff

Clone returns a copy

func (*ExponentialBackoff) Duration

func (b *ExponentialBackoff) Duration() time.Duration

Duration calculates how long should be waited before attempting again. Note that this method is stateful - each call counts as an "attempt".

func (*ExponentialBackoff) Reset

func (b *ExponentialBackoff) Reset()

Reset clears any state that the backoff strategy has.

type Looper

type Looper struct {
	Backoff Backoff
}

Looper provides for Backoff and cancellation

func (*Looper) Chan

func (l *Looper) Chan(ctx context.Context, work <-chan (WorkFunc), errHandler ErrorFunc)

Chan pulls work from work channel until channel is closed of Context canceled. Passed ctx is passed on to the work function and work should check for cancel if long-running. If errHandler itself returns an error, the daemon will exit.

func (*Looper) Run

func (l *Looper) Run(ctx context.Context, work WorkFunc, errHandler ErrorFunc) error

Run the work until successful (or ctx canceled) with backoff. Passed ctx should be cancelable - to exit, cancel the Context. Passed ctx is passed on to the work function and work should check for cancel if long-running. If errHandler itself returns an error, the daemon will exit.

func (*Looper) Start

func (l *Looper) Start(ctx context.Context, work WorkFunc, period time.Duration, errHandler ErrorFunc)

Start a daemon that repeatedly calls work function according to period. Passed ctx should be cancelable - to exit, cancel the Context. Passed ctx is passed on to the work function and work should check for cancel if long-running. If errHandler itself returns an error, the daemon will exit.

type WorkFunc

type WorkFunc = func(ctx context.Context) error

WorkFunc does work

Jump to

Keyboard shortcuts

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