runactor

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package runactor composes long-running components under one signal story: named actors run concurrently, the first exit (or failure) ends the whole group, teardown runs LIFO, and an interrupt actor turns SIGINT/SIGTERM into graceful cancellation — a second signal exits hard.

Stdlib + golang.org/x/sync/errgroup only (ADR-0004's framework-free stance). This is the pattern steal from cordis's run.Group, shaped so the drain invariant stays expressible: an actor's context is its shutdown signal, NEVER its task-execution context (see ExecutionScope).

Index

Constants

This section is empty.

Variables

View Source
var ErrInterrupted = errors.New("interrupted")

ErrInterrupted is the group's cancellation cause when a signal stopped it. Run reports nil for a plain interrupt (graceful shutdown), so this exists for callers that need to distinguish causes via errors.Is on context.Cause.

Functions

func ExecutionScope

func ExecutionScope(parent context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

ExecutionScope derives a task-execution context that shutdown NEVER cancels — bounded only by the per-task deadline. This makes the drain invariant structural (the stranded-in-running lesson): a Ctrl-C lets in-flight tasks finish and record their outcome; the pool's shutdown context stops the LOOP, not the work.

Types

type ExitCause

type ExitCause struct {
	Actor string
	Err   error
}

ExitCause explains why a group ended; it names the actor whose exit decided it.

func (ExitCause) Error

func (e ExitCause) Error() string

func (ExitCause) Unwrap

func (e ExitCause) Unwrap() error

type Group

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

Group runs named actors until one of them returns, then cancels the rest and tears down in reverse registration order.

func New

func New(parent context.Context) *Group

New builds a Group over parent. Call Go/InterruptOn/OnShutdown, then Run.

func (*Group) Ctx

func (g *Group) Ctx() context.Context

Ctx is the actors' shutdown context: cancelled when the group ends (signal, actor exit, or parent death).

func (*Group) Go

func (g *Group) Go(name string, fn func(ctx context.Context) error)

Go runs fn as a named actor. The FIRST actor to return decides the program: an error is the group's cause (surfaced by Run); a clean return while the group is still healthy is a graceful stop.

func (*Group) InterruptOn

func (g *Group) InterruptOn(sig ...os.Signal)

InterruptOn cancels the group on any of the signals; a second signal exits the process hard (exit code 130) — the operator's escape hatch when graceful drain hangs.

func (*Group) OnShutdown

func (g *Group) OnShutdown(fn func() error)

OnShutdown registers a teardown step; steps run LIFO after every actor has stopped. Use for Close-ordered resources (store last to close).

func (*Group) Run

func (g *Group) Run() error

Run blocks until every actor has returned, then tears down LIFO. It returns the decisive error (nil for a signal or clean exit), joined with any teardown errors.

Jump to

Keyboard shortcuts

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