taskqueue

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2021 License: MIT Imports: 1 Imported by: 4

README

Task Queues for Go

License MIT Report Card Build Status Reference

A Go package that provides queues for tasks to be handled sequentially.

Installation

go get github.com/jirenius/taskqueue

Usage

TaskQueue
    // Create a TaskQueue with a queue cap of 5.
    tq := taskqueue.NewTaskQueue(5)

    // Add callbacks to be called sequentially in the queued order.
    tq.Do(func() { fmt.Println("First") })
    tq.Do(func() { fmt.Println("Second") })

    // Add a callback unless the queue is full.
    if !tq.TryDo(func() { fmt.Println("Done") }) {
        panic("queue is full")
    }
KeyTaskQueue
    // Create a KeyTaskQueue with a queue cap of 5 per key.
    ktq := taskqueue.NewKeyTaskQueue(5)

    // Add callbacks to be called sequentially, on different
	// goroutines for each key, in the queued order.
    ktq.Do("foo", func() { fmt.Println("First foo") })
    ktq.Do("foo", func() { fmt.Println("Second foo") })
    ktq.Do("bar", func() { fmt.Println("First bar") })

    // Add a callback unless the queue is full.
    if !ktq.TryDo("foo", func() { fmt.Println("Done") }) {
        panic("foo queue is full")
    }

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

func NewTaskQueue(cap int) *TaskQueue

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.

func (*TaskQueue) Resume added in v1.2.0

func (tq *TaskQueue) Resume()

Resume starts task handling again after being paused by calling Pause.

func (*TaskQueue) TryDo

func (tq *TaskQueue) TryDo(task func()) bool

TryDo will try to enqueue the task and return true on success. It will return false if the work queue is full.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL