From 05fbdb8d5bdb7b79a9dc063b1e3ba6ee49645731 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 13 Apr 2019 11:15:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=AD=20Fix=20race=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ants_test.go | 62 ++++++++++++++++++++++++++-------------------------- pool.go | 1 - pool_func.go | 1 - 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/ants_test.go b/ants_test.go index cf66f00..9cec37e 100644 --- a/ants_test.go +++ b/ants_test.go @@ -95,40 +95,40 @@ func TestAntsPoolWithFuncWaitToGetWorker(t *testing.T) { t.Logf("memory usage:%d MB", curMem) } -// TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool. -// func TestAntsPoolGetWorkerFromCache(t *testing.T) { -// p, _ := ants.NewPool(TestSize) -// defer p.Release() +TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool. +func TestAntsPoolGetWorkerFromCache(t *testing.T) { + p, _ := ants.NewPool(TestSize) + defer p.Release() -// for i := 0; i < AntsSize; i++ { -// p.Submit(demoFunc) -// } -// time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second) -// p.Submit(demoFunc) -// t.Logf("pool, running workers number:%d", p.Running()) -// mem := runtime.MemStats{} -// runtime.ReadMemStats(&mem) -// curMem = mem.TotalAlloc/MiB - curMem -// t.Logf("memory usage:%d MB", curMem) -// } + for i := 0; i < AntsSize; i++ { + p.Submit(demoFunc) + } + time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second) + p.Submit(demoFunc) + t.Logf("pool, running workers number:%d", p.Running()) + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + curMem = mem.TotalAlloc/MiB - curMem + t.Logf("memory usage:%d MB", curMem) +} -// TestAntsPoolWithFuncGetWorkerFromCache is used to test getting worker from sync.Pool. -// func TestAntsPoolWithFuncGetWorkerFromCache(t *testing.T) { -// dur := 10 -// p, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc) -// defer p.Release() +TestAntsPoolWithFuncGetWorkerFromCache is used to test getting worker from sync.Pool. +func TestAntsPoolWithFuncGetWorkerFromCache(t *testing.T) { + dur := 10 + p, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc) + defer p.Release() -// for i := 0; i < AntsSize; i++ { -// p.Invoke(dur) -// } -// time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second) -// p.Invoke(dur) -// t.Logf("pool with func, running workers number:%d", p.Running()) -// mem := runtime.MemStats{} -// runtime.ReadMemStats(&mem) -// curMem = mem.TotalAlloc/MiB - curMem -// t.Logf("memory usage:%d MB", curMem) -// } + for i := 0; i < AntsSize; i++ { + p.Invoke(dur) + } + time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second) + p.Invoke(dur) + t.Logf("pool with func, running workers number:%d", p.Running()) + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + curMem = mem.TotalAlloc/MiB - curMem + t.Logf("memory usage:%d MB", curMem) +} //------------------------------------------------------------------------------------------- // Contrast between goroutines without a pool and goroutines with ants pool. diff --git a/pool.go b/pool.go index bfeeabb..cf951d2 100644 --- a/pool.go +++ b/pool.go @@ -235,6 +235,5 @@ func (p *Pool) revertWorker(worker *Worker) bool { // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. p.cond.Signal() p.lock.Unlock() - p.workerCache.Put(worker) return true } diff --git a/pool_func.go b/pool_func.go index c0a8bbd..7d3d799 100644 --- a/pool_func.go +++ b/pool_func.go @@ -239,6 +239,5 @@ func (p *PoolWithFunc) revertWorker(worker *WorkerWithFunc) bool { // Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue. p.cond.Signal() p.lock.Unlock() - p.workerCache.Put(worker) return true }