dispatcher

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2020 License: MIT Imports: 3 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Report Card GoDoc Build status

Dispatcher

Dispatcher provides a concurrent tasks treating functionality based on go-routines.

After you initialize and run dispatcher it is ready to perform tasks.

You can add tasks via AddWork() func. Both Run() and AddWork() must be called in separate go-routines. Tasks will be treated concurrently but amount of workers will be limited to the parameter specified at the dispatcher initialization call New().

To stop the dispatcher you can use the Stop() func. But you should be careful with it: just after this call the dispatcher stops receiving new work and stops returning results even if the task was already processed but result were not yet sent into appropriate channel. When Stop() was called, dispatcher flushes channels and exits.

If you need just to do tons of work with dispatcher and quit after without waiting for next portions of work, you can use the WaitUntilNoTasks(period) func. It checks the number of currently processing tasks each period of seconds. If it seems that dispatcher is not processing any task, it stops.

The Treat() by default is not thread-safe and one dispatcher performs multiple of Treat at a time. Be careful using shared memory in it to avoid data races. The onResultFunc also is not thread-safe. You should control safety by yourself.

You can use the second dispatcher and organize two dispatchers in a conveyor pattern using Treat in first dispatcher to treat first stage data, and in the onResultFunc of the first dispatcher you may call go AddWork(...) to send the result to the second dispatcher. The second dispatcher will treat the data from the first's onResultFunc in it's own Treat of the returned element and return it in it's own onResultFunc, this one of a second stage dispatcher. And so on: you can organize it in more stages.

For example of one-staged using please look into the tests.

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 implements a task-dispatching functionality

func New

func New(max int, onResultFunc OnResultFunc) *Dispatcher

New returns new dispatcher with max workers

func (*Dispatcher) AddWork

func (d *Dispatcher) AddWork(data Treator)

AddWork adds work with data to the dispatcher

func (*Dispatcher) ErrorCount

func (d *Dispatcher) ErrorCount() int

ErrorCount returns an error count

func (*Dispatcher) FillWork

func (d *Dispatcher) FillWork(data Treator, queueLimit int, checkInterval time.Duration)

FillWork adds work with data to the dispatcher after queue size will fall below queueLimit value, check it every checkInterval

func (*Dispatcher) QueueLen

func (d *Dispatcher) QueueLen() int

QueueLen returns current length of the queue

func (*Dispatcher) Run

func (d *Dispatcher) Run()

Run starts dispatching

func (*Dispatcher) SetMaxWorkers

func (d *Dispatcher) SetMaxWorkers(n int)

SetMaxWorkers to n

func (*Dispatcher) Stop

func (d *Dispatcher) Stop()

Stop the dispatcher

func (*Dispatcher) TasksDone

func (d *Dispatcher) TasksDone() int

TasksDone returns done tasks count

func (*Dispatcher) WaitUntilNoTasks

func (d *Dispatcher) WaitUntilNoTasks(period time.Duration)

WaitUntilNoTasks stops the dispatcher making checks for "0 tasks now" every period

func (*Dispatcher) Workers

func (d *Dispatcher) Workers() int

Workers returns current worker count

type OnResultFunc

type OnResultFunc func(result Result)

OnResultFunc is a func to do something with task result on it's finish

type Result

type Result struct {
	In    interface{}
	Out   interface{}
	Error error
}

Result consists of an input and output data

type Treator

type Treator interface {
	Treat() (interface{}, error)
}

Treator can treat something

Jump to

Keyboard shortcuts

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