package module
Version: v0.0.0-...-7eec91c Latest Latest

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

Go to latest
Published: Oct 8, 2019 License: MIT Imports: 5 Imported by: 2



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


  • 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.


Use go get to install and update:

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


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

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

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


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 (

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()



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




View Source
const (
	//OPEN indicates that the pool is open to accept new tasks
	OPEN state = 1

	//CLOSED indicates that the pool is closed and won't accept new tasks
	CLOSED state = 0
View Source
const DefaultPoolSize = 10

DefaultPoolSize is a default size for number of workers in the pool

View Source
const DefaultQueueSize = 10

DefaultQueueSize indicates the default size of the TaskQueue


View Source
var ErrInvalidPoolSize = errors.New("Invalid pool size: pool size must be a positive number")

ErrInvalidPoolSize indicates that the pool size is invalid

View Source
var ErrInvalidPoolState = errors.New("Pool is closed: cannot assign task to a closed pool")

ErrInvalidPoolState indicates that the invalid pool state

View Source
var ErrNilFunction = errors.New("Cannot submit Nil function()")

ErrNilFunction indicates that a nil function submitted


This section is empty.


type PoolService

type PoolService struct {
	// contains filtered or unexported fields

PoolService acts as an orchestrator of the entire GoHive functionality It consists of a pool, that manages workers that run tasks and it consists of a TaskQueue that holds tasks waiting to acquire a worker

func NewDefaultSizePool

func NewDefaultSizePool() *PoolService

NewDefaultSizePool returns PoolService object with the default pool size

func NewFixedSizePool

func NewFixedSizePool(nGoRoutines int) *PoolService

NewFixedSizePool returns PoolService object with the specified pool size

func (*PoolService) ActiveWorkers

func (rs *PoolService) ActiveWorkers() int

ActiveWorkers returns active workers

func (*PoolService) AvailableWorkers

func (rs *PoolService) AvailableWorkers() int

AvailableWorkers returns available workers out of total workers

func (*PoolService) Close

func (rs *PoolService) Close()

Close closes the pool

func (*PoolService) PoolSize

func (rs *PoolService) PoolSize() int

PoolSize returns pool size

func (*PoolService) Submit

func (rs *PoolService) Submit(fun func()) error

Submit submits a new task and assigns it to the pool

type Task

type Task struct {
	// contains filtered or unexported fields

Task represents an executable task

func NewTask

func NewTask(fun func()) Task

NewTask wraps the executable function and returns as Task

type TaskQueue

type TaskQueue struct {
	// contains filtered or unexported fields

TaskQueue represents a queue that holds tasks which are in waiting for workers from the pool

func NewTaskQueue

func NewTaskQueue() *TaskQueue

NewTaskQueue returns new TaskQueue with the default capacity

func (*TaskQueue) DequeueTask

func (wq *TaskQueue) DequeueTask() (Task, error)

DequeueTask returns a task and removes it from the TaskQueue

func (*TaskQueue) EnqueueTask

func (wq *TaskQueue) EnqueueTask(task Task)

EnqueueTask puts a new task in the TaskQueue

func (*TaskQueue) IsNotEmpty

func (wq *TaskQueue) IsNotEmpty() bool

IsNotEmpty returns whether the TaskQueue is empty or not

Jump to

Keyboard shortcuts

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