Documentation
¶
Overview ¶
Package taskqueue implements queues for tasks to be handled sequentially.
Example (KeyTaskQueue) ¶
ExampleKeyTaskQueue demonstrates how to enqueue callbacks on different goroutines based on key.
package main import ( "fmt" "github.com/jirenius/taskqueue" ) func main() { ach := make(chan string) bch := make(chan string) // Create KeyTaskQueue with a queue cap of 5 per key. tq := taskqueue.NewKeyTaskQueue(5) // The callback functions are called sequentially on // different goroutines for each key, in the queued order. tq.Do("a", func() { ach <- "First a" }) tq.Do("b", func() { bch <- "First b" }) tq.Do("a", func() { ach <- "Second a" }) tq.Do("b", func() { bch <- "Second b" }) tq.Do("a", func() { close(ach) }) tq.Do("b", func() { close(bch) }) // Print strings sent from queue a. for s := range ach { fmt.Println(s) } // Print strings sent from queue b. for s := range bch { fmt.Println(s) } }
Output: First a Second a First b Second b
Example (TaskQueue) ¶
ExampleTaskQueue demonstrates how to enqueue callbacks.
package main import ( "fmt" "github.com/jirenius/taskqueue" ) func main() { ch := make(chan string) // Create TaskQueue with a queue cap of 5. tq := taskqueue.NewTaskQueue(5) // The callback functions are called sequentially on // a different goroutine, in the queued order. tq.Do(func() { ch <- "First" }) tq.Do(func() { ch <- "Second" }) tq.Do(func() { close(ch) }) // Print strings sent from queued callbacks. for s := range ch { fmt.Println(s) } }
Output: First Second
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type KeyTaskQueue ¶
type KeyTaskQueue struct {
// contains filtered or unexported fields
}
KeyTaskQueue implements a queue for tasks to be handled sequentially in a first-in-first-out order on separate goroutines identified by a key string.
func NewKeyTaskQueue ¶
func NewKeyTaskQueue(cap int) *KeyTaskQueue
NewKeyTaskQueue returns a new KeyTaskQueue.
The value cap is the number of tasks that may be queue at the same time, and must be 1 or greater.
func (*KeyTaskQueue) Do ¶
func (nq *KeyTaskQueue) Do(key string, task func())
Do will enqueue the task to be handled by the task queue for the given key. If the queue is full, Do will wait block until there is room in the queue.
func (*KeyTaskQueue) TryDo ¶
func (nq *KeyTaskQueue) TryDo(key string, task func()) bool
TryDo will try to enqueue the task and return true on success. It will return false if the work queue for the given key is full.
type TaskQueue ¶
type TaskQueue struct {
// contains filtered or unexported fields
}
TaskQueue implements a queue for tasks to be handled sequentially in a first-in-first-out order on a separate goroutine.
func NewTaskQueue ¶
NewTaskQueue returns a new TaskQueue.
The value cap is the number of tasks that may be queue at the same time, and must be 1 or greater.
func (*TaskQueue) Do ¶
func (tq *TaskQueue) Do(task func())
Do will enqueue the task to be handled by the task queue. If the queue is full, Do will wait block until there is room in the queue.
func (*TaskQueue) Flush ¶ added in v1.1.0
func (tq *TaskQueue) Flush()
Flush waits for the queue to be cleared.
func (*TaskQueue) Pause ¶ added in v1.2.0
func (tq *TaskQueue) Pause()
Pause stops handling new tasks, and waits until any currently running task is complete. If Pause is called multiple times, an equal number of calls to Resume must be made to resume handling of tasks.