diff --git a/ants.go b/ants.go index a0f4aa9..f22782e 100644 --- a/ants.go +++ b/ants.go @@ -65,6 +65,7 @@ func Release() { // Errors for the Ants API var ( - ErrPoolSizeInvalid = errors.New("invalid size for pool") - ErrPoolClosed = errors.New("this pool has been closed") + ErrInvalidPoolSize = errors.New("invalid size for pool") + ErrInvalidPoolExpiry = errors.New("invalid expiry for pool") + ErrPoolClosed = errors.New("this pool has been closed") ) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index ef070fd..7ef3e89 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -41,8 +41,11 @@ const ( ZiB // 1180591620717411303424 (超过了int64的范围) YiB // 1208925819614629174706176 ) -const RunTimes = 1000000 -const loop = 10 +const ( + RunTimes = 1000000 + Param = 10 + AntsSize = 100000 +) func demoFunc() error { n := 10 @@ -68,7 +71,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) { for j := 0; j < RunTimes; j++ { wg.Add(1) go func() { - demoPoolFunc(loop) + demoPoolFunc(Param) wg.Done() }() } @@ -78,7 +81,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) { var wg sync.WaitGroup - p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { + p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error { demoPoolFunc(i) wg.Done() return nil @@ -88,7 +91,7 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) { for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { wg.Add(1) - p.Serve(loop) + p.Serve(Param) } wg.Wait() b.Logf("running goroutines: %d", p.Running()) @@ -98,19 +101,19 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) { func BenchmarkGoroutine(b *testing.B) { for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { - go demoPoolFunc(loop) + go demoPoolFunc(Param) } } } func BenchmarkAntsPool(b *testing.B) { - p, _ := ants.NewPoolWithFunc(50000, demoPoolFunc) + p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc) defer p.Release() b.ResetTimer() for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { - p.Serve(loop) + p.Serve(Param) } // b.Logf("running goroutines: %d", p.Running()) } diff --git a/pool.go b/pool.go index 79a2afb..d5069ef 100644 --- a/pool.go +++ b/pool.go @@ -95,7 +95,10 @@ func NewPool(size int) (*Pool, error) { // 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 + return nil, ErrInvalidPoolSize + } + if expiry <= 0 { + return nil, ErrInvalidPoolExpiry } p := &Pool{ capacity: int32(size), diff --git a/pool_func.go b/pool_func.go index f9ac15b..b2ef61e 100644 --- a/pool_func.go +++ b/pool_func.go @@ -66,7 +66,6 @@ func (p *PoolWithFunc) monitorAndClear() { heartbeat := time.NewTicker(p.expiryDuration) go func() { for range heartbeat.C { - time.Sleep(p.expiryDuration) currentTime := time.Now() p.lock.Lock() idleWorkers := p.workers @@ -81,7 +80,7 @@ func (p *PoolWithFunc) monitorAndClear() { p.running-- } if n > 0 { - n += 1 + n++ p.workers = idleWorkers[n:] } p.lock.Unlock() @@ -97,7 +96,10 @@ func NewPoolWithFunc(size int, f pf) (*PoolWithFunc, error) { // 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 { - return nil, ErrPoolSizeInvalid + return nil, ErrInvalidPoolSize + } + if expiry <= 0 { + return nil, ErrInvalidPoolExpiry } p := &PoolWithFunc{ capacity: int32(size),