Giter Club home page Giter Club logo

gohive's Introduction

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
  • Accepts tasks which implements Runner interface
  • Uses channels to accepts tasks and gets them executed via workers
  • Uses synchronization among workers to avoid race conditions

Installation

Use go get to install and update:

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

Usage

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

Submit function accepts a Runner object as an argument, which it passes to the pool if a worker is available, otherwise it will wait for the worker to be available

  • 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 (
   "fmt"
   "github.com/loveleshsharma/gohive"
   "sync"
)

func main() {
   var wg sync.WaitGroup
   pool := gohive.NewFixedPool(5)

   for i := 1; i <= 20; i++ {
      if err := pool.Submit(NewMyStruct(i, &wg)); err != nil {
         fmt.Println("error: ", err)
         break
      }
   }

   wg.Wait()
}

type MyStruct struct {
   num int
   wg  *sync.WaitGroup
}

func NewMyStruct(num int, wg *sync.WaitGroup) MyStruct {
   myStruct := MyStruct{
      num: num,
      wg:  wg,
   }
   wg.Add(1)
   return myStruct
}

func (s MyStruct) Run() {
   defer s.wg.Done()
   val := s.num
   fact := s.num
   for i := s.num - 1; i > 0; i-- {
      fact *= i
   }

   fmt.Printf("Factorial of %d: %d\n", val, fact)
}

Important : Always keep sync.WaitGroup in your struct and put defer wg.Done() as the first statement of your Run() function. It will wait for your task to complete.

TODO

  1. Maintain a waiting queue to stop blocking submit method when all goroutines are busy.
  2. Submitting priority tasks which takes priority over other tasks.
  3. Handling panics inside goroutines to prevent them from crashing.
  4. Implement dynamic pool which will scale the number of goroutines as per requirement and scales down when they are idle.
  5. Submitting multiple tasks together.

gohive's People

Contributors

loveleshsharma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gohive's Issues

Can I use gohive for executing task one by one?

Can I use gohive for executing tasks one by one like a serial queue?

For example:

     task1  ->  task2 -> task3

when task1 is completed, task2 will be executed, and when task2 is completed, task3 will be executed.

Finally, If yes, how to do it using gohive, can you give some examples in the Readme?

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.