Documentation
¶
Overview ¶
Package coma limits how many goroutines run concurrently and waits for all of them to finish.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrClosed = errors.New("coma: manager is shut down")
ErrClosed is returned by ConcurrencyManager.Acquire and ConcurrencyManager.AcquireContext when ConcurrencyManager.Wait has been called.
Functions ¶
This section is empty.
Types ¶
type ConcurrencyManager ¶
type ConcurrencyManager struct {
// contains filtered or unexported fields
}
ConcurrencyManager limits how many goroutines can run concurrently.
func New ¶
func New(max int) *ConcurrencyManager
New creates a ConcurrencyManager that allows at most max concurrently running goroutines. A max < 1 is treated as 1.
func (*ConcurrencyManager) Acquire ¶
func (c *ConcurrencyManager) Acquire() error
Acquire blocks until a slot is available and claims it for a new goroutine. If ConcurrencyManager.Wait has been called, Acquire returns ErrClosed.
func (*ConcurrencyManager) AcquireContext ¶
func (c *ConcurrencyManager) AcquireContext(ctx context.Context) error
AcquireContext blocks until a slot is available and claims it for a new goroutine, or returns ctx.Err() if the context is done first. If ConcurrencyManager.Wait has been called, AcquireContext returns ErrClosed.
func (*ConcurrencyManager) Release ¶
func (c *ConcurrencyManager) Release()
Release marks a goroutine as finished and releases one slot. Every successful ConcurrencyManager.Acquire or ConcurrencyManager.AcquireContext must be matched by exactly one Release.
Release blocks only while no slot is held at all. An unmatched Release made while other goroutines hold slots takes one of theirs instead of blocking, which lets the limit be exceeded and can make ConcurrencyManager.Wait return before those goroutines finish. The Release whose slot has been taken then blocks in its place.
func (*ConcurrencyManager) RunningCount ¶
func (c *ConcurrencyManager) RunningCount() int
RunningCount returns the number of currently held slots: those for which ConcurrencyManager.Acquire or ConcurrencyManager.AcquireContext returned nil and ConcurrencyManager.Release has not yet been called. Goroutines blocked in Acquire are not counted, so this is not necessarily the number of goroutines running.
func (*ConcurrencyManager) Wait ¶
func (c *ConcurrencyManager) Wait()
Wait waits until all goroutines are done. Wait is terminal, meaning ConcurrencyManager cannot be reused. Wait is safe for concurrent use. A repeated call is a safe no-op.