Documentation ¶
Index ¶
- Variables
- type ThreadGroup
- func (tg *ThreadGroup) Add() error
- func (tg *ThreadGroup) AfterStop(fn func())
- func (tg *ThreadGroup) Done()
- func (tg *ThreadGroup) Flush() error
- func (tg *ThreadGroup) OnStop(fn func())
- func (tg *ThreadGroup) Stop() error
- func (tg *ThreadGroup) StopChan() <-chan struct{}
- func (tg *ThreadGroup) StopCtx() context.Context
- type TryMutex
Constants ¶
This section is empty.
Variables ¶
var ErrStopped = errors.New("ThreadGroup already stopped")
ErrStopped is returned by ThreadGroup methods if Stop has already been called.
Functions ¶
This section is empty.
Types ¶
type ThreadGroup ¶
type ThreadGroup struct {
// contains filtered or unexported fields
}
A ThreadGroup is a one-time-use object to manage the life cycle of a group of threads. It is a sync.WaitGroup that provides functions for coordinating actions and shutting down threads. After Stop() is called, the thread group is no longer useful.
It is safe to call Add(), Done(), and Stop() concurrently, however it is not safe to nest calls to Add(). A simple example of a nested call to add would be:
tg.Add() tg.Add() tg.Done() tg.Done()
func (*ThreadGroup) Add ¶
func (tg *ThreadGroup) Add() error
Add increments the thread group counter.
func (*ThreadGroup) AfterStop ¶
func (tg *ThreadGroup) AfterStop(fn func())
AfterStop ensures that a function will be called after Stop() has been called and after all running routines have called Done(). The functions will be called in reverse order to how they were added, similar to defer. If Stop() has already been called, the input function will be called immediately.
The primary use of AfterStop is to allow code that opens and closes resources to be positioned next to each other. The purpose is similar to `defer`, except for resources that outlive the function which creates them.
func (*ThreadGroup) Flush ¶
func (tg *ThreadGroup) Flush() error
Flush will block all calls to 'tg.Add' until all current routines have called 'tg.Done'. This in effect 'flushes' the module, letting it complete any tasks that are open before taking on new ones.
func (*ThreadGroup) OnStop ¶
func (tg *ThreadGroup) OnStop(fn func())
OnStop ensures that a function will be called after Stop() has been called, and before blocking until all running routines have called Done(). It is safe to use OnStop to coordinate the closing of long-running threads. The OnStop functions will be called in the reverse order in which they were added, similar to defer. If Stop() has already been called, the input function will be called immediately.
func (*ThreadGroup) Stop ¶
func (tg *ThreadGroup) Stop() error
Stop will close the stop channel of the thread group, then call all 'OnStop' functions in reverse order, then will wait until the thread group counter reaches zero, then will call all of the 'AfterStop' functions in reverse order. After Stop is called, most actions will return ErrStopped.
func (*ThreadGroup) StopChan ¶
func (tg *ThreadGroup) StopChan() <-chan struct{}
StopChan provides read-only access to the ThreadGroup's stopChan. Callers should select on StopChan in order to interrupt long-running reads (such as time.After).
func (*ThreadGroup) StopCtx ¶
func (tg *ThreadGroup) StopCtx() context.Context
StopCtx returns the threadgroups underlying context.
type TryMutex ¶
type TryMutex struct {
// contains filtered or unexported fields
}
TryMutex provides a mutex that allows you to attempt to grab a mutex, and then fail if the mutex is either not grabbed immediately or is not grabbed by the specified duration.
func (*TryMutex) Lock ¶
func (tm *TryMutex) Lock()
Lock grabs a lock on the TryMutex, blocking until the lock is obtained.
func (*TryMutex) TryLock ¶
TryLock grabs a lock on the TryMutex, returning an error if the mutex is already locked.
func (*TryMutex) TryLockTimed ¶
TryLockTimed grabs a lock on the TryMutex, returning an error if the mutex is not grabbed after the provided duration.