utils

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2024 License: MIT Imports: 18 Imported by: 3

Documentation

Overview

Package utils contains all utility functions that currently have no better home than here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextMainIterFunc

func ContextMainIterFunc(ctx context.Context) func()

ContextMainIterFunc returns a function for indicating an iteration of the program has completed.

func ContextMainQuitSignal

func ContextMainQuitSignal(ctx context.Context) <-chan os.Signal

ContextMainQuitSignal returns a signal channel for quits. It may be nil if the value was never set.

func ContextMainReadyFunc

func ContextMainReadyFunc(ctx context.Context) func()

ContextMainReadyFunc returns a function for indicating readiness. This is intended for main functions that block forever (e.g. daemons).

func ContextWithIterFunc

func ContextWithIterFunc(ctx context.Context, f func()) context.Context

ContextWithIterFunc attaches an iteration func to the given context.

func ContextWithQuitSignal

func ContextWithQuitSignal(ctx context.Context, c <-chan os.Signal) context.Context

ContextWithQuitSignal attaches a quit signaler to the given context.

func ContextWithReadyFunc

func ContextWithReadyFunc(ctx context.Context, c chan<- struct{}) context.Context

ContextWithReadyFunc attaches a ready signaler to the given context.

func ContextualMain

func ContextualMain(main func(ctx context.Context, args []string, logger *zap.SugaredLogger) error, logger *zap.SugaredLogger)

ContextualMain calls a main entry point function with a cancellable context via SIGTERM. This should be called once per process so as to not clobber the signals from Notify.

func ContextualMainQuit

func ContextualMainQuit(main func(ctx context.Context, args []string, logger *zap.SugaredLogger) error, logger *zap.SugaredLogger)

ContextualMainQuit is the same as ContextualMain but catches quit signals into the provided context accessed via ContextMainQuitSignal.

func ErrorWithStack

func ErrorWithStack(err error) error

ErrorWithStack returns an error with a stacktrace as long as there is not one currently attached.

func FilterOutError

func FilterOutError(err, target error) error

FilterOutError filters out an error based on the given target. For example, if err was context.Canceled and so was the target, this would return nil. Furthermore, if err was a multierr containing a context.Canceled, it would also be filtered out from a new multierr. The code in this package originally comes from https://github.com/viamrobotics/goutils/blob/fadaa66af715d712feea4e3637cecd12ed4b742b/error.go which is Apache 2.0 licensed. The following changes are: - dont use multierror.

func FindGoroutineLeaks

func FindGoroutineLeaks(options ...goleak.Option) error

FindGoroutineLeaks finds any goroutine leaks after a program is done running. This should be used at the end of a main test run or a top-level process run.

func ManagedGo

func ManagedGo(f, onComplete func())

ManagedGo keeps the given function alive in the background until it terminates normally.

func NewObservedTestLogger

func NewObservedTestLogger(tb testing.TB) (*zap.SugaredLogger, *observer.ObservedLogs)

NewObservedTestLogger is like NewTestLogger but also saves logs to an in memory observer.

func NewTestLogger

func NewTestLogger(tb testing.TB) *zap.SugaredLogger

NewTestLogger directs logs to the go test logger.

func PanicCapturingGo

func PanicCapturingGo(f func())

PanicCapturingGo spawns a goroutine to run the given function and captures any panic that occurs and logs it.

func PanicCapturingGoWithCallback

func PanicCapturingGoWithCallback(f func(), callback func(err interface{}))

PanicCapturingGoWithCallback spawns a goroutine to run the given function and captures any panic that occurs, logs it, and calls the given callback. The callback can be used for restart functionality.

func SelectContextOrWait

func SelectContextOrWait(ctx context.Context, dur time.Duration) bool

SelectContextOrWait either terminates because the given context is done or the given duration elapses. It returns true if the duration elapsed.

func SelectContextOrWaitChan

func SelectContextOrWaitChan[T any](ctx context.Context, c <-chan T) bool

SelectContextOrWaitChan either terminates because the given context is done or the given channel is received on. It returns true if the channel was received on.

func SelectContextOrWaitChanVal added in v0.0.4

func SelectContextOrWaitChanVal[T any](ctx context.Context, c <-chan T) (T, bool)

SelectContextOrWaitChanVal either terminates because the given context is done or the given channel has a value on it. It returns true if the channel was received on.

func SlowGoroutineWatcher

func SlowGoroutineWatcher(
	dur time.Duration,
	slowMsg string,
	logger *zap.SugaredLogger,
) (<-chan struct{}, func())

SlowGoroutineWatcher is used to monitor if a goroutine is going "slow". It will first kick off a wait based on 'dur'. If 'dur' elapses before the cancel func is called, then currently running goroutines will be dumped. The returned channel can be used to wait for this background goroutine to finish.

func SlowGoroutineWatcherAfterContext

func SlowGoroutineWatcherAfterContext(
	ctx context.Context,
	dur time.Duration,
	slowMsg string,
	logger *zap.SugaredLogger,
) (<-chan struct{}, func())

SlowGoroutineWatcherAfterContext is used to monitor if a goroutine is going "slow". It will first wait for the given context to be done and then kick off a wait based on 'dur'. If 'dur' elapses before the cancel func is called, then currently running goroutines will be dumped. The returned channel can be used to wait for this background goroutine to finish.

Types

This section is empty.

Directories

Path Synopsis
Derived from Apache 2.0 Eric Daniels https://github.com/viamrobotics/test/tree/f61b7c01c33ed4e8d01ae2f2263e0c34559384d3
Derived from Apache 2.0 Eric Daniels https://github.com/viamrobotics/test/tree/f61b7c01c33ed4e8d01ae2f2263e0c34559384d3

Jump to

Keyboard shortcuts

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