ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - loveleshsharma/gohive: ๐Ÿ A Highly Performant and easy to use goroutine pool for Go
๐Ÿ A Highly Performant and easy to use goroutine pool for Go - loveleshsharma/gohive
Visit Site

GitHub - loveleshsharma/gohive: ๐Ÿ A Highly Performant and easy to use goroutine pool for Go

GitHub - loveleshsharma/gohive: ๐Ÿ A Highly Performant 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.

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory