workerqueue

package
v1.1.11 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

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

Dispatcher struct contains the necessary data to spawn the workers and start each worker, it contains a worker pool channel of fixed size ie buffered

func NewDispatcher

func NewDispatcher(maxWorkers int) *Dispatcher

NewDispatcher method will return a dispatcher object

func (*Dispatcher) Run

func (d *Dispatcher) Run(jobPool chan Job)

Run method will start the workers with the given jobPool which will be a buffered channel

func (*Dispatcher) Stop

func (d *Dispatcher) Stop()

Stop method will stop the dispatcher and its workers

type Job

type Job interface {
	Process() bool
}

Job interface

type Task

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

Task struct is just a wrapper for sync.WaitGroup

func (*Task) Run

func (t *Task) Run(job Job)

Run method should be used when you want to run a job in background but still ensure it is completed before program exits

func (*Task) Shutdown

func (t *Task) Shutdown(ctx context.Context) error

Shutdown method will wait for all the go routines associated with the tracker to complete or context to expire

type TaskQueue

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

TaskQueue should be used when you want to limit the number of tasks you want to process in background

func NewTaskQueue

func NewTaskQueue(l int) *TaskQueue

NewTaskQueue will create a new taskqueue of buffered job channel

func (*TaskQueue) AddJob

func (t *TaskQueue) AddJob(job Job)

AddJob method will add the job to the queue, this method is blocking since if the queue is full then it will wait till a job is finished

func (*TaskQueue) Shutdown

func (t *TaskQueue) Shutdown(ctx context.Context) error

Shutdown method will wait for all the go routines associated with the tracker to complete or context to expire

func (*TaskQueue) Start

func (t *TaskQueue) Start()

Start method will create a go routine that will process the work in background

type Worker

type Worker struct {
	ID         int
	JobChannel chan Job
	WorkerPool chan chan Job
	QuitChan   chan bool
}

Worker struct holds the information regarding the worker

func NewWorker

func NewWorker(id int, workerPool chan chan Job) *Worker

NewWorker method will create a worker object and return it

func (*Worker) Start

func (w *Worker) Start()

Start method will start the worker

func (*Worker) Stop

func (w *Worker) Stop()

Stop method will stop the worker

Jump to

Keyboard shortcuts

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