parallel

package
v0.6.7 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 3 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serial

func Serial[T any](ctx context.Context, parallelizable Interface[T], tasks int, option ...SerialOption) error

Serial schedules tasks in sequence and handles the execution result in sequence as well. If any task execution failed, it will terminate and return error immediately.

Types

type Interface

type Interface[T any] interface {
	// ParallelDo executes task within given routine in parallel.
	//
	// Note, this method is thread unsafe.
	ParallelDo(ctx context.Context, routine, task int) (T, error)

	// ParallelCollect handles execution result in a separate goroutine.
	//
	// Note, this method is thread safe.
	ParallelCollect(ctx context.Context, result *Result[T]) error
}

Interface defines methods to support parallel execution.

type Result

type Result[T any] struct {
	Routine int   // routine index
	Task    int   // task index
	Value   T     // task execution result
	Err     error // optional task execution error
}

Result represents a task execution result.

type SerialOption

type SerialOption struct {
	// Routines indicates the number of goroutines for parallel execution.
	//
	// By default, runtime.GOMAXPROCS(0) is used.
	Routines int

	// Window limits the maximum number of cached task execution result to handle in sequence.
	//
	// By default, 0 indicates no limit to execute all tasks as fast as it can.
	//
	// Otherwise, please set a proper value for some considerations, e.g.
	//  - Limits the memory usage to cache task execution results.
	//  - Business logic required.
	Window int
}

func (*SerialOption) Normalize

func (opt *SerialOption) Normalize(tasks int)

Jump to

Keyboard shortcuts

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