workers

package
v0.0.0-...-7d2715d Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2019 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package workers contains functions that allow for work to be dispatched for processing

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher struct {
	WorkerPool   Pool
	WorkerAmount int
	Workers      []Worker
	WaitGroup    *sync.WaitGroup
}

A Dispatcher manages the distribution of work to workers

func NewDispatcher

func NewDispatcher(workerAmount int) *Dispatcher

NewDispatcher creates a dispatcher and captures the amount of workers requested

func (*Dispatcher) CreateWorkers

func (d *Dispatcher) CreateWorkers()

CreateWorkers creates the requested amount of workers and adds them to an internal cache. The internal waitgroup is incremented for each worker created in order to wait for completion of all workers.

func (*Dispatcher) DispatchFrom

func (d *Dispatcher) DispatchFrom(jobQueue JobChannel)

DispatchFrom dispatches work from the job queue to the workers. Each worker has their own work channel and these channels are fed to the pool looking for work. Once a channel is retreived from the pool, work is submitted to the worker that owns that channel.

Once all work from the job queue is complete, all channels from the workers are closed.

func (*Dispatcher) WaitForCompletion

func (d *Dispatcher) WaitForCompletion()

WaitForCompletion waits for all work to be completed

type Job

type Job interface {
	Execute() error
}

Job is an interface abstraction for anything that implements the Execute function.

type JobChannel

type JobChannel chan Job

JobChannel is a channel that accepts Job types.

type Pool

type Pool chan JobChannel

Pool is a channel that accepts JobChannel types.

type Worker

type Worker struct {
	ID         int
	WorkerPool Pool
	JobChannel JobChannel
	WG         *sync.WaitGroup
}

Worker is a structure that executes Job types.

func NewWorker

func NewWorker(id int, pool Pool, wg *sync.WaitGroup) Worker

NewWorker creates a new Worker type.

func (*Worker) Start

func (w *Worker) Start()

Start is a gorountine that sends the worker's job channel to the pool in order for the consumer to dispatch a job to the worker. Once the consumer closes the worker's job channel, the gorountine exits.

func (*Worker) Stop

func (w *Worker) Stop()

Stop executes the Done() function on the worker's wait group to signal it is finished.

Jump to

Keyboard shortcuts

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