Documentation
¶
Overview ¶
Package gate provides primitive to limit number of concurrent goroutine workers. Useful when sync.Locker or sync.WaitGroup is not enough.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Gate ¶
type Gate struct {
// contains filtered or unexported fields
}
A Gate is a primitive intended to help in limiting concurrency in some scenarios. Think of it as a close sync.WaitGroup analog which has upper limit on its counter or a sync.Locker which allows up to max number of concurrent lockers to be held.
func New ¶
New returns new Gate with provided capacity. If capacity is non-positive, New would panic.
func (*Gate) Add ¶
Add implements similar semantic to sync.WaitGroup.Add. If Add is called with positive argument N, it essentially calls Lock N times; if N is negative, it calls Unlock N times. If absolute value of N is greater than Gate capacity, Add would panic. Add is safe for concurrent use, but should be used with care as deadlocks are possible.
func (*Gate) Lock ¶
func (g *Gate) Lock()
Lock implements sync.Locker interface. Gate capacity determines number of non-blocking Lock calls, when max number is reached, Lock would block until some other goroutine calls Unlock. Lock is safe for concurrent use.