ctxinterrupt

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2025 License: MIT Imports: 5 Imported by: 13

Documentation

Overview

Implements interrupts: events that normally signal intent to cancel a Context, but may be repeated to encourage closure of new Contexts used to clean up resources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wait

func Wait(ctx context.Context) error

Wait blocks until an interrupt is received, defaulting to interrupting on the default signals if no interrupt blocker is present in the Context. Returns nil if an interrupt occurs, else the Context error when it's done.

func WithCancelOnInterrupt

func WithCancelOnInterrupt(ctx context.Context) context.Context

WithCancelOnInterrupt returns a Context that is cancelled when Wait returns on the waiter in ctx. If there's no waiter, the default interrupt signals are used: In this case the signal hooking is not stopped until the original ctx is cancelled.

func WithSignalWaiter

func WithSignalWaiter(ctx context.Context) (_ context.Context, stop func())

WithSignalWaiter attaches an interrupt signal handler to the context which continues to receive signals after every wait, and also prevents the interrupt signals being handled before we're ready to wait for them. This helps functions wait on individual consecutive interrupts.

func WithSignalWaiterMain

func WithSignalWaiterMain(ctx context.Context) context.Context

WithSignalWaiterMain returns a Context with a signal interrupt blocker and leaks the destructor. Intended for use in main functions where we exit right after using the returned context anyway.

func WithWaiterFunc

func WithWaiterFunc(ctx context.Context, fn WaiterFunc) context.Context

WithInterruptWaiter overrides the interrupt waiter value, e.g. to insert a function that mocks interrupt signals for testing CLI shutdown without actual process signals.

Types

type WaiterFunc

type WaiterFunc func(ctx context.Context) (interrupt, ctxErr error)

Waits for an interrupt or context cancellation. ctxErr should be the context.Cause of ctx when it is done. interrupt is only inspected if ctxErr is nil, and is not required to be set.

Jump to

Keyboard shortcuts

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