Documentation
¶
Overview ¶
Package threadgroup provides a utility for performing organized clean shutdown and quick shutdown of long running groups of threads, such as networking threads, background threads, or resources like databases.
The OnStop and AfterStop functions are helpers which enable shutdown code to be inlined with resource allocation, similar to defer. The difference is that `OnStop` and `AfterStop` will be called following tg.Stop, instead of when the parent function goes out of scope.
Index ¶
- Variables
- type ThreadGroup
- func (tg *ThreadGroup) Add() error
- func (tg *ThreadGroup) AfterFunc(duration time.Duration, fn func())
- func (tg *ThreadGroup) AfterStop(fn func() error) error
- func (tg *ThreadGroup) Done()
- func (tg *ThreadGroup) Launch(fn func()) error
- func (tg *ThreadGroup) OnStop(fn func() error) error
- func (tg *ThreadGroup) Sleep(d time.Duration) bool
- func (tg *ThreadGroup) Stop() error
- func (tg *ThreadGroup) StopChan() <-chan struct{}
- func (tg *ThreadGroup) StopCtx() context.Context
Constants ¶
This section is empty.
Variables ¶
var ErrStopped = errors.New("ThreadGroup already stopped")
ErrStopped is returned by ThreadGroup methods if Stop has already been called.
Functions ¶
This section is empty.
Types ¶
type ThreadGroup ¶
type ThreadGroup struct {
// contains filtered or unexported fields
}
A ThreadGroup is a one-time-use object to manage the life cycle of a group of threads. It is a sync.WaitGroup that provides functions for coordinating actions and shutting down threads. After Stop() is called, the thread group is no longer useful.
It is safe to call Add(), Done(), and Stop() concurrently.
func (*ThreadGroup) Add ¶
func (tg *ThreadGroup) Add() error
Add increments the thread group counter.
func (*ThreadGroup) AfterFunc ¶
func (tg *ThreadGroup) AfterFunc(duration time.Duration, fn func())
AfterFunc works like time.AfterFunc, except that it will automatically cancel the function if tg.Stop() is called. The thread group uses timers and time.AfterFunc for maximal memory safety and efficiency.
func (*ThreadGroup) AfterStop ¶
func (tg *ThreadGroup) AfterStop(fn func() error) error
AfterStop ensures that a function will be called after Stop() has been called and after all running routines have called Done(). The functions will be called in reverse order to how they were added, similar to defer. If Stop() has already been called, the input function will not be run and an error will be returned.
The primary use of AfterStop is to allow code that opens and closes resources to be positioned next to each other. The purpose is similar to `defer`, except for resources that outlive the function which creates them.
func (*ThreadGroup) Launch ¶
func (tg *ThreadGroup) Launch(fn func()) error
Launch will launch a goroutine with the thread group.
Correct use looks like this:
err := tg.Launch(fn) if err != nil { // handle err, fn was not executed. }
This is equivalent to writing:
err := tg.Add() if err != nil { return err } go func() { fn() tg.Done() } return nil This is a convenience function which makes it more difficult to misuse tg.Add and tg.Done when launching new threads.
func (*ThreadGroup) OnStop ¶
func (tg *ThreadGroup) OnStop(fn func() error) error
OnStop ensures that a function will be called after Stop() has been called, and before blocking until all running routines have called Done(). It is safe to use OnStop to coordinate the closing of long-running threads. The OnStop functions will be called in the reverse order in which they were added, similar to defer. If Stop() has already been called, the input function will not be called, and ErrStopped will be returned.
func (*ThreadGroup) Sleep ¶
func (tg *ThreadGroup) Sleep(d time.Duration) bool
Sleep will sleep for the provided duration. If the threadgroup is stopped before the duration is complete, Sleep will return early with the value 'false', indicating that shutdown has occurred. Otherwise Sleep will return after the given duration with a value of 'true'.
Sleep uses a timer and cleans up correctly to ensure there is no time.After() related memory leak.
func (*ThreadGroup) Stop ¶
func (tg *ThreadGroup) Stop() error
Stop will close the stop channel of the thread group, then call all 'OnStop' functions in reverse order, then will wait until the thread group counter reaches zero, then will call all of the 'AfterStop' functions in reverse order.
The errors returned by the OnStop and AfterStop functions will be composed into a single error.
func (*ThreadGroup) StopChan ¶
func (tg *ThreadGroup) StopChan() <-chan struct{}
StopChan provides read-only access to the ThreadGroup's stopChan. Callers should select on StopChan in order to interrupt long-running reads (such as time.After).
func (*ThreadGroup) StopCtx ¶
func (tg *ThreadGroup) StopCtx() context.Context
StopCtx returns the threadgroups underlying context.