workerpool

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: MIT Imports: 3 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher struct {
	JobQueue chan IJob
	// contains filtered or unexported fields
}

Dispatcher holds worker pool, job queue and manages workers and job To submit a job to worker pool, use code `dispatcher.JobQueue <- job`

func NewDispatcher

func NewDispatcher(dispatcherName string, options ...Option) *Dispatcher

NewDispatcher : returns a new dispatcher. When no options are given, it returns a dispatcher with default settings 10 Workers and `newWorker` initialisation and default logger which logs to graylog @ 127.0.0.1:11100. This is not in use. So it is prety much useless. Set log level to INFO to track max used workers.

func (*Dispatcher) ResetDispatcherMaxWorkerUsed

func (d *Dispatcher) ResetDispatcherMaxWorkerUsed()

ResetDispatcherMaxWorkerUsed should be called whenever the max worker count needs to be reset

type IJob

type IJob interface {
	Process() error
}

IJob : Interface for the Job to be processed

type IWorker

type IWorker interface {
	Start()
	Stop()
}

IWorker : Interface for Worker

type Option

type Option func(d *Dispatcher)

Option sets a parameter for the Dispatcher

func SetJobQueue

func SetJobQueue(jobQueue chan IJob) Option

SetJobQueue sets the JobQueue in dispatcher

func SetLatencyLogger

func SetLatencyLogger(logger gologger.IMultiLogger) Option

SetLatencyLogger sets the latency logger for the dispatcher This should be set in order to get the max worker count

func SetLogger

func SetLogger(logger *gologger.CustomLogger) Option

SetLogger sets the logger in dispatcher

func SetMaxWorkers

func SetMaxWorkers(maxWorkers int) Option

SetMaxWorkers sets the number of workers. Default is 10

func SetNewWorker

func SetNewWorker(newWorker func(chan chan IJob, int) IWorker) Option

SetNewWorker sets the Worker initialisation function in dispatcher

type Worker

type Worker struct {
	WorkerPool   chan chan IJob // A pool of workers channels that are registered in the dispatcher
	JobChannel   chan IJob      // Channel through which a job is received by the worker
	Quit         chan bool      // Channel for Quit signal
	WorkerNumber int            // Worker Number
}

Worker : Default Worker implementation

func (*Worker) Start

func (w *Worker) Start()

Start : Start the worker and add to worker pool

func (*Worker) Stop

func (w *Worker) Stop()

Stop : Calling this method stops the worker

Jump to

Keyboard shortcuts

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