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

pooligo

package module
v1.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: 2 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 Pooli

type Pooli interface {
	Add(job)
	Close()
	SetClosed()
}

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

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