Documentation
¶
Overview ¶
Package proc provides a small work/goroutine coordination helper for graceful shutdown.
Index ¶
- Variables
- func Term() <-chan struct{}
- func TermContext() context.Context
- type Manager
- func (m *Manager) Add() Worker
- func (m *Manager) Close(fn func() error) error
- func (m *Manager) CloseContext(ctx context.Context, fn func() error) error
- func (m *Manager) CloseTimeout(duration time.Duration, fn func() error) error
- func (m *Manager) Context(parent context.Context) context.Context
- func (m *Manager) Count() int
- func (m *Manager) Done() <-chan struct{}
- func (m *Manager) Go(fn func())
- func (m *Manager) Once(fn func() error) (err error)
- func (m *Manager) Stop()
- func (m *Manager) Wait()
- func (m *Manager) WaitContext(ctx context.Context) error
- func (m *Manager) WaitTimeout(duration time.Duration) error
- func (m *Manager) Watch(ch <-chan struct{})
- func (m *Manager) WatchContext(ctx context.Context)
- type Worker
Constants ¶
This section is empty.
Variables ¶
var ErrTimeout = errors.New("timed out")
Functions ¶
func Term ¶
func Term() <-chan struct{}
Term returns a channel that is closed when one of the stop signals is received: os.Interrupt, syscall.SIGTERM, syscall.SIGHUP.
Signal notifiers are not initialized until Term or TermContext are called.
func TermContext ¶
TermContext returns a context that is canceled when one of the stop signals is received: os.Interrupt, syscall.SIGTERM, syscall.SIGHUP.
Signal notifiers are not initialized until Term or TermContext are called.
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager coordinates graceful shutdown of workers and goroutines. The zero value is ready to use. A Manager must not be copied after first use.
func (*Manager) Add ¶
Add registers a unit of work with the Manager and returns a Worker handle. Users of the returned Worker must call Complete when their work is finished.
Add is safe to call even if the Manager is already stopped and its Wait method has already been called.
If the Manager is already stopped, the returned Worker is disabled: its methods will return closed channels and canceled contexts.
func (*Manager) Close ¶
Close is a shorthand for Stop, Wait, and Once. See mentioned methods for details.
The provided fn can be nil.
func (*Manager) CloseContext ¶
CloseContext is a shorthand for Stop, WaitContext and Once. See mentioned methods for details.
Once is called regardless of the error returned by WaitContext. The error returned by CloseContext is either from Once or from WaitContext in that particular order.
The provided fn can be nil.
func (*Manager) CloseTimeout ¶
CloseTimeout is a shorthand for Stop, WaitTimeout and Once. See mentioned methods for details.
Once is called regardless of the error returned by WaitTimeout. The error returned by CloseTimeout is either from Once or from WaitTimeout in that particular order.
The provided fn can be nil.
func (*Manager) Context ¶
Context returns a context that is canceled when the Manager is stopped, or when the parent context is canceled or expires.
func (*Manager) Done ¶
func (m *Manager) Done() <-chan struct{}
Done returns a channel that is closed when the Manager is stopped.
func (*Manager) Go ¶
func (m *Manager) Go(fn func())
Go calls fn in a new goroutine and increases the number of active workers. When fn returns, the number of active workers is decreased. If the Manager is already stopped, fn is not called.
func (*Manager) Once ¶
Once calls the provided fn exactly once. Subsequent calls to Once are no-ops. If fn is nil, Once ignores it but still counts as performed.
func (*Manager) Wait ¶
func (m *Manager) Wait()
Wait waits until the Manager is stopped and then until all workers report completion.
Wait is not analogous to sync.WaitGroup's Wait, it will not return even if all the workers report completion but the Manager is still in active state (not stopped).
func (*Manager) WaitContext ¶
WaitContext waits until Manager is stopped and then until all workers report completion. If the provided context is canceled or expires during any of these steps, WaitContext returns ctx.Err().
WaitContext is not analogous to sync.WaitGroup's Wait, it will not return even if all the workers report completion but the Manager is still in active state (not stopped).
WaitContext may leak a goroutine if ctx is done and the Manager is never stopped.
func (*Manager) WaitTimeout ¶
WaitTimeout waits until Manager is stopped and then until all workers report completion. If the provided timeout expires during any of these steps, WaitTimeout returns ErrTimeout.
WaitTimeout is not analogous to sync.WaitGroup's Wait, it will not return even if all the workers report completion but the Manager is still in active state (not stopped).
WaitTimeout may leak a goroutine if the timeout expires and the Manager is never stopped.
func (*Manager) Watch ¶
func (m *Manager) Watch(ch <-chan struct{})
Watch stops the Manager when the provided channel is closed or a value is received from it.
func (*Manager) WatchContext ¶
WatchContext stops the Manager when the provided ctx is done.
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker represents a registered unit of work tied to a Manager.
func (Worker) Complete ¶
func (w Worker) Complete()
Complete reports that the unit of work is finished. It must be called exactly once. If the Worker was created from a stopped Manager, Complete is a no-op.
func (Worker) Context ¶
Context returns a context that is canceled when the worker is stopped or the provided parent is canceled or expires.
If the Worker was created from an already stopped Manager, Context returns a canceled context.
func (Worker) Disabled ¶
Disabled reports whether the Worker was created from a stopped Manager. It only checks a single pointer and is a bit faster than Stopped.