run

package
v0.0.0-...-3bfe646 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: Apache-2.0 Imports: 3 Imported by: 2

Documentation

Overview

Copyright 2016 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

Package run provides synchronization, error propagation, and Context cancelation for groups of goroutines working on subtasks of a common task. The package is based on golang.org/x/sync/errgroup and adds concurrency limits on top.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

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

A Group is a collection of goroutines working on subtasks that are part of the same overall task. The parallelization of tasks is controlled by a semaphore that is either unrestricted (allows unlimited number of concurrent tasks) or limits them to a certain amount subject to implemented strategy.

A zero Group is valid and does not cancel on error.

func WithContext

func WithContext(ctx context.Context, options ...Option) (*Group, context.Context)

WithContext returns a new group with the specified concurrency configuration.

func (*Group) Go

func (r *Group) Go(ctx context.Context, fn func() error)

Go calls the given function in a new goroutine. The call to Go might block if there're already as many tasks running as configured by r.parallel.

The first call to return a non-nil error cancels the group; its error will be returned by Wait.

func (*Group) Wait

func (r *Group) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns the first non-nil error (if any) from them.

type Option

type Option func(group *Group)

Option is a configuration option for Group

func WithCPU

func WithCPU() Option

WithCPU creates a new semaphore that executes as many tasks as there're CPU cores

func WithParallel

func WithParallel(parallel int) Option

WithParallel creates a new semaphore that caps the number of tasks to the specified value.

If parallel < 0, then the tasks are not capped. If parallel == 0, then the behaviour is as with parallel == 1 If parallel > 0, then the specified number of tasks is allowed to run concurrently

Jump to

Keyboard shortcuts

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