Goes 是一个简单易用且轻量的异步任务执行库。
- 支持只限制同时执行的协程数,但不复用协程,使用 Limiter。
- 支持限制同时执行的协程数,且复用协程,使用 Executor。
- 支持多种调度策略,包括轮询、随机等。
- 支持使用退避策略的自旋锁。
- 支持查询可用的 worker 数量。
- 支持 worker 动态扩缩容。
历史版本的特性请查看 HISTORY.md。未来版本的新特性和计划请查看 FUTURE.md。
$ go get -u github.com/FishGoddess/goespackage main
import (
"fmt"
"time"
"github.com/FishGoddess/goes"
)
func main() {
// Limits the number of simultaneous goroutines and not reuses them.
limiter := goes.NewLimiter(4)
for i := 0; i < 20; i++ {
limiter.Go(func() {
fmt.Printf("limiter --> %s\n", time.Now())
time.Sleep(time.Second)
})
}
limiter.Wait()
// Limits the number of simultaneous goroutines and reuses them.
executor := goes.NewExecutor(4)
defer executor.Close()
for i := 0; i < 20; i++ {
executor.Submit(func() {
fmt.Printf("executor --> %s\n", time.Now())
time.Sleep(time.Second)
})
}
}更多使用案例请查看 _examples 目录。
$ make benchgoos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkLimiter-2 2417040 498.5 ns/op 24 B/op 1 allocs/op
BenchmarkExecutor-2 20458502 58.3 ns/op 0 B/op 0 allocs/op
BenchmarkAntsPool-2 4295964 271.7 ns/op 0 B/op 0 allocs/op
BenchmarkLimiterTime-2: num is 1000000, cost is 300.936441ms
BenchmarkExecutorTime-2: num is 1000000, cost is 63.026947ms
BenchmarkAntsPoolTime-2: num is 999744, cost is 346.972287ms很明显,goes.Executor 的性能比功能更丰富的 ants.Pool 要高出 5 倍左右,所以当你需要一个很轻量且高性能的异步任务执行器时,可以尝试下 goes。
如果您觉得 goes 缺少您需要的功能,请不要犹豫,马上参与进来,发起一个 issue。