tasks

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package tasks provides functionality for services to run a fixed number of workers to conduct generic asynchronous tasks. This is an intentionally simple package to make sure that routine, non-critical work happens in a non-blocking fashion.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TaskWorker

func TaskWorker(wg *sync.WaitGroup, queue <-chan *TaskHandler)

Types

type Task

type Task interface {
	Do(context.Context)
}

Workers in the task manager handle Tasks which can hold state and other information needed by the task. You can also specify a simple function to execute by using the TaskFunc to create a Task to provide to the task manager.

type TaskFunc

type TaskFunc func(context.Context)

TaskFunc is an adapter to allow ordinary functions to be used as tasks.

func (TaskFunc) Do

func (t TaskFunc) Do(ctx context.Context)

type TaskHandler

type TaskHandler struct {
	// contains filtered or unexported fields
}

type TaskManager

type TaskManager struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

TaskManagers execute Tasks using a fixed number of workers that operate in their own go routines. The TaskManager also has a fixed task queue size, so that if there are more tasks added to the task manager than the queue size, back pressure is applied.

func New

func New(workers, queueSize int) *TaskManager

New returns TaskManager, running the specified number of workers in their own Go routines and creating a queue of the specified size. The task manager is now ready to perform routine tasks!

func (*TaskManager) IsStopped

func (tm *TaskManager) IsStopped() bool

Check if the task manager has been stopped

func (*TaskManager) Queue

func (tm *TaskManager) Queue(task Task)

Queue a task with a background context. Blocks if the queue is full.

func (*TaskManager) QueueContext

func (tm *TaskManager) QueueContext(ctx context.Context, task Task)

Queue a task with the specified context. Blocks if the queue is full.

func (*TaskManager) Stop

func (tm *TaskManager) Stop()

Stop the task manager waiting for all workers to stop their tasks before returning.

Jump to

Keyboard shortcuts

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