gotaskengine

package module
v0.0.0-...-53c6e96 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2020 License: MIT Imports: 5 Imported by: 0

README

go task engine

A multi-coroutine task processing engine.

What?

Recently, i'm working on handling multi-tasks in multi-coroutines.

There are many types of tasks, like A,B,C. The handle method is different if the type of task is different. When requests are coming, i need handle them in multi-coroutines.

How?

This is similar to a factory although i have never seen.

It should be like this: There are some conveyors is rolling in a factory. There are some workers working on the conveyor belt. They handle the rolling parts on the conveyor belt. Different parts are placed on different conveyor belts

Future

It's not good enough yet, welcome issue or pr.

Documentation

Index

Constants

View Source
const (
	StatusNew = iota
	StatusRun
	StatusStop
)

status from StatusNew to StatusStopped, no loop.

Variables

View Source
var ErrLineIsFull = errors.New("too many tasks, add a worker please")

ErrLineIsFull conveyor is full, can not puts any part. Client judges whether to add more workers according to the CPU's load.

View Source
var ErrLineStopped = errors.New("the conveyor is stopped")
View Source
var ErrUnallowed = errors.New("the factory is not allowed to add new conveyor")

Functions

This section is empty.

Types

type FuncWork

type FuncWork func(part Part)

FuncWork defines a function could deal parts.

type IConveyor

type IConveyor interface {

	// PutPart puts parts on conveyor belt, if a conveyor is full, return ErrLineIsFull.
	PutPart(p Part, duration time.Duration) error

	// GetPart return a part and the status of conveyor if closed.
	GetPart() (Part, bool)

	// Run lets the conveyor rolling.
	Run()

	// Stop stops a conveyor.
	Stop()
}

ConveyorI receives parts, and workers take parts from it.

func NewConveyor

func NewConveyor(pipeCap int, handler FuncWork, maxWorkerCnt, minWokerCnt int, interval time.Duration) IConveyor

NewConveyor create a conveyor instance.

type IFactory

type IFactory interface {
	// AddLine add conveyor to the factory.
	AddLine(name string, c IConveyor) error

	// Run lets all conveyors running.
	Run()

	// Stop lets all conveyors stop.
	Stop()

	// GetLine
	GetLine(name string) IConveyor
}

func NewFactory

func NewFactory() IFactory

type IWorker

type IWorker interface {
	Working()
	Stop()
}

Worker - worker take parts from conveyor and handle them.

type Part

type Part interface{}

Part workers take it to do something

type TConveyor

type TConveyor struct {
	StopSignC chan struct{} // receive stop signal.
	// contains filtered or unexported fields
}

func (*TConveyor) GetPart

func (c *TConveyor) GetPart() (Part, bool)

func (*TConveyor) PutPart

func (c *TConveyor) PutPart(p Part, duration time.Duration) error

func (*TConveyor) Run

func (c *TConveyor) Run()

func (*TConveyor) Stop

func (c *TConveyor) Stop()

type TFactory

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

emptyFactory a instance of Factory interface

func (*TFactory) AddLine

func (f *TFactory) AddLine(name string, c IConveyor) error

func (*TFactory) GetLine

func (f *TFactory) GetLine(name string) IConveyor

func (*TFactory) Run

func (f *TFactory) Run()

func (*TFactory) Stop

func (f *TFactory) Stop()

type TWorker

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

TWorker is the implementation of IWorker interface.

func NewWorker

func NewWorker(conveyor IConveyor, handler FuncWork) *TWorker

func (*TWorker) Stop

func (w *TWorker) Stop()

func (*TWorker) Working

func (w *TWorker) Working()

Jump to

Keyboard shortcuts

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