Documentation
¶
Overview ¶
Package dispatch provides goroutine dispatch and concurrency limiting. It provides an object Dispatch which is a queueing system for concurrent functions. It implements a dynamic limit on the number of routines that it runs simultaneously. It also uses a Queue interface, allowing for alternate queue implementations.
See github.com/bmatsuo/dispatch/queues for more about the Queue interface and, a list of commonly used queues.
See github.com/bmatsuo/dispatch/examples for usage examples.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dispatch ¶
type Dispatch struct {
// The maximum number of goroutines can be changed while the queue is
// processing.
MaxGo int
// contains filtered or unexported fields
}
A Dispatch is an automated function dispatch queue with a limited number of concurrent gorountines. The queue can be altered with the Dispatch methods Enqueue and SetKey (TODO add method Remove).
func NewCustom ¶
Create a new Dispatch object with a custom backend queue satisfying interface queues.Queue. It is not safe to allow non-Dispatch methods any access to the object queue. This can lead to race conditions with possible corruption of internal structures. So, it's considered a best practice to only pass NewCustom(...) newly created queues.
fifoDispatch := NewCustom(10, queues.NewFIFO()) priorityDispatch := NewCustom(20, queues.NewPriorityQueue())
func (*Dispatch) Enqueue ¶
Enqueue a task for execution as a goroutine. The given queues.Task is given a unique id (int64) and stored in the Dispatch gq's backend queues.Queue object.
func (*Dispatch) Start ¶
func (gq *Dispatch) Start()
Start executing goroutines. Don't stop until gq.Stop() is called. This method will take control of the calling thread. But, it's safe to call in a goroutine.
gq := dispatch.New()
go gq.Start()
wg := new(sync.WaitGroup)
for i := 0 ; i < 1000 ; i++ {
wg.Add(1)
gq.Enqueue(dispatch.NewTask(
func(id int64) {
log.Printf("I'm alive %d", i)
wg.Done()
} ) )
}
wg.Wait()
gq.Stop()
type StdTask ¶
type StdTask struct {
F func(id int64)
}
A simple task for use in a priority-less queue. See package github.com/bmatsuo/dispatch/queues
Directories
¶
| Path | Synopsis |
|---|---|
|
Examples using the dispatch package.
|
Examples using the dispatch package. |
|
Package queues defines the Queue interface used in package dispatch, and several Queue implementations.
|
Package queues defines the Queue interface used in package dispatch, and several Queue implementations. |