https://github.com/sysulq/goroutine-pool
A simple goroutine pool which can create and release goroutine dynamically, inspired by fasthttp.
https://github.com/sysulq/goroutine-pool
fasthttp golang goroutine goroutine-pool pool
Last synced: 14 days ago
JSON representation
A simple goroutine pool which can create and release goroutine dynamically, inspired by fasthttp.
- Host: GitHub
- URL: https://github.com/sysulq/goroutine-pool
- Owner: sysulq
- License: mit
- Created: 2018-01-19T01:56:58.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2024-11-19T02:38:08.000Z (5 months ago)
- Last Synced: 2025-04-15T11:13:21.228Z (14 days ago)
- Topics: fasthttp, golang, goroutine, goroutine-pool, pool
- Language: Go
- Homepage: https://pkg.go.dev/github.com/hnlq715/goroutine-pool
- Size: 39.1 KB
- Stars: 35
- Watchers: 9
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# goroutine-pool
[](https://github.com/sysulq/goroutine-pool/actions/workflows/go.yml)
[](https://codecov.io/gh/sysulq/goroutine-pool)A simple goroutine pool which can create and release goroutine dynamically, inspired by fasthttp.
# install
```
go get -u -v github.com/sysulq/goroutine-pool
```
# example
```
package mainimport (
"fmt"
pool "github.com/sysulq/goroutine-pool"
"sync"
)func main() {
wg := sync.WaitGroup{}
for i := 0; i < 5; i++ {
wg.Add(1)
pool.Go(func() {
fmt.Println(i)
wg.Done()
})
}
wg.Wait()
fmt.Println()for i := 0; i < 5; i++ {
wg.Add(1)
n := i
pool.Go(func() {
fmt.Println(n)
wg.Done()
})
}
wg.Wait()
}
``````
$ go run main.go
5
5
5
5
54
1
0
2
3
```# benchmarks
## With Wait
```
Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkGoroutine$goos: windows
goarch: amd64
pkg: workerpool
BenchmarkGoroutine-4 1000 1634621 ns/op 65 B/op 1 allocs/op
PASS
ok workerpool 2.047s
Success: Benchmarks passed.
``````
Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkPool$goos: windows
goarch: amd64
pkg: workerpool
BenchmarkPool-4 2000 1146818 ns/op 17 B/op 1 allocs/op
PASS
ok workerpool 2.702s
Success: Benchmarks passed.
```## Without Wait
### cpu run at 100%
```
Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkGoroutineWithoutWait$goos: windows
goarch: amd64
pkg: workerpool
BenchmarkGoroutineWithoutWait-4 1000000 4556 ns/op 517 B/op 1 allocs/op
PASS
ok workerpool 5.649s
Success: Benchmarks passed.
```### cpu relatively low
```
Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkPoolWithoutWait$goos: windows
goarch: amd64
pkg: workerpool
BenchmarkPoolWithoutWait-4 10000000 144 ns/op 3 B/op 0 allocs/op
PASS
ok workerpool 4.812s
Success: Benchmarks passed.
```