pool

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: BSD-3-Clause Imports: 0 Imported by: 0

README

Generic worker pool Go Reference

Go (1.18+) framework to run a pool of N workers

go get github.com/quakephil/generic-worker-pool@v0.2.1

Pool

workers := pool.New[I, O](input, worker, output)
result := workers.Wait(concurrency)

Input

func input(in chan<- I) {
  for ... {
    in <- I{ ... }
  }  
}

Worker

func worker(i I) I {
  i.update = ...
  return i
}

Output

func output(results <-chan I) (out O) {
  for result := range results {
    out.update += ...
  }
  return
}

Examples: https://github.com/QuakePhil/generic-worker-pool-examples

Documentation

Overview

Package pool implements a generic worker pool with shared channels.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool[I, O any] struct {
	// contains filtered or unexported fields
}

Pool contains channels for the generic worker() to run at limited concurrency.

func New

func New[I, O any](
	input func(chan<- I),
	worker func(I) I,
	output func(<-chan I) O,
) (p Pool[I, O])

New creates the channels and kicks off the input producer and the output consumer.

func (Pool[I, O]) Wait

func (p Pool[I, O]) Wait(concurrency int) O

Wait runs workers concurrently and returns the result of output. If there's no worker, input is sent directly to output.

Jump to

Keyboard shortcuts

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