🐝 A Highly Performant and easy to use goroutine pool for Go

Overview

gohive

Package gohive implements a simple and easy to use goroutine pool for Go

Features

  • Pool can be created with a specific size as per the requirement
  • Offers efficient performance by implementing sync.Pool, which maintains pool of workers in which workers gets recycled automatically when not in use
  • Implements a Task Queue which can hold surplus tasks in waiting, if submitted more than the pool capacity
  • Implements PoolService type, which acts as an easy to use API with simple methods to interact with gohive
  • Gracefully handles panics and prevent the application from getting crashed or into deadlocks
  • Provides functions like: AvailableWorkers(), ActiveWorkers() and Close() etc.

Installation

Use go get to install and update:

$ go get -u github.com/loveleshsharma/gohive

Usage

  • Create an instance of PoolService type first
hive := gohive.NewFixedSizePool(5)
  • Invoke the Submit() function and pass the task to execute
hive.Submit(someTask())

Submit function accepts a function as an argument, which it passes to the pool if a worker is available, otherwise enqueues it in a waiting queue

  • To close the pool we can invoke the Close() function
hive.Close()

Once the pool is closed, we cannot assign any task to it

Example

Let's get into a full program where we can see how to use the gohive package in order to execute many goroutines simultaneously

package main

import (
	"github.com/loveleshsharma/gohive"
	"fmt"
	"sync"
)

func main() {

	var wg sync.WaitGroup
	hivePool := gohive.NewFixedSizePool(5)

	//wrap your executable function into another function with wg.Done()
	executableTask := func() {
		defer wg.Done()
		factorial(5)
	}

	wg.Add(1)
	hivePool.Submit(executableTask)

	wg.Wait()
}

func factorial(val int) {
	var fact = val
	var res = 1

	for i := fact; i > 0; i-- {
		res = res * i
	}

	fmt.Printf("Factorial: %v", res)
}

Important : Always put defer wg.Done() as the first statement of your wrapper function. It will prevent your program from deadlocks in case of panics

Workers implements a notifying mechanism, due to which they can notify to the pool that their task is completed and they are available to execute more tasks if in waiting queue

Issues
  • hive.Submit(someTask()) not working properly

    hive.Submit(someTask()) not working properly

    var wg sync.WaitGroup
    hivePool := gohive.NewFixedSizePool(5)
    
    //wrap your executable function into another function with wg.Done()
    executableTask := func(i int) {
    	defer wg.Done()
    	factorial(i)
    }
    
    wg.Add(1)
    hivePool.Submit(executableTask(5))
    
    wg.Wait()
    

    what i get: ./example1.go:22:32: executableTask(5) used as value

    what i expected: Working working properly

    opened by raszia 0
Owner
Lovelesh
Software Enthusiast | Server-Side Engineer | Loves to CoDe❤️
Lovelesh
gpool - a generic context-aware resizable goroutines pool to bound concurrency based on semaphore.

gpool - a generic context-aware resizable goroutines pool to bound concurrency. Installation $ go get github.com/sherifabdlnaby/gpool import "github.c

Sherif Abdel-Naby 81 Aug 21, 2021
:speedboat: a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation

Package pool Package pool implements a limited consumer goroutine or unlimited goroutine pool for easier goroutine handling and cancellation. Features

Go Playgound 642 Oct 24, 2021
Go simple async worker pool

??‍?? worker-pool Go simple async worker pool. ?? ABOUT Worker pool is a software design pattern for achieving concurrency of task execution. Maintain

Rafał Lorenz 81 Sep 27, 2021
Routines was a fixed number thread pool to process the user task, and it would respawn a corresponding new thread when panic

Routines Routines was a fixed number thread pool to process the user task, and it would respawn a corresponding new thread when panic. It supports the

hulk 10 Sep 1, 2021
A goroutine pool for Go

Tunny is a Golang library for spawning and managing a goroutine pool, allowing you to limit work coming from any number of goroutines with a synchrono

Ashley Jeffs 2.7k Oct 17, 2021
goroutine pool in golang

goroutine pool in golang

wksw 1 Oct 20, 2021
🐝 A Highly Performant and easy to use goroutine pool for Go

gohive Package gohive implements a simple and easy to use goroutine pool for Go Features Pool can be created with a specific size as per the requireme

Lovelesh 13 Jul 13, 2021
Lightweight Goroutine pool

grpool Lightweight Goroutine pool Clients can submit jobs. Dispatcher takes job, and sends it to first available worker. When worker is done with proc

Ivan Pusic 659 Oct 14, 2021
Off heap golang memory pool

Stealthpool stealthpool provides a memory pool that allocates blocks off-heap that will NOT be tracked by the garbage collector. The name stealthpool

null 54 Oct 13, 2021
Queue is a Golang library for spawning and managing a Goroutine pool

Queue is a Golang library for spawning and managing a Goroutine pool, Alloowing you to create multiple worker according to limit CPU number of machine.

golang-queue 74 Oct 21, 2021
Provides some convenient API, includes Goid(), AllGoid(), and LocalStorage, which is a goroutine's local storage, just like ThreadLocal in other languages.

routine 中文版 routine encapsulates and provides some easy-to-use, high-performance goroutine context access interfaces, which can help you access corout

null 27 Oct 13, 2021
Golang simple thread pool implementation

Golang Threadpool implementation Scalable threadpool implementation using Go to handle the huge network trafic. Install go get github.com/shettyh/thre

Manjunath Shetty 61 Sep 12, 2021
golang worker pool , Concurrency limiting goroutine pool

golang worker pool 中文说明 Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks su

xxj 328 Oct 21, 2021
Reactive Extensions for the Go language.

RxGo Reactive Extensions for the Go Language ReactiveX ReactiveX, or Rx for short, is an API for programming with Observable streams. This is the offi

ReactiveX 3.8k Oct 22, 2021
errgroup with goroutine worker limits

neilotoole/errgroup neilotoole/errgroup is a drop-in alternative to Go's wonderful sync/errgroup but limited to N goroutines. This is useful for inter

Neil O'Toole 94 Sep 28, 2021
高性能go协程池,代码清晰易懂

gowp 高性能go协程池,代码清晰易懂 安装 go get github.com/linzhepeng/gowp 为什么要用协程池 go的协程虽然轻量级,单个goroutine只有2KB,但毫无止境地使用也会占据大量的系统资源,导致调度性能下降、GC 频繁、内存暴涨, 引发一系列问题。 可以测试如

Bing 3 May 6, 2021
Concurrency limiting goroutine pool

workerpool Concurrency limiting goroutine pool. Limits the concurrency of task execution, not the number of tasks queued. Never blocks submitting task

Andrew Gillis 654 Oct 22, 2021
This repository collects common concurrency patterns in Golang

Go Concurrency Patterns This repository collects common concurrency patterns in Golang Materials Concurrency is not parallelism Go Concurrency Pattern

Kha Nguyen 1.2k Oct 24, 2021
A simple and useful goroutine concurrent library.

Taskgroup A simple and useful goroutine concurrent library. Installation go get github.com/anthhub/taskgroup

Tangqy 4 May 19, 2021