util

package
v0.35.4 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: AGPL-3.0 Imports: 9 Imported by: 8

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 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 struct {
	Message string
	Total   int
	Sampler zerolog.Sampler
}

func DefaultLogProgressConfig added in v0.33.1

func DefaultLogProgressConfig(
	message string,
	total int,
) LogProgressConfig

func NewLogProgressConfig added in v0.33.1

func NewLogProgressConfig(
	message string,
	total int,
	sampler zerolog.Sampler) LogProgressConfig

type LogProgressFunc added in v0.33.1

type LogProgressFunc func(addProgress int)

LogProgressFunc is a function that can be called to add to the progress

func LogProgress added in v0.29.0

func LogProgress(
	log zerolog.Logger,
	config LogProgressConfig,
) LogProgressFunc

LogProgress takes a total and return function such that when called adds the given number to the progress and logs the progress every 10% or every 60 seconds whichever comes first. The returned function can be called concurrently. An eta is also logged, but it assumes that the progress is linear.

type LogProgressOption added in v0.33.1

type LogProgressOption func(config *LogProgressConfig)

type TimedSampler added in v0.33.1

type TimedSampler struct {
	Duration time.Duration
	// contains filtered or unexported fields
}

func NewTimedSampler added in v0.33.1

func NewTimedSampler(duration time.Duration) *TimedSampler

func (*TimedSampler) Reset added in v0.33.1

func (s *TimedSampler) Reset()

func (*TimedSampler) Sample added in v0.33.1

func (s *TimedSampler) Sample(_ zerolog.Level) bool

Jump to

Keyboard shortcuts

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