utils

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnonymizeToken

func AnonymizeToken(token string, clearLen int) string

AnonymizeToken - replaces chunk of a string with '*'

func ConstRefBool

func ConstRefBool(b bool) *bool

ConstRefBool - constructs boolean

func ConstRefInt32

func ConstRefInt32(i int32) *int32

ConstRefInt32 - constructs 32 bit integer

func ConstRefStr

func ConstRefStr(s string) *string

ConstRefStr - construct string

func EnvVarBool

func EnvVarBool(varName string, defaultValue bool) bool

EnvVarBool - retrieves value of boolean environment variable, fails if variable contains non-boolean value

func EnvVarReqStr

func EnvVarReqStr(varName string) string

EnvVarReqStr - retrieves value of string environment variable, fails if it is not present or empty

func EnvVarSeconds

func EnvVarSeconds(varName string, defaultValue time.Duration) time.Duration

EnvVarSeconds - retrieves value of environment variable reperesenting duration in seconds, fails if variable non-parseable values

func EnvVarStr

func EnvVarStr(varName string, defaultValue string) string

EnvVarStr - retrieves value of string environment variable, while applying default

func LoadDotEnvFile

func LoadDotEnvFile()

LoadDotEnvFile - Loads environment variables from .env file in the current working directory (if found)

func MinInt

func MinInt(a int, b int) int

MinInt - returns smaller of 2 integers TODO: Go seriously doesn't have a Min function??

func RunWithPerseverance

func RunWithPerseverance(handler func(AttemptContext), ctx GracefulContext, opts PerseverenceOpts)

RunWithPerseverance - runs handler and tries it again if it fails

Types

type AttemptContext

type AttemptContext interface {
	GracefulContext

	// GetTry - returns assigned number of tries
	GetTry() int
}

AttemptContext - attempt context container

type GracefulContext

type GracefulContext interface {
	// Done - blocks until cancelled
	Done() <-chan struct{}

	// RunAsChild - runs handler within child context
	RunAsChild(callback func(GracefulContext)) GracefulRunner

	// Fail - cancels run from the inside and propagates cancel to all children
	// Does not await the cancellation (obviously)
	Fail(err error)
}

GracefulContext - a context carries channel factory for cancelation

type GracefulRunner

type GracefulRunner interface {
	// Wait - blocks until finishes execution
	Wait() (bool, error)

	// Cancel - notifies handler to cancel the execution and awaits graceful return (clean up)
	Cancel()
}

GracefulRunner - outter API for controlling gracefully run handlers

func RunWithGracefulCancel

func RunWithGracefulCancel(callback func(GracefulContext)) GracefulRunner

RunWithGracefulCancel - runs callback as a go routine and returns cancel routine This is inspired by context but with the key difference that the cancel function waits until the handler finishes all the cleanup @see https://blog.golang.org/context

type LogTailer

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

LogTailer - implements circular buffer for tailing last n-lines

func NewLogTailer

func NewLogTailer(maxNumLines int) *LogTailer

NewLogTailer - constructor

func (*LogTailer) Append

func (tailer *LogTailer) Append(line string)

Append - appends line to the buffer

func (*LogTailer) GetLines

func (tailer *LogTailer) GetLines() []string

GetLines - return tailed lines

func (*LogTailer) String

func (tailer *LogTailer) String() string

String - returns tailed lines as string

func (*LogTailer) Tail

func (tailer *LogTailer) Tail(r io.Reader)

Tail - tails line of given reader

type PerseverenceOpts

type PerseverenceOpts struct {
	// Cooldown - List of cooldown periods for failed attempts.
	// If execution fails more times than length of this array, last item is used.
	Cooldown []time.Duration

	// ResetThreshold - After this time failed attempts are counted as first failure
	ResetThreshold time.Duration

	// RunnerID - optional string name for the runner for debugging purposes
	RunnerID string
}

PerseverenceOpts - options container for RunWithPerseverance

Jump to

Keyboard shortcuts

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