concurrentloop

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2025 License: MIT Imports: 16 Imported by: 16

README

concurrentloop

Go

concurrentloop provides a function to call a function concurrently powered by generics, channels, and goroutines.

Install

$ go get github.com/thalesfsp/concurrentloop

Specific version

Example: $ go get github.com/thalesfsp/concurrentloop@v1.2.0

Usage

See example_test.go, and concurrentloop_test.go file.

Documentation

Run $ make doc or check out online.

Development

Check out CONTRIBUTION.

Release
  1. Update CHANGELOG accordingly.
  2. Once changes from MR are merged.
  3. Tag and release.

Roadmap

Check out CHANGELOG.

Documentation

Overview

Package concurrentloop provides a function to call a function concurrently powered by generics, channels, and goroutines.

Index

Constants

View Source
const Name = "concurrentloop"

Name of the package.

Variables

This section is empty.

Functions

func ExecuteCh added in v1.1.1

func ExecuteCh[T any](ctx context.Context, fns []ExecuteFunc[T]) chan ResultCh[T]

ExecuteCh calls the `fns` concurrently.

NOTE: It's the caller's responsibility to close the channel.

func Flatten2D added in v1.2.5

func Flatten2D[T any](data [][]T) []T

Flatten2D takes a 2D slice and returns a 1D slice containing all the elements.

func RemoveZeroValues added in v1.1.9

func RemoveZeroValues[T any](removeZeroValues bool, results []T) []T

RemoveZeroValues removes zero values from the results.

func SplitSlice added in v1.3.0

func SplitSlice[T any](items []T, batchSize int) [][]T

SplitSlice splits a slice into batches of a given size.

Types

type Errors added in v1.1.0

type Errors []error

Errors is a slice of errors.

func Execute added in v1.1.1

func Execute[T any](ctx context.Context, fns []ExecuteFunc[T]) ([]T, Errors)

Execute calls the `fns` concurrently, and returns the results and any errors that occurred. The function blocks until all executions have completed.

NOTE: Order is may preserved.

func Map added in v1.1.1

func Map[T any, Result any](
	ctx context.Context,
	items []T,
	f MapFunc[T, Result],
	opts ...Func,
) ([]Result, Errors)

Map processes each element in a slice concurrently using the provided function. Returns a slice of results and any errors that occurred during processing.

func MapCh added in v1.1.1

func MapCh[T any, Result any](
	ctx context.Context,
	itemsMap map[string]T,
	f MapMFuncCh[T, Result],
	perCycleCh chan<- Result,
	endCh chan<- Result,
	opts ...Func,
) Errors

MapCh processes each key-value pair in a map concurrently with real-time result streaming. It follows the same pattern as Map but sends results to channels instead of returning them. Per-cycle results are sent to perCycleCh, final results (after RemoveZeroValues) are sent to endCh. Returns only errors that occurred during processing.

func MapDone added in v1.3.3

func MapDone[T any, Result any](
	ctx context.Context,
	items []T,
	f MapFuncCh[T, Result],
	opts ...Func,
) ([]Result, Errors)

MapDone processes each element in a slice concurrently with early termination support. The processing function receives a done channel that can be used to signal early termination. Returns a slice of results and any errors that occurred during processing.

func MapM added in v1.1.14

func MapM[T any, Result any](
	ctx context.Context,
	itemsMap map[string]T,
	f MapMFunc[T, Result],
	opts ...Func,
) ([]Result, Errors)

MapM processes each key-value pair in a map concurrently using the provided function. Returns a slice of results and any errors that occurred during processing.

func (Errors) Error added in v1.1.0

func (e Errors) Error() string

Error returns a string representation of the combined errors in the `Errors` slice, separated by commas. This method satisfies the `error` interface.

type ExecuteFunc added in v1.1.1

type ExecuteFunc[T any] func(context.Context) (T, error)

ExecuteFunc is the type of the function that will be executed concurrently for each element in a slice of type `T`. The function takes a `context.Context` and a value of type `T`, and returns a value of type `Result` and an error value.

type Func added in v1.0.1

type Func func(o Option) Option

Func allows to specify message's options.

func WithBatchSize added in v1.2.0

func WithBatchSize(concurrency int) Func

WithBatchSize sets the size of the batch.

func WithLimit added in v1.1.12

func WithLimit(limit int) Func

WithLimit sets the max amount of results to collect before stopping the loop.

func WithRandomDelayTime added in v1.2.1

func WithRandomDelayTime(minDelay, maxDelay int, d time.Duration) Func

WithRandomDelayTime sets the random delay time between each iteration.

func WithRemoveZeroValues added in v1.1.11

func WithRemoveZeroValues(remove bool) Func

WithRemoveZeroValues if set to true removes zero values from the results.

func WithWriter added in v1.4.0

func WithWriter(w io.Writer) Func

WithWriter sets a writer to write results to.

type MapFunc added in v1.1.1

type MapFunc[T any, Result any] func(ctx context.Context, item T) (Result, error)

MapFunc is the type of the function that will be executed concurrently for each element in a slice of type `T`. The function takes a `context.Context` and a value of type `T`, and returns a value of type `Result` and an error value.

type MapFuncCh added in v1.3.3

type MapFuncCh[T any, Result any] func(ctx context.Context, item T, done chan<- struct{}) (Result, error)

MapFuncCh is the type of the function that will be executed concurrently for each element in a slice with a done channel for early termination.

type MapMFunc added in v1.1.14

type MapMFunc[T any, Result any] func(ctx context.Context, key string, item T) (Result, error)

MapMFunc is the type of the function that will be executed concurrently for each element in the map.

type MapMFuncCh added in v1.4.0

type MapMFuncCh[T any, Result any] func(
	ctx context.Context,
	key string,
	item T,
	perCycleCh chan<- Result,
	endCh chan<- Result,
) (Result, error)

MapMFuncCh is the type of the function that will be executed concurrently for each element in a map with per-cycle and end channels for real-time result streaming.

type Option added in v1.1.7

type Option struct {
	// BatchSize is the size of the batch.
	BatchSize int

	// The max amount of results to collect before
	Limit int

	// RandomDelayTimeDuration is the unit of the duration (Second, Millisecond, etc.)
	RandomDelayTimeDuration time.Duration

	// RandomDelayTimeMax is the upper limit.
	RandomDelayTimeMax int

	// RandomDelayTimeMin is the lower limit.
	RandomDelayTimeMin int

	// RemoveZeroValues indicates whether to remove zero values from the results.
	RemoveZeroValues bool

	// Writer is an optional writer to write results to.
	Writer io.Writer
}

Option for the concurrent loop.

type ResultCh added in v1.1.0

type ResultCh[T any] struct {
	Error  error
	Index  int
	Output T
}

ResultCh receives the result from the channel.

Jump to

Keyboard shortcuts

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