https://github.com/poabob/grpool
A Practical Goroutine Pool and Easy to Use.
https://github.com/poabob/grpool
go golang goroutine-pool goroutines
Last synced: about 1 year ago
JSON representation
A Practical Goroutine Pool and Easy to Use.
- Host: GitHub
- URL: https://github.com/poabob/grpool
- Owner: POABOB
- License: mit
- Created: 2023-09-13T01:52:01.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-09-18T14:32:58.000Z (over 2 years ago)
- Last Synced: 2025-01-21T11:45:14.418Z (over 1 year ago)
- Topics: go, golang, goroutine-pool, goroutines
- Language: Go
- Homepage: https://pkg.go.dev/github.com/POABOB/grpool
- Size: 59.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# grpool
## Introduction
`grpool` is a groutine pool which can provide a fixed size of capacity, recycle the stale workers.
## Installation
```bash
go get -u github.com/POABOB/grpool
```
## How to use
If you want to process a massive number of jobs, you don't need to use the same number of goroutines. It's wasteful to use excessive memory, leading to high consumption.
### A Simple Example with Default Pool
```go
package main
import (
"fmt"
"strconv"
"sync"
"sync/atomic"
"github.com/POABOB/grpool"
)
var wg sync.WaitGroup
var count int32 = 0
func main() {
// Use default Pool Will use the math.MaxInt32 of capacity.
pool := grpool.NewDefaultPool()
// Release worker resource
defer pool.Release()
for i := 0; i < 10000; i++ {
wg.Add(1)
_ = pool.Schedule(func() {
printFunc(i)
})
}
wg.Wait()
fmt.Printf("running goroutines: %d\n", pool.Running())
}
func printFunc(i int) {
atomic.AddInt32(&count, 1)
fmt.Println("Hello World! I am worker" + strconv.Itoa(i) + " from goroutine pool.")
wg.Done()
}
```
### Limit the goroutines and pre-alloc it
```go
package main
import (
"fmt"
"strconv"
"sync"
"sync/atomic"
"github.com/POABOB/grpool"
)
var count int32 = 0
var wg sync.WaitGroup
func main() {
// init a capacity of 18 goroutine pool with preallocing the space.
pool, _ := grpool.NewPool(18, grpool.WithPreAlloc(true))
// Release worker resource
defer pool.Release()
for i := 0; i < 10; i++ {
wg.Add(1)
_ = pool.Schedule(func() {
printFunc(i)
})
}
wg.Wait()
fmt.Printf("running goroutines: %d\n", pool.Running())
}
func printFunc(i int) {
atomic.AddInt32(&count, 1)
fmt.Println("Hello World! I am worker" + strconv.Itoa(i) + " from goroutine pool.")
wg.Done()
}
```
### Use non-blocking pool
```go
pool, err := grpool.NewPool(1000, grpool.WithNonblocking(true))
```
### Customize panic handler
```go
func ph(v interface{}) {
// dosomething...
fmt.Printf("[panic occurred]: %v\n", v)
}
pool, err := grpool.NewPool(1000, grpool.WithPanicHandler(ph))
```
### Customize the time interval of clear stale worker
```go
pool, err := grpool.NewPool(1000, grpool.WithExpiryDuration(time.Second * 5))
```
## License
[MIT License](https://github.com/POABOB/grpool/blob/main/LICENSE)