lifecycle

package
v0.1.79 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package lifecycle contains helpers which manage context.Context instances.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDone

func IsDone(ctx context.Context) (done bool)

IsDone is a helper which checks <-ctx.Done().

Types

type CGroup

type CGroup interface {
	// Adds the context to this group.
	// Returns true if the underlying context is not yet done and this context was added to the active set, even briefly.
	Add(c context.Context) (ok bool)

	// Starts or retrieves the prior context for this CGroup.
	// This will be already canceled if no contexts were added.
	Start() (ctx context.Context)

	// Wait ensures this group has started, and then blocks until the [context.Context] is completed, returning its cause if not the default [context.Canceled].
	Wait() (err error)

	// Go runs the given method as part of this group.
	// It will only start after Start() has been called with valid contexts.
	// Any returned error will cancel the group's [context.Context] directly, rather than with any provided cause.
	// Returns true if the method has a chance of running.
	Go(fn func(c context.Context) (err error)) (ok bool)

	// Halt runs the given method when this group may be about to shut down.
	//
	// It is passed a channel which is closed if the group restarts.
	// The method passed here should prevent further Add() calls in your code before doing teardown work.
	// Otherwise, you risk a resume race.
	//
	// The passed method will only run after a successful Start() and then a potential shutdown.
	// Any returned error will cancel the group's [context.Context] directly, rather than with any provided cause.
	// Returns true if the method has a chance of running.
	Halt(fn func(c context.Context, resume <-chan struct{}) (err error)) (ok bool)
}

CGroup provides a context.Context while any contained context is active.

func NewCGroup

func NewCGroup() (cg CGroup)

NewCGroup creates a new CGroup, which simply provides a context.Context while any passed context is active. The new context is not derived from anything.

func NewCGroupCause

func NewCGroupCause(cause error) (cg CGroup)

NewCGroupCause creates a new CGroup that will eventually cancel with the specified cause.

type Check

type Check[T comparable] func(ctx context.Context, token T) (err error)

Check checks the given token. If the func is nil, treat as returning nil err.

type TGroup

type TGroup[T comparable] interface {
	// Provide provides a context tagged with a unique T.
	// Calling Provide with the same T unions the lifecycle of the context.
	Provide(token T, ctx context.Context) (ok bool)

	// Revoke revokes a prior T early, ignoring its [context.Context].
	Revoke(token T) (ok bool)

	// Access provides a derived context that is valid while any T passes the given check, and has a valid context.
	Access(parent context.Context, check Check[T]) (derived context.Context)

	// Permit performs a one-time check to determine whether any T passes the given check.
	Permit(parent context.Context, check Check[T]) (err error)
}

TGroup provides context.Context if tokens pass check functions.

func NewTGroup

func NewTGroup[T comparable]() (t TGroup[T])

TGroup creates a new TGroup for the given T.

type WorkerFunc added in v0.1.79

type WorkerFunc[E any] func(ctx context.Context, events iter.Seq[E]) (err error)

WorkerFunc runs a task. The passed iterator can only be called once, or it will panic.

type WorkerStatus added in v0.1.79

type WorkerStatus interface {
	// Ready returns a channel which is closed when this Worker's iterator has been started.
	// This may never be closed.
	Ready() (ch <-chan struct{})

	// Idle returns a unique channel which is closed when this Worker is no longer reading its iterator.
	// It yields true if Ready() was previously closed.
	Idle() (ch <-chan bool)

	// Done returns a unique channel which is closed when this Worker is finally shutdown.
	// This returns any error reason (context or direct).
	Done() (ch <-chan error)
}

func Worker added in v0.1.79

func Worker[E any](ctx context.Context, dataCh <-chan E, fn WorkerFunc[E]) (st WorkerStatus)

Worker runs a task that processes data from a channel and reports its lifecycle status.

Jump to

Keyboard shortcuts

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