pool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 26, 2019 License: MIT Imports: 1 Imported by: 0

README

pool

a simple worker pool for gophers

installation

as with all other go packages, you know what to do:

go get github.com/rocketbitz/pool

usage

this worker pool package was designed to be straightforward, specifically because I found others to be unnecessarily complex. here's an example:

package main

import (
	"fmt"

	"github.com/rocketbitz/pool"
)

func main() {
	numWorkers := 10
	jobToRun := func(input interface{}) {
		fmt.Println("let's do a job on this input: ", input)
	}

	jobStartCallback := pool.Callback{
		Event: pool.JobStart,
		Func: func() {
			fmt.Println("we're starting a job...")
		},
	}

	jobEndCallback := pool.Callback{
		Event: pool.JobEnd,
		Func: func() {
			fmt.Println("we've finished a job.")
		},
	}

	p := pool.New(
		numWorkers,
		jobToRun,
		jobStartCallback,
		jobEndCallback,
	)

	c := make(chan interface{})

	go p.Work(c)

	for i := 0; i < 100; i++ {
		c <- "hello, gophers"
	}

	close(c)
	p.Wait()
}

hopefully that example makes sense to you. note that the callback argument to New() is variadic, so register as many callbacks as your heart desires. oh, one more thing, if you forget a callback when you declare the pool, you can always register one later like so:

p.RegisterCallback(
  Callback{
    Event: JobEnd,
    Func: func() { fmt.Println("i'm a forgetful gopher") },
  },
)

contribute

pr's are welcome. if they're awesome, they'll get reviewed and merged. if they're not, they'll get reviewed and closed, hopefully with a kind comment as to the reason.

license

MIT ...move along, move along.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback struct {
	Func  func()
	Event JobEvent
}

Callback defines a function that is meant to be run each time the specified JobEvent occurs

type JobEvent

type JobEvent int

JobEvent defines the kind of event upon which the PoolCallback is executed

const (
	// JobStart callbacks run just before the job
	// is executed
	JobStart JobEvent = iota

	// JobEnd callbacks run just after the job
	// has executed
	JobEnd
)

type Pool

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

Pool defines a worker pool

func New

func New(routines int, job func(input interface{}), pcbs ...Callback) *Pool

New creates a new worker pool with a goroutine limit and a job function to execute on the incoming data

func (*Pool) RegisterCallback

func (p *Pool) RegisterCallback(pcb Callback)

RegisterCallback registers a callback to be triggered by the pool

func (*Pool) Wait

func (p *Pool) Wait()

Wait waits until the pool is finished

func (*Pool) Work

func (p *Pool) Work(c <-chan interface{})

Work is a blocking call that starts the pool working on a data input channel

Jump to

Keyboard shortcuts

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