workerpool

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

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

Go to latest
Published: Jan 29, 2019 License: MIT Imports: 2 Imported by: 1

README

workerpool

Concurrency limiting goroutine pool.

Install

go get -v github.com/neoql/workerpool

Example

package main

import (
    "fmt"
    "time"
    "sync"

    "github.com/neoql/workerpool"
)

func main() {
    var wg sync.WaitGroup

    fn := func(argv workerpool.Argv) {
        no := argv.(int)
        fmt.Printf("fn-%d enter...\n", no)
        time.Sleep(time.Second*3)
        fmt.Printf("fn-%d exit.\n", no)
        wg.Done()
    }

    pool := workerpool.New(2, time.Second*1)

    wg.Add(3)
    for i := 0; i < 3; i++ {
        if !pool.Spawn(fn, i) {
            fmt.Printf("launch fn-%d failed, WorkerPool is full", i)
            fmt.Println("Waiting for an empty position")
            pool.WaitSpawn(fn, i)
        }
    }

    wg.Wait()
}
fn-0 enter...
fn-1 enter...
launch fn-2 failed, WorkerPool is fullWaiting for an empty position
fn-1 exit.
fn-0 exit.
fn-2 enter...
fn-2 exit.

License

MIT, read more here.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Argv

type Argv interface{}

Argv is arguments value

type Fn

type Fn func(argv Argv)

Fn is a function

type WorkerPool

type WorkerPool interface {
	// Spawn returns true if total of workers < size, else false.
	Spawn(Fn, Argv) bool
	// WaitSpawn will spawn, and block when pool is full.
	WaitSpawn(Fn, Argv)
	Start()
	Stop()
}

WorkerPool can manage a collection of workers, each with their own goroutine.

func New

func New(size int, maxIdleWorkerDuration time.Duration) WorkerPool

New creates a new WorkerPool

Jump to

Keyboard shortcuts

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