terminator

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: MIT Imports: 10 Imported by: 3

README

Go Reference Go Report Card

terminator

import "github.com/bruceesmith/terminator"

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

func Add

func Add(delta int)

Add adds delta to the count of goroutines in the group

func Done

func Done()

Done decrements the count of goroutines in the group by one

func Go

func Go(f func())

Go runs a function inside an Add(1) --- Done() sequence

func SetDefault

func SetDefault(t *Terminator)

SetDefault sets the default Terminator

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
Output
jobs sent
job 1
job 2
job 3
job 4
job 5
draining worker
worker stopped

func ShuttingDown

func ShuttingDown() bool

ShuttingDown returns true if shutdown is in progress

func Stop

func Stop()

Stop signals that all goroutines in the group should safely exit

func Wait

func Wait()

Wait blocks until every goroutines in the group has called Done()

type ShutdownManager

ShutdownManager orchestrates ordered, phased shutdown.

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

func NewShutdownManager
func NewShutdownManager(total time.Duration) *ShutdownManager

func (*ShutdownManager) Register
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
func (s *ShutdownManager) Shutdown() error

Shutdown runs all phases sequentially with independent timeouts.

type Terminator

Terminator manages groups of goroutines

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

func Default
func Default() *Terminator

Default returns the default Terminator.

func New
func New() *Terminator

New returns a Terminator

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
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()

Generated by gomarkdoc

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 Add

func Add(delta int)

Add adds delta to the count of goroutines in the group

func Done

func Done()

Done decrements the count of goroutines in the group by one

func Go added in v1.1.0

func Go(f func())

Go runs a function inside an Add(1) --- Done() sequence

func SetDefault

func SetDefault(t *Terminator)

SetDefault sets the default Terminator

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

func ShuttingDown

func ShuttingDown() bool

ShuttingDown returns true if shutdown is in progress

func Stop

func Stop()

Stop signals that all goroutines in the group should safely exit

func Wait

func Wait()

Wait blocks until every goroutines in the group has called Done()

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 Default

func Default() *Terminator

Default returns the default Terminator.

func New

func New() *Terminator

New returns a Terminator

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()

Jump to

Keyboard shortcuts

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