Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type MailboxSize ¶
type MailboxSize int
type WorkerPool ¶ added in v0.3.0
type WorkerPool chan func()
func New ¶ added in v0.3.0
func New(mailboxSize MailboxSize) WorkerPool
New creates a new WorkerPool without any initial workers. To spawn workers, Grow must be called.
func (WorkerPool) Blocking ¶ added in v0.3.0
func (pool WorkerPool) Blocking(ctx context.Context, callback func()) error
Blocking will panic, if the workerpool is stopped.
Example ¶
pool := New(1) defer pool.Stop() pool.Grow(context.Background(), 1) var state int64 job := func() { atomic.AddInt64(&state, 19) } _ = pool.Blocking(context.Background(), job) fmt.Println(atomic.LoadInt64(&state))
Output: 19
func (WorkerPool) Grow ¶ added in v0.3.0
Example ¶
const n = 19 pool := New(1) defer pool.Stop() pool.Grow(context.Background(), 3) // spin up three new workers var state int64 wg := &sync.WaitGroup{} wg.Add(n) for i := 0; i < n; i++ { _ = pool.SemiBlocking(context.Background(), func() { defer wg.Done(); atomic.AddInt64(&state, 1) }) } wg.Wait() fmt.Println(state)
Output: 19
func (WorkerPool) SemiBlocking ¶ added in v0.3.0
func (pool WorkerPool) SemiBlocking(ctx context.Context, callback func()) error
SemiBlocking sends the job to the worker in a non-blocking manner, as long as the mailbox is not full. After that, it becomes blocking until there is an empty space in the mailbox. If the workerpool is stopped, SemiBlocking will panic.
Example ¶
pool := New(1) defer pool.Stop() pool.Grow(context.Background(), 1) var state int64 jobDone := make(chan struct{}) job := func() { defer close(jobDone) atomic.AddInt64(&state, 19) } _ = pool.SemiBlocking(context.Background(), job) <-jobDone fmt.Println(state)
Output: 19
func (WorkerPool) Stop ¶ added in v0.3.0
func (pool WorkerPool) Stop()
Click to show internal directories.
Click to hide internal directories.