use sync.Pool to recycle goroutines

This commit is contained in:
Andy Pan 2018-05-20 02:52:39 +08:00
parent a59f51fefd
commit 91349c14bb
4 changed files with 47 additions and 82 deletions

View File

@ -1,6 +1,6 @@
package ants package ants
const DEFAULT_POOL_SIZE = 10000 const DEFAULT_POOL_SIZE = 50000
var defaultPool = NewPool(DEFAULT_POOL_SIZE) var defaultPool = NewPool(DEFAULT_POOL_SIZE)

View File

@ -29,9 +29,9 @@ func TestDefaultPool(t *testing.T) {
ants.Push(demoFunc) ants.Push(demoFunc)
} }
t.Logf("pool capacity:%d", ants.Cap()) //t.Logf("pool capacity:%d", ants.Cap())
t.Logf("running workers number:%d", ants.Running()) //t.Logf("running workers number:%d", ants.Running())
t.Logf("free workers number:%d", ants.Free()) //t.Logf("free workers number:%d", ants.Free())
ants.Wait() ants.Wait()
@ -69,5 +69,4 @@ func TestNoPool(t *testing.T) {
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) runtime.ReadMemStats(&mem)
t.Logf("memory usage:%d", mem.TotalAlloc/1024) t.Logf("memory usage:%d", mem.TotalAlloc/1024)
} }

59
pool.go
View File

@ -4,7 +4,6 @@ import (
"runtime" "runtime"
"sync/atomic" "sync/atomic"
"sync" "sync"
"math"
) )
type sig struct{} type sig struct{}
@ -12,57 +11,39 @@ type sig struct{}
type f func() type f func()
type Pool struct { type Pool struct {
capacity int32 capacity int32
running int32 running int32
tasks *ConcurrentQueue freeSignal chan sig
workers *ConcurrentQueue workers []*Worker
freeSignal chan sig workerPool sync.Pool
launchSignal chan sig destroy chan sig
destroy chan sig m sync.Mutex
m *sync.Mutex wg *sync.WaitGroup
wg *sync.WaitGroup
} }
func NewPool(size int) *Pool { func NewPool(size int) *Pool {
p := &Pool{ p := &Pool{
capacity: int32(size), capacity: int32(size),
tasks: NewConcurrentQueue(), freeSignal: make(chan sig, size),
workers: NewConcurrentQueue(), destroy: make(chan sig, runtime.GOMAXPROCS(-1)),
freeSignal: make(chan sig, math.MaxInt32), wg: &sync.WaitGroup{},
launchSignal: make(chan sig, math.MaxInt32),
destroy: make(chan sig, runtime.GOMAXPROCS(-1)),
wg: &sync.WaitGroup{},
} }
p.loop()
return p return p
} }
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
func (p *Pool) loop() {
for i := 0; i < runtime.GOMAXPROCS(-1); i++ {
go func() {
for {
select {
case <-p.launchSignal:
p.getWorker().sendTask(p.tasks.pop().(f))
case <-p.destroy:
return
}
}
}()
}
}
func (p *Pool) Push(task f) error { func (p *Pool) Push(task f) error {
if len(p.destroy) > 0 { if len(p.destroy) > 0 {
return nil return nil
} }
p.tasks.push(task)
p.wg.Add(1) p.wg.Add(1)
p.launchSignal <- sig{} w := p.getWorker()
w.sendTask(task)
//p.launchSignal <- sig{}
return nil return nil
} }
func (p *Pool) Running() int { func (p *Pool) Running() int {
return int(atomic.LoadInt32(&p.running)) return int(atomic.LoadInt32(&p.running))
} }
@ -102,7 +83,6 @@ func (p *Pool) newWorker() *Worker {
worker := &Worker{ worker := &Worker{
pool: p, pool: p,
task: make(chan f), task: make(chan f),
exit: make(chan sig),
} }
worker.run() worker.run()
atomic.AddInt32(&p.running, 1) atomic.AddInt32(&p.running, 1)
@ -110,15 +90,18 @@ func (p *Pool) newWorker() *Worker {
} }
func (p *Pool) getWorker() *Worker { func (p *Pool) getWorker() *Worker {
if w := p.workers.pop(); w != nil { if w := p.workerPool.Get(); w != nil {
return w.(*Worker) return w.(*Worker)
} }
return p.newWorker() return p.newWorker()
} }
func (p *Pool) putWorker(worker *Worker) { func (p *Pool) putWorker(worker *Worker) {
p.workers.push(worker) p.workerPool.Put(worker)
p.m.Lock()
p.workers = append(p.workers, worker)
if p.reachLimit() { if p.reachLimit() {
p.freeSignal <- sig{} p.freeSignal <- sig{}
} }
p.m.Unlock()
} }

View File

@ -2,64 +2,47 @@ package ants
import ( import (
"sync/atomic" "sync/atomic"
"container/list"
"sync"
) )
type Worker struct { type Worker struct {
pool *Pool pool *Pool
task chan f task chan f
exit chan sig
} }
//func (w *Worker) run() {
// go func() {
// for {
// select {
// case f := <-w.task:
// f()
// w.pool.putWorker(w)
// w.pool.wg.Done()
// case <-w.exit:
// atomic.AddInt32(&w.pool.running, -1)
// return
// }
// }
// }()
//}
func (w *Worker) run() { func (w *Worker) run() {
go func() { go func() {
for { for f := range w.task {
select { if f == nil {
case f := <-w.task:
f()
w.pool.putWorker(w)
w.pool.wg.Done()
case <-w.exit:
atomic.AddInt32(&w.pool.running, -1) atomic.AddInt32(&w.pool.running, -1)
return return
} }
f()
w.pool.putWorker(w)
w.pool.wg.Done()
} }
}() }()
} }
func (w *Worker) stop() { func (w *Worker) stop() {
w.exit <- sig{} w.task <- nil
} }
func (w *Worker) sendTask(task f) { func (w *Worker) sendTask(task f) {
w.task <- task w.task <- task
} }
//--------------------------------------------------------------------------------
type ConcurrentQueue struct {
queue *list.List
m sync.Mutex
}
func NewConcurrentQueue() *ConcurrentQueue {
q := new(ConcurrentQueue)
q.queue = list.New()
return q
}
func (q *ConcurrentQueue) push(v interface{}) {
defer q.m.Unlock()
q.m.Lock()
q.queue.PushFront(v)
}
func (q *ConcurrentQueue) pop() interface{} {
defer q.m.Unlock()
q.m.Lock()
if elem := q.queue.Back(); elem != nil {
return q.queue.Remove(elem)
}
return nil
}