parallel

package
v5.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package parallel provides utilities for executing independent operations concurrently with controlled parallelism using errgroup and semaphore.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultMaxConcurrency

func DefaultMaxConcurrency() int64

DefaultMaxConcurrency returns the default maximum concurrency based on available CPUs.

Types

type Executor

type Executor struct {
	// contains filtered or unexported fields
}

Executor provides controlled parallel execution of tasks.

func NewExecutor

func NewExecutor(maxConcurrency int64) *Executor

NewExecutor creates a new parallel executor with the specified max concurrency. If maxConcurrency <= 0, DefaultMaxConcurrency() is used.

func (*Executor) Execute

func (executor *Executor) Execute(ctx context.Context, tasks ...Task) error

Execute runs all tasks concurrently with controlled parallelism. It returns the first error encountered, canceling remaining tasks. If all tasks succeed, it returns nil.

type Results

type Results[T any] struct {
	// contains filtered or unexported fields
}

Results collects results from parallel tasks with thread-safe access.

func NewResults

func NewResults[T any]() *Results[T]

NewResults creates a new Results collector.

func (*Results[T]) Add

func (results *Results[T]) Add(value T)

Add appends a result value.

func (*Results[T]) AddError

func (results *Results[T]) AddError(err error)

AddError appends an error.

func (*Results[T]) Errors

func (results *Results[T]) Errors() []error

Errors returns all collected errors.

func (*Results[T]) HasErrors

func (results *Results[T]) HasErrors() bool

HasErrors returns true if any errors were collected.

func (*Results[T]) Values

func (results *Results[T]) Values() []T

Values returns all collected values.

type SyncWriter

type SyncWriter struct {
	// contains filtered or unexported fields
}

SyncWriter is a thread-safe writer that serializes writes from multiple goroutines.

func NewSyncWriter

func NewSyncWriter(writer io.Writer) *SyncWriter

NewSyncWriter creates a new synchronized writer wrapping the given writer.

func (*SyncWriter) Write

func (syncWriter *SyncWriter) Write(data []byte) (int, error)

Write writes data to the underlying writer with synchronization.

type Task

type Task func(ctx context.Context) error

Task represents a unit of work that can be executed in parallel.

Jump to

Keyboard shortcuts

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