routinepool

package module
v0.0.0-...-7dd96af Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: MIT Imports: 1 Imported by: 0

README

gocover.run

Routinepool

Routinepool is a simple Go library to start a worker pool. The example provided in the test illustrates everything what this worker pool can do. e.g. See number of active jobs, pending jobs, stop worker pool etc.

  1. Start worker pool
  2. Push tasks/work to the pool
  3. See active number of tasks
  4. See pending tasks

Methods available

  1. New() - returns a new Pool pointer with all the configurations set, ready to be used
  2. p.Start() - starts the workerpool and waits for tasks to be pushed
  3. p.Push(<task>) - pushes a work/task to the queue
  4. p.Active() - returns the number of tasks which are actively running
  5. p.Pending() - returns the number of tasks which are pending in the queue
  6. p.Stop() - gracefully shutsdown the workerpool, i.e. waits for the queue to be empty and shutdown

Documentation

Overview

Package routinepool lets you create a workerpool made of Go routines.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrShutdown = errors.New("Shutdown in progress")

ErrShutdown is the error returned if the pool shutdown is already in progress And you try calling Stop()

Functions

This section is empty.

Types

type Pool

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

Pool is the struct which handles the worker pool

Example
var completed uint64
const (
	jobs     = 32
	poolsize = 4
)
p := New(poolsize, jobs)

//Start the routine pool and wait for jobs/tasks
p.Start()

for i := 0; i < jobs; i++ {
	go p.Push(func() {
		time.Sleep(time.Millisecond * 50)
		atomic.AddUint64(&completed, 1)
	})
}

//Gracefully shutdown the routine pool.
p.Stop()

fmt.Println("Active jobs:", p.Active(), "Pending jobs:", p.Pending(), "Completed:", completed, "out of", jobs)
Output:

Active jobs: 0 Pending jobs: 0 Completed: 32 out of 32

func New

func New(pSize, csize uint64) *Pool

New returns a Pool object pointer with all the default values set

func (*Pool) Active

func (p *Pool) Active() int

Active returns the number of active jobs

func (*Pool) Pending

func (p *Pool) Pending() int

Pending returns the number of jobs in queue

func (*Pool) Push

func (p *Pool) Push(w poolFn)

Push pushes a task to the worker pool

func (*Pool) Start

func (p *Pool) Start()

Start starts the worker Q

func (*Pool) Stop

func (p *Pool) Stop() error

Stop stops the pool and exits all the go routines immediately

Jump to

Keyboard shortcuts

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