Help us shape the future of Go! Take the 2026 Go Developer Survey

pooligo

package module
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2018 License: MIT Imports: 4 Imported by: 0

README

PooliGo

Package pooligo provides a basic worker-pool. This can be used to restrict the number of threads created by an application while providing multi-threaded capabilities.

Usage

import (
  "github.com/bcvery1/pooligo"
)

const (
  fooCount = 1000
)

var (
  wg sync.WaitGroup
)

type Foo struct {}

// Provide an `Action` function for the structure
func (f *Foo) Action() {
  defer wg.Done()
  // ...
  // Code to be performed on f
  // ...
}

func main() {
  p := pooligo.NewPool(5, 10)

  for i := 0; i < fooCount; i++ {
    wg.Add(1)
    f := &Foo{}
    p.Add(f)
  }

  // Wait for pool to finish, or run indefinitely
  wg.Wait()
}

Documentation

Overview

Package pooligo provides a basic worker-pool. This can be used to restrict the number of threads created by an application while providing multi-threaded capabilities.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CtxPool

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

CtxPool struct representing a standard worker-pool but with a parent context. If you do not need to explicitly pass in a context, use `Pool` (from `NewPool()`)

func NewCtxPool

func NewCtxPool(parentCtx context.Context, workerCount, queueSize int) *CtxPool

NewCtxPool creates a standard worker pool with a parent context

func (*CtxPool) Add

func (p *CtxPool) Add(j job)

Add is used to add a job to the worker-pool

func (*CtxPool) Close

func (p *CtxPool) Close()

Close will stop any workers doing any jobs added after calling Close Any jobs being processed will be completed

func (*CtxPool) Size

func (p *CtxPool) Size() int

Size returns the current size of the worker pool.

type Flexpool

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

Flexpool is an extension to the standard worker pool. It can be grown and shrunk after creation

func NewFlexPool

func NewFlexPool(workerCount, queueSize int) *Flexpool

NewFlexPool creates a standard worker pool

func (*Flexpool) Add

func (p *Flexpool) Add(j job)

Add is used to add a job to the worker-pool.

func (*Flexpool) Close

func (p *Flexpool) Close()

Close will stop any workers doing any jobs added after calling Close. Any jobs being processed will be completed.

func (*Flexpool) Grow

func (p *Flexpool) Grow(i uint)

Grow will increase the worker pool by the number provided.

func (*Flexpool) Shrink

func (p *Flexpool) Shrink(i int) error

Shrink will decrease the worker pool by the number provided. Will return an error if the number provided is greater than the worker pool size. Shrink will cancel the pool context if the number provided is the same size as the worker pool.

func (*Flexpool) Size

func (p *Flexpool) Size() int

Size returns the current size of the worker pool.

type Pooli

type Pooli interface {
	// Add will add a job to the worker pool.  This will be process asychronously.
	Add(job)

	// Close will stop any new jobs being added and/or processed.  Close will not
	// force stop any currently running jobs, unless specific pool types allow
	// and specify.
	Close()

	// Size returns the current size of the worker pool.
	Size() int
	// contains filtered or unexported methods
}

Pooli an interface to pool types. All pools returned by this module will be implement Pooli.

func NewPool

func NewPool(workerCount, queueSize int) Pooli

NewPool creates a standard worker pool. This pool will run, processing jobs asynchronously until the application exits. If you need to stop the pool based on a context.Context being closed, use `NewCtxPool()`

Example
package main

import (
	"fmt"
	"sync"
	"time"
)

var (
	exampleWG sync.WaitGroup
)

// delayprinter is the struct used to perform asynchonous jobs on.
// This will be used as an example of using a worker pool
type delayprinter struct {
	id    int
	delay time.Duration
}

// Action is the function called by the workers from the pool
func (d *delayprinter) Action() {
	time.Sleep(d.delay)
	fmt.Printf("Performed action on ID: %d\n", d.id)

	exampleWG.Done()
}

func main() {
	// Create two delayprinters
	d1 := &delayprinter{1, time.Second * 2}
	d2 := &delayprinter{2, time.Second}

	// Create a worker pool
	pool := NewPool(10, 5)

	exampleWG.Add(2)

	// Add the jobs to the worker pool
	pool.Add(d1)
	pool.Add(d2)

	// Wait for the goroutines to complete
	exampleWG.Wait()

}
Output:
Performed action on ID: 2
Performed action on ID: 1

Jump to

Keyboard shortcuts

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