Documentation
¶
Overview ¶
Package closer offers a simple, thread-safe closer.
It allows to build up a tree of closing relationships, where you typically start with a root closer that branches into different children and children's children. When a parent closer spawns a child closer, the child either has a one-way or two-way connection to its parent. One-way children are closed when their parent closes. In addition, two-way children also close their parent, if they are closed themselves.
A closer is also useful to ensure that certain dependencies, such as network connections, are reliably taken down, once the closer closes. In addition, a closer can be concurrently closed many times, without closing more than once, but still returning the errors to every caller.
This allows to represent complex closing relationships and helps avoiding leaking goroutines, gracefully shutting down, etc.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Closer ¶
type Closer interface {
// Close closes this closer in a thread-safe manner.
//
// Implements the io.Closer interface.
//
// This method returns always the close error, regardless of how often
// it gets called. Close blocks, until all close functions are done,
// no matter which goroutine called this method.
// Returns a hashicorp multierror.
Close() error
// CloseAndDone performs the same operation as Close(), but decrements
// the closer's wait group by one beforehand.
// Attention: Calling this without first adding to the WaitGroup by
// calling AddWaitGroup() results in a panic.
CloseAndDone() error
// ClosedChan returns a channel, which is closed as
// soon as the closer is completely closed.
ClosedChan() <-chan struct{}
// CloserAddWait adds the given delta to the closer's
// wait group. Useful to wait for routines associated
// with this closer to gracefully shutdown.
CloserAddWait(delta int)
// CloserDone decrements the closer's wait group by one.
// Attention: Calling this without first adding to the WaitGroup by
// calling AddWaitGroup() results in a panic.
CloserDone()
// CloserOneWay creates a new child closer that has a one-way relationship
// with the current closer. This means that the child is closed whenever
// the parent closes, but not vice versa.
CloserOneWay(f ...CloseFunc) Closer
// CloserTwoWay creates a new child closer that has a two-way relationship
// with the current closer. This means that the child is closed whenever
// the parent closes and vice versa.
CloserTwoWay(f ...CloseFunc) Closer
// ClosingChan returns a channel, which is closed as
// soon as the closer is about to close.
// Remains closed, once ClosedChan() has also been closed.
ClosingChan() <-chan struct{}
// IsClosed returns a boolean indicating
// whether this instance has been closed completely.
IsClosed() bool
// IsClosing returns a boolean indicating
// whether this instance is about to close.
// Also returns true, if IsClosed() returns true.
IsClosing() bool
// Calls the close function on close.
// Errors are appended to the Close() multi error.
// Close functions are called in LIFO order.
OnClose(f ...CloseFunc)
}
A Closer is a thread-safe helper for common close actions.