Documentation
¶
Overview ¶
Package producers counts the goroutines that keep something alive, and decides its end under the lock that admits them.
A sync.WaitGroup cannot do that: a member must not be added from outside while Wait may be returning, and the owners of these goroutines are joined from outside by nature. A log session takes a source from an options update while its own goroutine is finishing; a session manager takes a session from a caller while it is closing. A WaitGroup in that place is a data race under -race and, in a plugin, "sync: WaitGroup is reused before previous Wait has returned": a panic that ends the process. A Group makes the end of the owner a state instead, so that a producer that races it is refused, or waited for, and never lost.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group tracks the producers of one owner: the goroutines that go on feeding it after its own work is over. Its owner waits for them, so that it can publish its end when the last one has returned.
It does what a sync.WaitGroup does, without the one thing a WaitGroup forbids: a member cannot be added from outside while Wait may be returning, and a producer is outside by nature. Here the end of the group is a state, decided under the same lock that admits members, so a producer either joins the group or is told it is over.
A group passes through three states:
- open: the owner's own work is still running. Start always succeeds.
- closing: the owner has called Wait, because its own work is over, and the producers that remain are still running. Start still succeeds: a running producer keeps the owner alive (a session's watcher may add a source, and an update may restart one).
- ended: Wait has returned, because the last producer returned. Start fails for good, and the owner's end is, or has been, published.
An owner that has to stop admitting at the moment it begins to shut down, and not when the last producer has gone, keeps its own flag under the lock that admits, and checks it before Start (a manager's Close does). The group guarantees the rest: that a producer admitted before the end is waited for, and that one that comes after it is refused.
The zero value is an open group.
func (*Group) Done ¶
func (g *Group) Done()
Done reports that a producer registered with Start has returned. It panics when no producer is registered: a Done without a Start is a bug in the owner.
func (*Group) Freeze ¶
func (g *Group) Freeze() (thaw func())
Freeze holds the group's lock until the returned function is called: every Start, Done and Wait blocks meanwhile. It is a seam for tests that have to hold a producer's reservation where it happens, to force an interleaving of the code that reserves it; nothing else should call it.