参考 github.com/panjf2000/ants
package main
import (
"fmt"
"sync"
"sync/atomic"
"time"
_ "github.com/panjf2000/ants"
)
type pool struct {
// 协程池最大容量
cap int32
// 当前运行的协程个数
run int32
block bool
// 空闲goroutinue
idleWorkers []*worker
// 最大空闲时间,超过就挥手
idleTimeoutSec uint32
lock sync.Mutex
cond *sync.Cond
close chan struct{
}
wg sync.WaitGroup
}
func NewPool(cap int32) *pool {
c := &pool{
cap: cap,
//idleWorkers: make([]*worker, 0, cap),
close: make(chan struct{
}),
}
c.cond = sync.NewCond(&c.lock)
go c.checkIdleWork()
return c
}
func (p *pool) checkIdleWork() {
for {
select {
case <-time.After(time.Second * 5):
{
p.lock.Lock()
defer p.lock.Unlock()
var i = 0
for ; i < len(p.idleWorkers) &&
uint32(time.Now()