Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewSemaphore ¶
NewSemaphore makes Semaphore with given capacity
Types ¶
type ESGOption ¶ added in v1.0.0
type ESGOption func(esg *errSizedGroup) error
ESGOption functional option type
func Preemptive ¶
func Preemptive() ESGOption
Preemptive sets locking mode preventing spawning waiting goroutine. May cause Go call to block!
type ErrSizedGroup ¶
ErrSizedGroup is a SizedGroup with error control. Works the same as errgrp.Group, i.e. returns first error. Can work as regular errgrp.Group or with early termination. Thread safe. ErrSizedGroup interface enforces constructor usage and doesn't allow direct creation of errSizedGroup
func NewErrSizedGroup ¶
func NewErrSizedGroup(size int, options ...ESGOption) ErrSizedGroup
NewErrSizedGroup makes wait group with limited size alive goroutines. By default all goroutines will be started but will wait inside. For limited number of goroutines use Preemptive() options. TermOnErr will skip (won't start) all other goroutines if any error returned.
type SizedGroup ¶
type SizedGroup interface {
Go(fn func())
Wait()
}
SizedGroup has the same role as WaitingGroup but adds a limit of the amount of goroutines started concurrently. Uses similar Go() scheduling as errgrp.Group, thread safe. SizedGroup interface enforces constructor usage and doesn't allow direct creation of sizedGroup
Example (Go) ¶
illustrates the use of a SizedGroup for concurrent, limited execution of goroutines.
grp := NewSizedGroup(10) // create sized waiting group allowing maximum 10 goroutines
var c uint32
for i := 0; i < 1000; i++ {
grp.Go(func() { // Go call is non-blocking, like regular go statement
// do some work in 10 goroutines in parallel
atomic.AddUint32(&c, 1)
time.Sleep(10 * time.Millisecond)
})
}
// Note: grp.Go acts like go command - never blocks. This code will be executed right away
log.Print("all 1000 jobs submitted")
grp.Wait() // wait for completion
func NewSizedGroup ¶
func NewSizedGroup(size int) SizedGroup
NewSizedGroup makes wait group with limited size alive goroutines