Categorygithub.com/jirenius/taskqueue
modulepackage
1.2.0
Repository: https://github.com/jirenius/taskqueue.git
Documentation: pkg.go.dev

# 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")
    }

# Functions

NewKeyTaskQueue returns a new KeyTaskQueue.
NewTaskQueue returns a new TaskQueue.

# Structs

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.
TaskQueue implements a queue for tasks to be handled sequentially in a first-in-first-out order on a separate goroutine.