Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/loveleshsharma/gohive
🐝 A Highly Performant and easy to use goroutine pool for Go
https://github.com/loveleshsharma/gohive
async asynchronous-tasks concurren go goroutine goroutine-pool goroutine-scheduling pool task-queue task-scheduler
Last synced: about 2 months ago
JSON representation
🐝 A Highly Performant and easy to use goroutine pool for Go
- Host: GitHub
- URL: https://github.com/loveleshsharma/gohive
- Owner: loveleshsharma
- License: mit
- Created: 2019-05-31T10:44:24.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-19T05:43:55.000Z (about 1 year ago)
- Last Synced: 2024-07-31T20:51:54.230Z (5 months ago)
- Topics: async, asynchronous-tasks, concurren, go, goroutine, goroutine-pool, goroutine-scheduling, pool, task-queue, task-scheduler
- Language: Go
- Homepage:
- Size: 187 KB
- Stars: 52
- Watchers: 4
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - gohive - A highly performant and easy to use Goroutine pool for Go. (Goroutines / Search and Analytic Databases)
- awesome-go-extra - gohive - 05-31T10:44:24Z|2021-11-27T10:45:02Z| (Goroutines / Advanced Console UIs)
README
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
$ go get -u github.com/loveleshsharma/gohive
```## Usage
- Create an instance of Pool type first
```go
hive := gohive.NewFixedPool(5)
```- Invoke the Submit() function and pass the task to execute
```go
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
```go
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
```go
package mainimport (
"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.