xsync

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ParallelMapper

type ParallelMapper[Input, Output any] interface {
	// Map applies the given mapper to each element of the input slice in parallel.
	// If the context expires before all the mappers are finished, the remaining mappers are cancelled.
	// If a mapper fails, the error is returned in the errs slice.
	// If a mapper succeeds, the result is returned in the results slice.
	// FUnction returns an error if thera is problem not related to the mapper function.
	Map([]Input) error

	// Results returns the results of the last call to Map.
	Results() []Output

	// Errors returns the errors of the last call to Map.
	Errors() []error
}

func NewParallelMapper

func NewParallelMapper[Input, Output any](worker func(Input) (Output, error), options ...ParallelOption) ParallelMapper[Input, Output]

NewParallelMapper returns a mapper hat applies the given mapper to each element of the input slice in parallel with at most maxWorkers in parallel. The results and errors are returned in the same order as the slice. If a maxWait is configured and the context expires before all the mappers are finished, the remaining mappers are cancelled.

If a mapper fails, the error is returned in the errs slice. If a mapper succeeds, the result is returned in the results slice.

Example:

   values := []int{1, 2, 3, 4, 5}
   mapper := NewParallelMapper(func(ctx context.Context, v V) (T, error) {
	    return v * v, nil
   }, WithMaxWait( 20 * time.Second), WithMaxWorkers(2))

  errs := mapper.Map(values)
  results := mapper.Results()
  errs := mapper.Errors()

type ParallelOption

type ParallelOption func(*parallelMapperOptions)

func WithMaxWait

func WithMaxWait(maxWait time.Duration) ParallelOption

WithMaxWait configures the maximum time to wait for all the mappers to finish.

func WithMaxWorkers

func WithMaxWorkers(maxWorkers int) ParallelOption

WithMaxWorkers configures the maximum number of workers to use.

Jump to

Keyboard shortcuts

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