optimization for timed task to clear idle workers

This commit is contained in:
Andy Pan 2018-07-08 10:40:27 +08:00
commit 8d3f3cbee2
7 changed files with 23 additions and 12 deletions

View File

@ -84,7 +84,7 @@ func main() {
// use the pool with a function // use the pool with a function
// set 10 the size of goroutine pool and 1 second for expired duration // set 10 the size of goroutine pool and 1 second for expired duration
p, _ := ants.NewPoolWithFunc(10, 1, func(i interface{}) error { p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
myFunc(i) myFunc(i)
wg.Done() wg.Done()
return nil return nil

View File

@ -85,7 +85,7 @@ func main() {
// use the pool with a function // use the pool with a function
// set 10 the size of goroutine pool and 1 second for expired duration // set 10 the size of goroutine pool and 1 second for expired duration
p, _ := ants.NewPoolWithFunc(10, 1, func(i interface{}) error { p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
myFunc(i) myFunc(i)
wg.Done() wg.Done()
return nil return nil

View File

@ -36,7 +36,7 @@ const (
) )
// Init a instance pool when importing ants // Init a instance pool when importing ants
var defaultPool, _ = NewPool(DefaultPoolSize, DefaultCleanIntervalTime) var defaultPool, _ = NewPool(DefaultPoolSize)
// Submit submit a task to pool // Submit submit a task to pool
func Submit(task f) error { func Submit(task f) error {

View File

@ -78,7 +78,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
func BenchmarkAntsPoolWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) {
var wg sync.WaitGroup var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(50000, 1, func(i interface{}) error { p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error {
demoPoolFunc(i) demoPoolFunc(i)
wg.Done() wg.Done()
return nil return nil
@ -104,7 +104,7 @@ func BenchmarkGoroutine(b *testing.B) {
} }
func BenchmarkAntsPool(b *testing.B) { func BenchmarkAntsPool(b *testing.B) {
p, _ := ants.NewPoolWithFunc(50000, 1, demoPoolFunc) p, _ := ants.NewPoolWithFunc(50000, demoPoolFunc)
defer p.Release() defer p.Release()
b.ResetTimer() b.ResetTimer()

View File

@ -67,7 +67,7 @@ func main() {
// use the pool with a function // use the pool with a function
// set 10 the size of goroutine pool and 1 second for expired duration // set 10 the size of goroutine pool and 1 second for expired duration
p, _ := ants.NewPoolWithFunc(10, 1, func(i interface{}) error { p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
myFunc(i) myFunc(i)
wg.Done() wg.Done()
return nil return nil

11
pool.go
View File

@ -62,9 +62,9 @@ type Pool struct {
} }
func (p *Pool) monitorAndClear() { func (p *Pool) monitorAndClear() {
heartbeat := time.NewTicker(p.expiryDuration)
go func() { go func() {
for { for range heartbeat.C {
time.Sleep(p.expiryDuration)
currentTime := time.Now() currentTime := time.Now()
p.lock.Lock() p.lock.Lock()
idleWorkers := p.workers idleWorkers := p.workers
@ -88,7 +88,12 @@ func (p *Pool) monitorAndClear() {
} }
// NewPool generates a instance of ants pool // NewPool generates a instance of ants pool
func NewPool(size, expiry int) (*Pool, error) { func NewPool(size int) (*Pool, error) {
return NewTimingPool(size, DefaultCleanIntervalTime)
}
// NewTimingPool generates a instance of ants pool with a custom timed task
func NewTimingPool(size, expiry int) (*Pool, error) {
if size <= 0 { if size <= 0 {
return nil, ErrPoolSizeInvalid return nil, ErrPoolSizeInvalid
} }

View File

@ -63,8 +63,9 @@ type PoolWithFunc struct {
} }
func (p *PoolWithFunc) monitorAndClear() { func (p *PoolWithFunc) monitorAndClear() {
heartbeat := time.NewTicker(p.expiryDuration)
go func() { go func() {
for { for range heartbeat.C {
time.Sleep(p.expiryDuration) time.Sleep(p.expiryDuration)
currentTime := time.Now() currentTime := time.Now()
p.lock.Lock() p.lock.Lock()
@ -88,8 +89,13 @@ func (p *PoolWithFunc) monitorAndClear() {
}() }()
} }
// NewPoolWithFunc generates a instance of ants pool with a specific function. // NewPoolWithFunc generates a instance of ants pool with a specific function
func NewPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) { func NewPoolWithFunc(size int, f pf) (*PoolWithFunc, error) {
return NewTimingPoolWithFunc(size, DefaultCleanIntervalTime, f)
}
// NewTimingPoolWithFunc generates a instance of ants pool with a specific function and a custom timed task
func NewTimingPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) {
if size <= 0 { if size <= 0 {
return nil, ErrPoolSizeInvalid return nil, ErrPoolSizeInvalid
} }