Documentation
¶
Overview ¶
Package utils contains all utility functions that currently have no better home than here.
Index ¶
- func ContextMainIterFunc(ctx context.Context) func()
- func ContextMainQuitSignal(ctx context.Context) <-chan os.Signal
- func ContextMainReadyFunc(ctx context.Context) func()
- func ContextWithIterFunc(ctx context.Context, f func()) context.Context
- func ContextWithQuitSignal(ctx context.Context, c <-chan os.Signal) context.Context
- func ContextWithReadyFunc(ctx context.Context, c chan<- struct{}) context.Context
- func ContextualMain(main func(ctx context.Context, args []string, logger *zap.SugaredLogger) error, ...)
- func ContextualMainQuit(main func(ctx context.Context, args []string, logger *zap.SugaredLogger) error, ...)
- func ErrorWithStack(err error) error
- func FilterOutError(err, target error) error
- func FindGoroutineLeaks(options ...goleak.Option) error
- func ManagedGo(f, onComplete func())
- func NewObservedTestLogger(tb testing.TB) (*zap.SugaredLogger, *observer.ObservedLogs)
- func NewTestLogger(tb testing.TB) *zap.SugaredLogger
- func PanicCapturingGo(f func())
- func PanicCapturingGoWithCallback(f func(), callback func(err interface{}))
- func SelectContextOrWait(ctx context.Context, dur time.Duration) bool
- func SelectContextOrWaitChan[T any](ctx context.Context, c <-chan T) bool
- func SelectContextOrWaitChanVal[T any](ctx context.Context, c <-chan T) (T, bool)
- func SlowGoroutineWatcher(dur time.Duration, slowMsg string, logger *zap.SugaredLogger) (<-chan struct{}, func())
- func SlowGoroutineWatcherAfterContext(ctx context.Context, dur time.Duration, slowMsg string, ...) (<-chan struct{}, func())
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextMainIterFunc ¶
ContextMainIterFunc returns a function for indicating an iteration of the program has completed.
func ContextMainQuitSignal ¶
ContextMainQuitSignal returns a signal channel for quits. It may be nil if the value was never set.
func ContextMainReadyFunc ¶
ContextMainReadyFunc returns a function for indicating readiness. This is intended for main functions that block forever (e.g. daemons).
func ContextWithIterFunc ¶
ContextWithIterFunc attaches an iteration func to the given context.
func ContextWithQuitSignal ¶
ContextWithQuitSignal attaches a quit signaler to the given context.
func ContextWithReadyFunc ¶
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 ¶
ErrorWithStack returns an error with a stacktrace as long as there is not one currently attached.
func FilterOutError ¶
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 ¶
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 ¶
SelectContextOrWait either terminates because the given context is done or the given duration elapses. It returns true if the duration elapsed.
func SelectContextOrWaitChan ¶
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
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.