goroutine pool
- resize worker num
- worker idle timeout
- aysnc & sync mode
package main
import (
"fmt"
"sync"
"time"
"github.com/rfyiamcool/gpool"
)
var (
wg = sync.WaitGroup{}
)
func main() {
gp, err := gpool.NewGPool(&gpool.Options{
MaxWorker: 5, // ζ倧ηεη¨ζ°
MinWorker: 2, // ζε°ηεη¨ζ°
JobBuffer: 1, // ηΌε²ιεη倧ε°
IdleTimeout: 120 * time.Second, // εη¨ηη©Ίι²θΆ
ζΆιεΊζΆι΄
})
if err != nil {
panic(err.Error())
}
for index := 0; index < 1000; index++ {
wg.Add(1)
idx := index
gp.ProcessAsync(func() {
fmt.Println(idx, time.Now())
time.Sleep(1 * time.Second)
wg.Done()
})
}
wg.Done()
}