Documentation
¶
Overview ¶
Package nest provides a guarded, derivable goroutine lifecycle boundary.
A Nest tracks spawned workers via sync.WaitGroup, separates Parent (inherited) from Children (dispatched) context, and supports graceful Cancel with optional shutdown timeout.
Usage:
n := nest.New(ctx, nest.WithShutdownTimeout(5*time.Second))
n.Spawn(func(ctx context.Context) {
doWork(ctx)
})
n.Cancel(nil)
<-n.Done()
For job queues and parallel scheduling, use package schedx instead.
Code generated by genx:code@v0.3.7 DO NOT EDIT.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Nest ¶
type Nest interface {
context.Context
Parent() context.Context
Children() context.Context
Spawn(func(context.Context)) error
Cancel(error)
Canceled() bool
}
Example ¶
outside, cancel := context.WithCancelCause(context.Background())
defer cancel(nil)
ctx, cancel2 := context.WithCancel(outside)
defer cancel2()
fmt.Println("==> worker was scheduled before nest.Cancel")
n := NewAndSpawn(ctx, worker1, 1, 1)
time.Sleep(3 * unit)
n.Cancel(nil)
fmt.Printf("Closed: %v\n\n", n.Err())
fmt.Println("==> worker was not scheduled before nest.Cancel")
n = NewAndSpawn(ctx, worker1, 2, 1)
time.Sleep(1 * unit)
n.Cancel(nil)
fmt.Printf("Closed: %v\n\n", n.Err())
fmt.Println("==> without shutdown timeout")
n = NewAndSpawn(ctx, worker2, 5, 0)
time.Sleep(8 * unit)
n.Cancel(nil)
fmt.Printf("Closed: %v\n\n", n.Err())
fmt.Println("==> nest close timeout")
n = NewAndSpawn(ctx, worker2, 5, 1)
n.Cancel(errors.New("cause"))
fmt.Printf("Closed: %v\n\n", n.Err())
time.Sleep(6 * unit)
fmt.Println("==> shutdown triggered by outside nest.Cancel")
n = NewAndSpawn(ctx, worker2, 5, 1)
cancel(errors.New("outside"))
<-n.Done()
fmt.Printf("Closed: %v\n\n", n.Err())
time.Sleep(6 * unit)
_ = 1
Output: ==> worker was scheduled before nest.Cancel Worker 1 finished task Worker 2 finished task Worker 3 finished task Before done After: <nil> Closed: <nil> ==> worker was not scheduled before nest.Cancel Before done Worker 1 canceled Worker 2 canceled Worker 3 canceled After: <nil> Closed: <nil> ==> without shutdown timeout Worker 1 finished task Worker 2 finished task Worker 3 finished task Before done After: <nil> Closed: <nil> ==> nest close timeout Before done After: [NEST::ERROR:2] NEST_CLOSE_TIMEOUT cause Closed: [NEST::ERROR:2] NEST_CLOSE_TIMEOUT cause Worker 1 finished task Worker 2 finished task Worker 3 finished task ==> shutdown triggered by outside nest.Cancel Before done After: [NEST::ERROR:2] NEST_CLOSE_TIMEOUT outside Closed: [NEST::ERROR:2] NEST_CLOSE_TIMEOUT outside Worker 1 finished task Worker 2 finished task Worker 3 finished task
type OptionApplier ¶
type OptionApplier func(*option)
func WithAfterCloseFunc ¶
func WithAfterCloseFunc(afterCloseFunc func(error) error) OptionApplier
WithAfterCloseFunc registers a post-close hook. ensures afterCloseFunc is invoked after all goroutines exited, providing synchronous waiting for Nest.Cancel. The err parameter passed to indicates why the shutdown was triggered (eg: context.Cause).
func WithBeforeCloseFunc ¶
func WithBeforeCloseFunc(beforeCloseFunc func(context.Context)) OptionApplier
WithBeforeCloseFunc registers a hook that executes before Nest.Cancel process begins. The provided context is canceled either when all goroutines have finished or once the shutdownTimeout is reached if configurated. This is typically used to signal or wake up suspended goroutines (e.g., via sync.Cond). Note: The implementation of this function must decide whether to operate synchronously or asynchronously.
func WithShutdownTimeout ¶
func WithShutdownTimeout(timeout time.Duration) OptionApplier