...

Package par

import "cmd/go/internal/par"
Overview
Index

Overview ▾

Package par implements parallel execution helpers.

Variables

var ErrCacheEntryNotFound = errors.New("cache entry not found")

type Cache

Cache runs an action once per key and caches the result.

type Cache[K comparable, V any] struct {
    // contains filtered or unexported fields
}

func (*Cache[K, V]) Do

func (c *Cache[K, V]) Do(key K, f func() V) V

Do calls the function f if and only if Do is being called for the first time with this key. No call to Do with a given key returns until the one call to f returns. Do returns the value returned by the one call to f.

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, bool)

Get returns the cached result associated with key and reports whether there is such a result.

If the result for key is being computed, Get does not wait for the computation to finish.

type ErrCache

ErrCache is like Cache except that it also stores an error value alongside the cached value V.

type ErrCache[K comparable, V any] struct {
    Cache[K, errValue[V]]
}

func (*ErrCache[K, V]) Do

func (c *ErrCache[K, V]) Do(key K, f func() (V, error)) (V, error)

func (*ErrCache[K, V]) Get

func (c *ErrCache[K, V]) Get(key K) (V, error)

Get returns the cached result associated with key. It returns ErrCacheEntryNotFound if there is no such result.

type Queue

Queue manages a set of work items to be executed in parallel. The number of active work items is limited, and excess items are queued sequentially.

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

func NewQueue

func NewQueue(maxActive int) *Queue

NewQueue returns a Queue that executes up to maxActive items in parallel.

maxActive must be positive.

func (*Queue) Add

func (q *Queue) Add(f func())

Add adds f as a work item in the queue.

Add returns immediately, but the queue will be marked as non-idle until after f (and any subsequently-added work) has completed.

func (*Queue) Idle

func (q *Queue) Idle() <-chan struct{}

Idle returns a channel that will be closed when q has no (active or enqueued) work outstanding.

type Work

Work manages a set of work items to be executed in parallel, at most once each. The items in the set must all be valid map keys.

type Work[T comparable] struct {
    // contains filtered or unexported fields
}

func (*Work[T]) Add

func (w *Work[T]) Add(item T)

Add adds item to the work set, if it hasn't already been added.

func (*Work[T]) Do

func (w *Work[T]) Do(n int, f func(item T))

Do runs f in parallel on items from the work set, with at most n invocations of f running at a time. It returns when everything added to the work set has been processed. At least one item should have been added to the work set before calling Do (or else Do returns immediately), but it is allowed for f(item) to add new items to the set. Do should only be used once on a given Work.