limited

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 3 Imported by: 1

README

Limited Group

Go Reference

This module (github.com/tschaub/limited) provides utilities for spawning a limited number of goroutines. It is intended to provide a stop-gap solution until the golang.org/x/sync/errgroup provides similar functionality (see https://github.com/golang/go/issues/27837).

Installation

go get github.com/tschaub/limited

Use

See the reference documentation for example usage.

Documentation

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.

(A zero Group is not valid, use the WithContext function.)

func WithContext

func WithContext(ctx context.Context, limit int) (*Group, context.Context)

WithContext returns a new Group and an associated Context derived from ctx.

The derived Context is canceled the first time a function passed to Go returns a non-nil error or the first time Wait returns, whichever occurs first. No more than limit goroutines will be created at a time.

func (*Group) Go

func (g *Group) Go(f func() error) error

Go calls the given function in a new goroutine.

The first call to return a non-nil error cancels the group; its error will be returned by Wait. The function will block the number of current goroutines started by the group exceeds the limit. Any returned error will typically be due to context cancellation.

func (*Group) Wait

func (g *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.

Jump to

Keyboard shortcuts

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