concurrency

package
v1.20220526.1 Latest Latest
Warning

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

Go to latest
Published: May 26, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package concurrency contains utilities for writing concurrent Go code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool struct {
	Tasks []*Task
	// contains filtered or unexported fields
}

Pool is a worker group that runs a number of tasks at a configured concurrency.

func NewPool

func NewPool(tasks []*Task, concurrency int) *Pool

NewPool initializes a new pool with the given tasks and at the given concurrency.

func (*Pool) Run

func (p *Pool) Run() error

Run runs all work within the pool and blocks until it's finished. Upon completion, the task errors will be collected into a multi-error.

type Task

type Task struct {
	// Error holds an error that occurred during a task. Its
	// result is only meaningful after Run has been called
	// for the pool that holds it.
	Error error
	// contains filtered or unexported fields
}

Task encapsulates a work item that should go in a worker pool.

If a task is run more than once, the `Error` will be overwritten.

func NewTask

func NewTask(f func(i int) error) *Task

NewTask creates a new task based on a given work function.

func (*Task) Run

func (t *Task) Run(wg *sync.WaitGroup, i int)

Run runs a Task, stores any error and marks the passed in work group as done.

This modifies the current task in a way that is not concurrency safe; the expectation is that a given task will be handled by / run in a single goroutine.

Jump to

Keyboard shortcuts

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