workerpool

package module
v0.0.0-...-5c3dbc7 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2023 License: MIT Imports: 3 Imported by: 0

README

Worker Pool

Go Reference Go Report Card Codecov

This library is a fork of Dirk Pahl's kyoo. I have added more comments and refactored a little for better understandability.

This is a job queue + worker pool implementation.

design.png

Install

$ go get github.com/arjunsk/workerpool

Usage

package main

import (
	"fmt"
	"github.com/arjunsk/workerpool"
)

func main() {
	wp := workerpool.NewWorkerPool(2)
	wp.Start()

	for i := int32(0); i < 10; i++ {
		val := i // create a copy of i
		err := wp.Submit(func() {
			fmt.Printf(" %v ", val)
		})

		if err != nil {
			panic(err)
		}
	}
	wp.Stop()
}

// Output:
// 0  2  3  4  5  6  7  8  9  1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Work

type Work func()

type Worker

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

Worker is responsible for executing the work. There could be many workers in a worker pool.

func NewWorker

func NewWorker(freeWorkerPoolCh chan chan Work, wg *sync.WaitGroup) *Worker

func (*Worker) Start

func (w *Worker) Start()

func (*Worker) Stop

func (w *Worker) Stop()

type WorkerPool

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

func NewWorkerPool

func NewWorkerPool(workerCount int) *WorkerPool

func (*WorkerPool) Start

func (q *WorkerPool) Start()

func (*WorkerPool) Stop

func (q *WorkerPool) Stop()

func (*WorkerPool) Submit

func (q *WorkerPool) Submit(work Work) error

Directories

Path Synopsis
examples
simple command

Jump to

Keyboard shortcuts

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