workerpool

package
v0.0.0-...-4eb781d Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: MIT Imports: 3 Imported by: 0

README

workerpool

Used to execute tasks with a concurrent limit. The worker pool starts executing as soon as work is submitted. If an error occurs during a piece of work, the worker pool stops executing all work and returns an error.

Example

Here a worker pool executes max 2 requests concurrently.

func (a *App) getIDs(ctx context.Context, ids []string) error {
	wp := workerpool.New(ctx, 2)

	for _, id := range ids {
		id := id
		wp.Do(func(ctx context.Context) {
			resp, err := a.ServiceClient.GetIDs(ctx, id)
			if err != nil {
				return err
			}

			// store response in concurrency safe store
		})
	}

	return wp.Wait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Worker

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

func New

func New(ctx context.Context, workers int) *Worker

func (*Worker) Do

func (w *Worker) Do(fns ...WorkerFunc)

func (*Worker) Wait

func (w *Worker) Wait() error

type WorkerFunc

type WorkerFunc func(context.Context) error

Jump to

Keyboard shortcuts

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