util

package
v0.43.3 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2025 License: AGPL-3.0 Imports: 11 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllClosed added in v0.23.2

func AllClosed(channels ...<-chan struct{}) <-chan struct{}

AllClosed returns a channel that is closed when all input channels are closed.

func AllDone

func AllDone(components ...module.ReadyDoneAware) <-chan struct{}

AllDone calls Done on all input components and returns a channel that is closed when all input components are done.

func AllReady

func AllReady(components ...module.ReadyDoneAware) <-chan struct{}

AllReady calls Ready on all input components and returns a channel that is closed when all input components are ready.

func CheckClosed added in v0.23.9

func CheckClosed(done <-chan struct{}) bool

CheckClosed checks if the provided channel has a signal or was closed. Returns true if the channel was signaled/closed, otherwise, returns false.

This is intended to reduce boilerplate code when multiple channel checks are required because missed signals could cause safety issues.

func DetypeSlice added in v0.29.0

func DetypeSlice[T any](typedSlice []T) []any

DetypeSlice converts a typed slice containing any kind of elements into an untyped []any type, in effect removing the element type information from the slice. It is useful for passing data into structpb.NewValue, which accepts []any but not []T for any specific type T.

func IsEmptyOrNotExists added in v0.43.0

func IsEmptyOrNotExists(path string) (bool, error)

IsEmptyOrNotExists returns true if the directory does not exist or is empty. It returns an error if there's an issue accessing the directory.

func MergeChannels added in v0.23.9

func MergeChannels(channels interface{}) interface{}

MergeChannels merges a list of channels into a single channel

func MergeReadyDone added in v0.29.0

func MergeReadyDone(components ...module.ReadyDoneAware) module.ReadyDoneAware

MergeReadyDone merges []module.ReadyDoneAware into one module.ReadyDoneAware.

func SampleN added in v0.31.0

func SampleN(n int, max, percentage float64) uint

SampleN computes a percentage of the given number 'n', and returns the result as an unsigned integer. If the calculated sample is greater than the provided 'max' value, it returns the ceil of 'max'. If 'n' is less than or equal to 0, it returns 0.

Parameters: - n: The input number, used as the base to compute the percentage. - max: The maximum value that the computed sample should not exceed. - percentage: The percentage (in range 0.0 to 1.0) to be applied to 'n'.

Returns: - The computed sample as an unsigned integer, with consideration to the given constraints.

func WaitClosed added in v0.23.9

func WaitClosed(ctx context.Context, ch <-chan struct{}) error

WaitClosed waits for either a signal/close on the channel or for the context to be cancelled Returns nil if the channel was signalled/closed before returning, otherwise, it returns the context error.

This handles the corner case where the context is cancelled at the same time that the channel is closed, and the Done case was selected. This is intended for situations where ignoring a signal can cause safety issues.

func WaitError added in v0.23.9

func WaitError(errChan <-chan error, done <-chan struct{}) error

WaitError waits for either an error on the error channel or the done channel to close Returns an error if one is received on the error channel, otherwise it returns nil

This handles a race condition where the done channel could have been closed as a result of an irrecoverable error being thrown, so that when the scheduler yields control back to this goroutine, both channels are available to read from. If the done case happens to be chosen at random to proceed instead of the error case, then we would return without error which could result in unsafe continuation.

Types

type LogProgressConfig added in v0.33.1

type LogProgressConfig[T int | uint | int32 | uint32 | uint64 | int64] struct {
	// contains filtered or unexported fields
}

func DefaultLogProgressConfig added in v0.33.1

func DefaultLogProgressConfig[T int | uint | int32 | uint32 | uint64 | int64](
	message string,
	total T,
) LogProgressConfig[T]

DefaultLogProgressConfig returns a LogProgressConfig with default values. The default values will log every 10% and will log an additional line if new data is received after no data has been received for 1 minute.

func NewLogProgressConfig added in v0.33.1

func NewLogProgressConfig[T int | uint | int32 | uint32 | uint64 | int64](
	message string,
	total T,
	noDataLogDuration time.Duration,
	ticks uint64,
) LogProgressConfig[T]

NewLogProgressConfig creates and returns a new LogProgressConfig with the specified message, total, duration, and ticks. The duration is rounded to the nearest millisecond. The number of ticks is the number of increments to log at. Logging at 0% is always done. If you want to log at 10% increments, set ticks to 10.

type LogProgressFunc added in v0.33.1

type LogProgressFunc[T int | uint | int32 | uint32 | uint64 | int64] func(addProgress T)

LogProgressFunc is a function that can be called to add to the progress. The function can be called concurrently. addProgress is the amount to add to the progress. It is any integer number type, but all negative values are ignored.

func LogProgress added in v0.29.0

func LogProgress[T int | uint | int32 | uint32 | uint64 | int64](
	log zerolog.Logger,
	config LogProgressConfig[T],
) LogProgressFunc[T]

LogProgress takes a LogProgressConfig and return function such that when called adds the given number to the progress and logs the progress in defined increments or there is a time gap between progress updates. The returned function can be called concurrently. An eta is also logged, but it assumes that the progress is linear.

Jump to

Keyboard shortcuts

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