concurrentloop

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: May 20, 2023 License: MIT Imports: 4 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],
	opts ...Func,
) chan ResultCh[Result]

MapCh is a generic function in Go that applies a function concurrently to each element in a slice and sends the results via a channel. It uses a semaphore to control the concurrency level.

The function takes four parameters: - ctx: A context.Context which is used to control the cancellation of the computation. - concurrency: An int64 that represents the maximum number of concurrent goroutines that can run. If set to 0 or -1, it will use the runtime.NumCPU(). - sl: A slice of elements of type T. The function will be applied to each of these elements. - f: A MapFunc function that takes a context and an element of type T and returns a Result of type any and an error.

The function returns a channel which will receive a ResultCh struct containing three fields: - Index: The index of the input slice that the Result corresponds to. - Output: The Result of applying the function f to the corresponding element of the input slice. - Error: Any error that occurred while applying the function f.

The function uses a semaphore to limit the number of goroutines that can run concurrently. Each goroutine applies the function f to an element of the input slice and sends the result via the resultsCh channel. If the context is cancelled or the semaphore cannot be acquired, an error is sent via the resultsCh channel. If an error occurs while applying the function f, the error is also sent via the resultsCh channel.

NOTE: that the function does not close the resultsCh channel. It is the responsibility of the caller to ensure that all results are read from the channel to avoid a goroutine leak.

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,
	sl []T,
	f MapFunc[T, Result],
	opts ...Func,
) ([]Result, Errors)

Map calls the `Func` concurrently on each element of `sl`, and returns the results and any errors that occurred.

NOTE: For more info. see the `MapCh` function.

NOTE: Order is preserved.

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 WithConcurrency added in v1.1.7

func WithConcurrency(concurrency int) Func

WithConcurrency sets the OnFinished function.

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 Option added in v1.1.7

type Option struct {
	// Concurrency is the number of concurrent goroutines that will be used.
	Concurrency int
}

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