concurrentloop

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: MIT Imports: 2 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

This section is empty.

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 MapCh added in v1.1.1

func MapCh[T any, Result any](ctx context.Context, sl []T, f MapFunc[T, Result]) chan ResultCh[Result]

MapCh calls the `Func` concurrently on each element of `sl`, and returns a channel that receives the results. The results are returned as a `resultCh` struct, which contains the output value and an error value if the function call failed.

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

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.

func Map added in v1.1.1

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

Map calls the `Func` concurrently on each element of `sl`, and returns the results and any errors that occurred. The function blocks until all executions have completed.

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 MapFunc added in v1.1.1

type MapFunc[T any, Result any] func(context.Context, 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 ResultCh added in v1.1.0

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

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