Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Discard ¶ added in v1.3.0
func Discard(o *options)
Discard will discard new goroutines if semaphore is full, i.e. no more goroutines allowed
func Preemptive ¶
func Preemptive(o *options)
Preemptive sets locking mode preventing spawning waiting goroutine. May cause Go call to block!
Types ¶
type ErrSizedGroup ¶
type ErrSizedGroup struct {
// contains filtered or unexported fields
}
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
Example (Go) ¶
illustrates the use of a SizedGroup for concurrent, limited execution of goroutines.
// create sized waiting group allowing maximum 10 goroutines grp := NewErrSizedGroup(10) var c uint32 for i := 0; i < 1000; i++ { // Go call is non-blocking, like regular go statement grp.Go(func() error { // do some work in 10 goroutines in parallel atomic.AddUint32(&c, 1) time.Sleep(10 * time.Millisecond) return nil }) } // Note: grp.Go acts like go command - never blocks. This code will be executed right away log.Print("all 1000 jobs submitted") // wait for completion if err := grp.Wait(); err != nil { panic(err) }
Output:
func NewErrSizedGroup ¶
func NewErrSizedGroup(size int, options ...GroupOption) *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.
func (*ErrSizedGroup) Go ¶
func (g *ErrSizedGroup) Go(f func() error)
Go calls the given function in a new goroutine. The first call to return a non-nil error cancels the group if termOnError; its error will be returned by Wait. If no termOnError all errors will be collected in multierror.
func (*ErrSizedGroup) Wait ¶
func (g *ErrSizedGroup) Wait() error
Wait blocks until all function calls from the Go method have returned, then returns all errors (if any) wrapped with multierror from them.
type GroupOption ¶
type GroupOption func(o *options)
GroupOption functional option type
func Context ¶
func Context(ctx context.Context) GroupOption
Context passes ctx to group, goroutines will be canceled if ctx is canceled
type Locker ¶ added in v1.3.0
Locker is a superset of sync.Locker interface with TryLock method.
func NewSemaphore ¶
NewSemaphore makes Semaphore with given capacity
type MultiError ¶ added in v1.3.2
type MultiError struct {
// contains filtered or unexported fields
}
MultiError is a thread safe container for multi-error type that implements error interface
func (*MultiError) Error ¶ added in v1.3.2
func (m *MultiError) Error() string
Error returns multi-error string
func (*MultiError) ErrorOrNil ¶ added in v1.3.2
func (m *MultiError) ErrorOrNil() error
ErrorOrNil returns nil if no errors or multierror if errors occurred
func (*MultiError) Errors ¶ added in v1.3.2
func (m *MultiError) Errors() []error
type SizedGroup ¶
type SizedGroup struct {
// contains filtered or unexported fields
}
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(ctx context.Context) { // 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
Output:
func NewSizedGroup ¶
func NewSizedGroup(size int, opts ...GroupOption) *SizedGroup
NewSizedGroup makes wait group with limited size alive goroutines
func (*SizedGroup) Go ¶
func (g *SizedGroup) Go(fn func(ctx context.Context))
Go calls the given function in a new goroutine. Every call will be unblocked, but some goroutines may wait if semaphore locked.
func (*SizedGroup) Wait ¶
func (g *SizedGroup) Wait()
Wait blocks until the SizedGroup counter is zero. See sync.WaitGroup documentation for more information.