wpool

package module
v0.0.0-...-20dffa2 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2018 License: MIT Imports: 1 Imported by: 0

README

wpool

golang worker pool library with example(s)

This lib is based on article Writing worker queues, in Go

usage

package main

import (
	"log"
	"os"
	"os/signal"
	"time"

	"github.com/gotohr/wpool"
)

type Numbers struct {
	A int
	B int
}

func main() {
	// we start two simple dispatcher with 4 workers each
	adder := wpool.NewDispatcher("adder", 4)
	multiplier := wpool.NewDispatcher("multiplier", 4)

	// ProcessorFn "add"
	add := func(w wpool.Work, dName string, destination *wpool.Dispatcher) {
		nums := w.(Numbers)

		sum := nums.A + nums.B

		// just make this function "doing stuff"
		time.Sleep(1 * time.Second)

		// pipe resulting work to destination Dispatcher
		destination.WorkQueue <- sum
	}

	// ProcessorFn "multiply"
	multiply := func(w wpool.Work, dName string, destination *wpool.Dispatcher) {
		value := w.(int)

		// just make this function "doing stuff"
		time.Sleep(1 * time.Second)

		log.Println(dName, value*2)

		// pipe resulting work to destination Dispatcher
		destination.WorkQueue <- Numbers{value, 2}
	}

	// start dispatcher with 4 workers that run "add" function and pipe results to "multiplier" dispatcher
	adder.Start(add, &multiplier)

	// start dispatcher with 4 workers that run "multiply" function and pipe results to "adder" dispatcher
	multiplier.Start(multiply, &adder)

	// give "adder" some Work
	adder.WorkQueue <- Numbers{1, 2}
	adder.WorkQueue <- Numbers{2, 2}
	adder.WorkQueue <- Numbers{3, 2}
	adder.WorkQueue <- Numbers{4, 2}
	adder.WorkQueue <- Numbers{5, 2}
	adder.WorkQueue <- Numbers{6, 2}
	adder.WorkQueue <- Numbers{7, 2}
	adder.WorkQueue <- Numbers{8, 2}

	// block until app is terminated
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	<-c
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher struct {
	Name        string
	NWorkers    int
	WorkerQueue chan chan Work
	WorkQueue   chan Work
}

func NewDispatcher

func NewDispatcher(name string, nworkers int) Dispatcher

func (Dispatcher) Start

func (d Dispatcher) Start(pfn ProcessorFn, destination *Dispatcher) Dispatcher

type PElement

type PElement struct {
	Name     string
	NWorkers int
	PFN      ProcessorFn
}

type Pipeline

type Pipeline struct {
	Dispatchers []Dispatcher
}

func NewPipeline

func NewPipeline(elements []PElement) Pipeline

type ProcessorFn

type ProcessorFn func(w Work, dispatcherName string, destination *Dispatcher)

type Work

type Work interface{}

type Worker

type Worker struct {
	ID          int
	Work        chan Work
	WorkerQueue chan chan Work
	QuitChan    chan bool
	PFN         ProcessorFn
	Destination *Dispatcher
}

func NewWorker

func NewWorker(id int, workerQueue chan chan Work, pfn ProcessorFn, destination *Dispatcher) Worker

func (*Worker) Start

func (w *Worker) Start(dispatcherName string)

func (*Worker) Stop

func (w *Worker) Stop()

stop listening for work requests (worker stops only after work is done)

Directories

Path Synopsis
examples
fetchconvert command
pipeline command
twodisaptchers command
withbeanrpc command

Jump to

Keyboard shortcuts

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