Documentation
¶
Overview ¶
Package terminator permits orderly stopping / shutdown of a group of goroutines via methods which mimic stop of a sync.WaitGroup.There is a default Terminator accessible through top level functions (Add, Done, Wait and so on) that call the corresponding Terminator methods
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ShutDown ¶
func ShutDown() <-chan struct{}
ShutDown allows code to wait for a shut down signal
Example (Background_worker) ¶
// --- Job processor ---
process := func(job string) {
fmt.Println(job)
}
// --- Background Worker ---
queue := make(chan string, 10)
worker := func(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("worker stopped")
return
case job := <-queue:
process(job)
}
}
}
// --- Launch the background worker inside a context.WithCancel ---
workerCtx, workerCancel := context.WithCancel(context.Background())
workerDone := make(chan struct{})
go func() {
defer close(workerDone)
worker(workerCtx)
}()
// --- Shutdown Manager ---
sm := NewShutdownManager(5 * time.Second)
// --- Register a drain function for the background worker
sm.Register(
"worker-drain",
20*time.Second,
func(ctx context.Context) error {
fmt.Println("draining worker")
workerCancel() // signal the worker to stop
select {
case <-workerDone:
return nil
case <-ctx.Done():
return fmt.Errorf("worker did not drain in time: %w", ctx.Err())
}
})
// Submit jobs to the worker queue
for i := range 5 {
queue <- fmt.Sprintf("%s %d", "job", i+1)
}
fmt.Println("jobs sent")
// Give jobs a chance then quit gracefully
time.Sleep(1 * time.Second)
if err := sm.Shutdown(); err != nil {
fmt.Println("shutdown completed with errors", "error", err)
}
Output: jobs sent job 1 job 2 job 3 job 4 job 5 draining worker worker stopped
Types ¶
type ShutdownManager ¶ added in v1.2.0
type ShutdownManager struct {
// contains filtered or unexported fields
}
ShutdownManager orchestrates ordered, phased shutdown.
func NewShutdownManager ¶ added in v1.2.0
func NewShutdownManager(total time.Duration) *ShutdownManager
func (*ShutdownManager) Register ¶ added in v1.2.0
func (s *ShutdownManager) Register(name string, timeout time.Duration, fn func(ctx context.Context) error)
Register adds a shutdown function to a named phase. Phases execute in registration order.
func (*ShutdownManager) Shutdown ¶ added in v1.2.0
func (s *ShutdownManager) Shutdown() error
Shutdown runs all phases sequentially with independent timeouts.
type Terminator ¶
type Terminator struct {
// contains filtered or unexported fields
}
Terminator manages groups of goroutines
func (*Terminator) Add ¶
func (t *Terminator) Add(delta int)
Add adds delta to the count of goroutines in the group
func (*Terminator) Done ¶
func (t *Terminator) Done()
Done decrements the count of goroutines in the group by one
func (*Terminator) Go ¶ added in v1.1.0
func (t *Terminator) Go(f func())
Go runs a function inside an Add(1) --- Done() sequence
func (*Terminator) ShutDown ¶
func (t *Terminator) ShutDown() <-chan struct{}
ShutDown allows code to wait for a shut down signal
func (*Terminator) ShuttingDown ¶
func (t *Terminator) ShuttingDown() bool
ShuttingDown returns true if shutdown is in Default().progress
func (*Terminator) Stop ¶
func (t *Terminator) Stop()
Stop signals that all goroutines in the group should safely exit
func (*Terminator) Wait ¶
func (t *Terminator) Wait()
Wait blocks until every goroutines in the group has called Done()