channels

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBroadcastWithNoReceivers is returned, when Broadcast is called with no receiver channels
	ErrBroadcastWithNoReceivers = fmt.Errorf("broadcast called with no receiver channels")
	// ErrInvalidChunkSize is returned, when ChunkAndDo is called with chunk size of 0
	ErrInvalidChunkSize = fmt.Errorf("parameter size must be greater than 0")
)

Functions

func Broadcast

func Broadcast[T any](ctx context.Context, src <-chan T, receiver ...chan<- T) error

Broadcast consumes the src channel and yields the incoming messages on every receiver channel. It waits in each cycle until the message could be sent on every dst channel.

The function is context aware.

func BroadcastWithCount

func BroadcastWithCount[T any](ctx context.Context, src <-chan T, receiver ...chan<- T) (int, error)

BroadcastWithCount works like Broadcast, but additionally returns the number of items sent to all receivers. That doesn't mean that one or the other receiver might have received more messages.

The function is context aware.

func ChunkAndDo

func ChunkAndDo[T any](ctx context.Context, src <-chan T, size uint, doer func(context.Context, []T) error) error

ChunkAndDo reads the given channel up until X elements, defined by size parameter, have been consumed, and then calls the doer function provided, passing the actual chunk of data.

The function is context aware.

func Collect

func Collect[T any](ctx context.Context, src <-chan T) ([]T, error)

Collect reads the given channel and returns a slice with all elements received on the src channel.

The function is context aware.

func Drain

func Drain[T any](ctx context.Context, src <-chan T) error

Drain will drain the provided src channel up until all elements have been drained and the channel has been closed, or the given context has been canceled.

If you need to drain the channel in any case, pass a context not being canceled.

⚠️ Beware: The function will block until the src channel is closed then.

func DrainWithCount

func DrainWithCount[T any](ctx context.Context, src <-chan T) (int, error)

DrainWithCount works like Drain, but additionally returns the number of items discarded.

func Forward

func Forward[T any](ctx context.Context, generate generateFN[T], items chan<- T, errs chan<- error) error

Forward pipes the data emitted on the channels returned by the generate function to the given outer channels without any other manipulation. It blocks until both channels coming from generate function have been closed

func ForwardWithCounts

func ForwardWithCounts[T any](
	ctx context.Context,
	generate generateFN[T],
	items chan<- T,
	errs chan<- error,
) (itemCount, errCount int, _ error)

ForwardWithCounts works like Forward, but additionally returns the number of items and errors forwarded.

func Merge added in v0.2.0

func Merge[T any](ctx context.Context, sources ...<-chan T) <-chan T

Merge joins the provided list of source channels into a single one, which can then be consumed in the main thread

It returns the joined T channel and takes care of closing it properly after having consumed all the source channels until they are closed, or the context is canceled. In case of context cancellation, make sure to cancel the producing channels to avoid leaking goroutines.

func Send

func Send[T any](ctx context.Context, dst chan<- T, items ...T) error

Send securely sends all items provided on channel dst in a context aware fashion.

It returns an error in case the context was canceled, nil otherwise.

Therefore, it's a shorthand notation for the following select statement

select {
case <-ctx.Done():
	// abort operation
case dst <- item:
}

func SendE

func SendE(ctx context.Context, dst chan<- error, err ...error) error

SendE is an error type bound variant of Send, to not run into type inference issues when using error types directly

func SendEWithCount

func SendEWithCount(ctx context.Context, dst chan<- error, err ...error) (int, error)

SendEWithCount is an error type bound variant of SendWithCount, to not run into type inference issues when using error types directly

func SendWithCount

func SendWithCount[T any](ctx context.Context, dst chan<- T, items ...T) (int, error)

SendWithCount works like Send, but additionally returns the number of items sent.

func Wrap

func Wrap[I, O any](ctx context.Context, src <-chan I, proc func(context.Context, I) (O, bool)) <-chan O

Wrap consumes the src channel of type I passing every item received to the proc function provided, and yields the return value of it on the result channel of type O returned. The process function must return a boolean flag, whether the returned value is ok, and shall be pushed on the outgoing channel. If the ok flag is set to false, the item will be discarded.

This outgoing channel is closed, once either the incoming channel has been closed, or the context has been canceled

func WrapSpread

func WrapSpread[I, O any](ctx context.Context, src <-chan I, proc func(context.Context, I) []O) <-chan O

WrapSpread consumes the src channel of type I passing every item received to the proc function provided, and yields the return values of it on the result channel of type O returned. The typical slice semantics apply here. So if you wish the result to be ignored, just return a nil/empty slice from the proc function.

This outgoing channel is closed, once either the incoming channel has been closed, or the context has been canceled

Types

This section is empty.

Jump to

Keyboard shortcuts

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