diff --git a/README.md b/README.md index 4e6156f..f41ca07 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ func main() { // use the pool with a function // 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) wg.Done() return nil diff --git a/README_ZH.md b/README_ZH.md index fb140b2..b39c7a0 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -85,7 +85,7 @@ func main() { // use the pool with a function // 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) wg.Done() return nil diff --git a/ants.go b/ants.go index adfaed8..a0f4aa9 100644 --- a/ants.go +++ b/ants.go @@ -36,7 +36,7 @@ const ( ) // Init a instance pool when importing ants -var defaultPool, _ = NewPool(DefaultPoolSize, DefaultCleanIntervalTime) +var defaultPool, _ = NewPool(DefaultPoolSize) // Submit submit a task to pool func Submit(task f) error { diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index ffff333..ef070fd 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -78,7 +78,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) { var wg sync.WaitGroup - p, _ := ants.NewPoolWithFunc(50000, 1, func(i interface{}) error { + p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { demoPoolFunc(i) wg.Done() return nil @@ -104,7 +104,7 @@ func BenchmarkGoroutine(b *testing.B) { } func BenchmarkAntsPool(b *testing.B) { - p, _ := ants.NewPoolWithFunc(50000, 1, demoPoolFunc) + p, _ := ants.NewPoolWithFunc(50000, demoPoolFunc) defer p.Release() b.ResetTimer() diff --git a/examples/main.go b/examples/main.go index 5a30c56..feef5c4 100644 --- a/examples/main.go +++ b/examples/main.go @@ -67,7 +67,7 @@ func main() { // use the pool with a function // 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) wg.Done() return nil diff --git a/pool.go b/pool.go index 8eda8d3..53044a5 100644 --- a/pool.go +++ b/pool.go @@ -62,9 +62,9 @@ type Pool struct { } func (p *Pool) monitorAndClear() { + heartbeat := time.NewTicker(p.expiryDuration) go func() { - for { - time.Sleep(p.expiryDuration) + for range heartbeat.C{ currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers @@ -88,7 +88,12 @@ func (p *Pool) monitorAndClear() { } // 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 { return nil, ErrPoolSizeInvalid } diff --git a/pool_func.go b/pool_func.go index 7c6caf5..4176d71 100644 --- a/pool_func.go +++ b/pool_func.go @@ -88,8 +88,13 @@ func (p *PoolWithFunc) monitorAndClear() { }() } -// NewPoolWithFunc generates a instance of ants pool with a specific function. -func NewPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) { +// NewTimingPoolWithFunc generates a instance of ants pool with a specific function and a custom timed task +func NewPoolWithFunc(size int, f pf) (*PoolWithFunc, error) { + return NewTimingPoolWithFunc(size, DefaultCleanIntervalTime, f) +} + +// NewPoolWithFunc generates a instance of ants pool with a specific function +func NewTimingPoolWithFunc(size, expiry int, f pf) (*PoolWithFunc, error) { if size <= 0 { return nil, ErrPoolSizeInvalid }