completion

package
v1.21.0-pre.4 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 31

Documentation

Overview

Package completion implements a variant of sync.WaitGroup that is associated with a context.Context.

A WaitGroup is created by calling NewWaitGroup with the context:

wg := completion.NewWaitGroup(ctx)

For each concurrent computation to wait for, a Completion is created by calling AddCompletionWithCallback and then passing it to the concurrent computation:

comp1 := wg.AddCompletionWithCallback(nil, nil)
DoSomethingConcurrently(..., comp1)
comp2 := wg.AddCompletionWithCallback(nil, nil)
DoSomethingElse(..., comp2)

The Completion type provides the Complete and Completed() methods:

func (c *Completion) Complete()
func (c *Completion) Completed() <-chan struct{}

The Complete method must be called when the concurrent computation is completed, for instance:

func DoSomethingConcurrently(..., comp Completion) {
    ...
    go func() {
        ...
        // Computation is completed successfully.
        comp.Complete(nil)
    }()
    ...
}

Once all Completions are created, one can wait for the completion of all of the Completions by calling Wait:

err := wg.Wait()

Wait blocks until all Completions are completed, or the context is canceled or times out. A failed operation also cancels the context. Before returning, Wait settles any remaining Completions and drains the group.

With Completions, Wait returns nil if they all completed successfully, even if the context was canceled or timed out. Otherwise, it returns a completion error, overridden by the context error if Wait observes a timeout.

For an empty WaitGroup, Wait returns the context error, which is nil if the context has not been canceled or timed out. Wait cancels the context before returning, so subsequent calls return context.DeadlineExceeded if the context timed out, or context.Canceled otherwise.

If the caller abandons the operation before Wait, it must call Cancel instead. Cancel cancels the group's context and synchronously drains pending callbacks and owner cleanup through Wait, discarding its error. Canceling the context alone does not perform this cleanup. Registration must finish before either method is called. Neither may be called from a completion callback or owner cleanup hook, or while holding locks those hooks need.

A Completion can also be created with a callback, which is called at most once when the Completion is completed, including by Wait or Cancel:

comp := wg.AddCompletionWithCallback(nil, func(err error) {
    if err == nil {
        fmt.Println("completed')
    }
})

The callback is called in the goroutine which first completes it, either through Complete, Wait, or Cancel. The callback is called with an non-nil error if the associated concurrent operation fails. Error values 'context.DeadlineExceeded' and 'context.Canceled' are passed if the WaitGroup's context times out or is canceled. Note that the context is canceled also if any of the other completions in the WaitGroup fails, and the non-failing completions will have their callbacks called with 'context.Canceled'.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Completion

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

Completion provides the Complete callback to be called when an asynchronous computation is completed.

func NewCompletion

func NewCompletion(owner Owner, cancel context.CancelFunc, callback func(err error)) *Completion

NewCompletion creates a Completion which calls a function upon Complete(). 'cancel' is called if the associated operation fails for any reason.

func (*Completion) Complete

func (c *Completion) Complete(err error) error

Complete notifies of the completion of the asynchronous computation. Idempotent. If the operation completed successfully 'err' is passed as nil. Returns the error state the completion completed with, which is generally different from 'err' if already completed.

func (*Completion) Completed

func (c *Completion) Completed() <-chan struct{}

Completed returns a channel that's closed when the completion is completed, i.e. when Complete is called the first time, or when the call to the parent WaitGroup's Wait terminated because the context was canceled.

func (*Completion) Err

func (c *Completion) Err() error

Err returns a non-nil error if the completion ended in error

func (*Completion) Id added in v1.18.9

func (c *Completion) Id() string

type Owner added in v1.20.0

type Owner interface {
	ID() string
	CleanupAfterWait(*Completion)
}

Owner provides optional debug correlation and wait-time cleanup for a completion created with AddCompletionWithCallback.

type WaitGroup

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

WaitGroup waits for a collection of Completions to complete. Register all completions before calling Wait or Cancel; no more may be added afterward. Both methods synchronously run callbacks and owner cleanup, so they must not be called from those hooks or while holding locks the hooks need.

func NewWaitGroup

func NewWaitGroup(ctx context.Context) *WaitGroup

NewWaitGroup returns a new WaitGroup using the given context. Its internal context is canceled when a completion fails, Wait returns, or Cancel is called. Callers must eventually call Wait or Cancel to release resources and settle pending completions, even if the context has already been canceled.

func (*WaitGroup) AddCompletionWithCallback

func (wg *WaitGroup) AddCompletionWithCallback(owner Owner, callback func(err error)) *Completion

AddCompletionWithCallback creates a new completion, adds it to the wait group, and returns it. The callback will be called upon completion. Completion can complete in a failure (err != nil)

func (*WaitGroup) Cancel added in v1.17.15

func (wg *WaitGroup) Cancel()

Cancel cancels the underlying context and drains pending completions through Wait, which does not wait on a cancelled context. No completion callbacks or owner cleanup remain in progress when it returns. Cancel discards the wait error; a subsequent Wait cannot retrieve it. Harmless if called after Wait().

func (*WaitGroup) Wait

func (wg *WaitGroup) Wait() error

Wait blocks until all completions added to the wait group are completed, or the context is canceled or times out, whichever happens first. No callbacks of the completions in this wait group will be called after this returns. Returns any completion error if there are completions, overridden by context error if the context times out. The context error is returned for empty WaitGroups and for subsequent Wait calls. The context error is nil for the first Wait call if the context was not cancelled or timed out. Further calls return context.DeadlineExceeded if the context timed out, or context.Canceled otherwise.

Jump to

Keyboard shortcuts

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