gopool

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2021 License: Apache-2.0 Imports: 0 Imported by: 4

README

Go Pool

Simple implementation of a concurrency pool with limited concurrency

Example usage:

package main

import (
	"log"
	"time"

	"github.com/aherve/gopool"
)

func main() {

	pool := gopool.NewPool(3) // creates pool with limited concurrency of 3
	for i := 0; i < 10; i++ {
		pool.Add(1) // This will wait until a slot is available
		go work(i, pool)
	}

	pool.Wait()
	log.Println("All Done !")
}

func work(i int, pool *gopool.GoPool) {
	defer pool.Done() // just like with sync.WaitGroup
	log.Printf("working hard on %v", i)
	time.Sleep(time.Second)
	log.Printf("%v is done", i)
}

Documentation

Overview

Package gopool provides Add, Done and Wait methods just like sync.WaitGroup would do. On top of that, a max concurrency is applied so that the number of goroutines stays under control

Example
package main

import (
	"log"
	"time"

	"github.com/aherve/gopool"
)

func main() {

	pool := gopool.NewPool(3) // creates pool with limited concurrency of 3
	for i := 0; i < 10; i++ {
		pool.Add(1) // This will pause until a slot is available
		go work(i, pool)
	}

	pool.Wait()
	log.Println("All Done !")
}

func work(i int, pool *gopool.GoPool) {
	defer pool.Done() // just like with sync.WaitGroup
	log.Printf("working hard on %v", i)
	time.Sleep(time.Second)
	log.Printf("%v is done", i)
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GoPool

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

GoPool describe a concurrency pool

func NewPool

func NewPool(maxConcurrency int) *GoPool

NewPool creates a new pool with max concurrency size

func (*GoPool) Add

func (gp *GoPool) Add(n int)

Add declares new tasks

func (*GoPool) Done

func (gp *GoPool) Done()

Done frees a slot

func (*GoPool) Wait

func (gp *GoPool) Wait()

Wait can be used to wait for all goroutines to finish

Jump to

Keyboard shortcuts

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