From 8556a67f0f3e08c5f4a40b2f128edae61ba6df1d Mon Sep 17 00:00:00 2001 From: andy pan Date: Wed, 29 Aug 2018 09:53:07 +0800 Subject: [PATCH 1/3] fixed workers leak --- pool.go | 7 +++---- pool_func.go | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/pool.go b/pool.go index 656bf19..7a7a48e 100644 --- a/pool.go +++ b/pool.go @@ -67,7 +67,7 @@ func (p *Pool) periodicallyPurge() { p.lock.Unlock() return } - n := 0 + n := -1 for i, w := range idleWorkers { if currentTime.Sub(w.recycleTime) <= p.expiryDuration { break @@ -76,12 +76,11 @@ func (p *Pool) periodicallyPurge() { w.task <- nil idleWorkers[i] = nil } - n++ - if n > 0 { + if n > -1 { if n >= len(idleWorkers) { p.workers = idleWorkers[:0] } else { - p.workers = idleWorkers[n:] + p.workers = idleWorkers[n+1:] } } p.lock.Unlock() diff --git a/pool_func.go b/pool_func.go index 0b2eed5..7c0df59 100644 --- a/pool_func.go +++ b/pool_func.go @@ -68,7 +68,7 @@ func (p *PoolWithFunc) periodicallyPurge() { p.lock.Unlock() return } - n := 0 + n := -1 for i, w := range idleWorkers { if currentTime.Sub(w.recycleTime) <= p.expiryDuration { break @@ -77,12 +77,11 @@ func (p *PoolWithFunc) periodicallyPurge() { w.args <- nil idleWorkers[i] = nil } - n++ - if n > 0 { + if n > -1 { if n >= len(idleWorkers) { p.workers = idleWorkers[:0] } else { - p.workers = idleWorkers[n:] + p.workers = idleWorkers[n+1:] } } p.lock.Unlock() From e25a58c9f36091dd67427947cf9aef059623dd54 Mon Sep 17 00:00:00 2001 From: andy pan Date: Wed, 29 Aug 2018 10:02:41 +0800 Subject: [PATCH 2/3] optimization for cleaning workers --- pool.go | 2 +- pool_func.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pool.go b/pool.go index 7a7a48e..987ba4d 100644 --- a/pool.go +++ b/pool.go @@ -77,7 +77,7 @@ func (p *Pool) periodicallyPurge() { idleWorkers[i] = nil } if n > -1 { - if n >= len(idleWorkers) { + if n >= len(idleWorkers)-1 { p.workers = idleWorkers[:0] } else { p.workers = idleWorkers[n+1:] diff --git a/pool_func.go b/pool_func.go index 7c0df59..6051117 100644 --- a/pool_func.go +++ b/pool_func.go @@ -78,7 +78,7 @@ func (p *PoolWithFunc) periodicallyPurge() { idleWorkers[i] = nil } if n > -1 { - if n >= len(idleWorkers) { + if n >= len(idleWorkers)-1 { p.workers = idleWorkers[:0] } else { p.workers = idleWorkers[n+1:] From b1e6e05756723ffd9fe6c748fac11c139892259f Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Fri, 31 Aug 2018 23:13:22 +0800 Subject: [PATCH 3/3] update --- ants_test.go | 40 +--------------------------------------- 1 file changed, 1 insertion(+), 39 deletions(-) diff --git a/ants_test.go b/ants_test.go index 83519e8..aff9053 100644 --- a/ants_test.go +++ b/ants_test.go @@ -130,42 +130,4 @@ func TestCodeCov(t *testing.T) { p.ReSize(TestSize) p.ReSize(AntsSize) t.Logf("pool with func, after resize, capacity:%d, running:%d", p.Cap(), p.Running()) -} - -// func TestNoPool(t *testing.T) { -// var wg sync.WaitGroup -// for i := 0; i < n; i++ { -// wg.Add(1) -// go func() { -// demoPoolFunc(n) -// wg.Done() -// }() -// } - -// wg.Wait() -// mem := runtime.MemStats{} -// runtime.ReadMemStats(&mem) -// t.Logf("memory usage:%d", mem.TotalAlloc/GiB) -// } - -//func TestCustomPool(t *testing.T) { -// p, _ := ants.NewPool(30000) -// var wg sync.WaitGroup -// for i := 0; i < n; i++ { -// wg.Add(1) -// p.Submit(func() { -// demoFunc() -// //demoFunc() -// wg.Done() -// }) -// } -// wg.Wait() -// -// //t.Logf("pool capacity:%d", p.Cap()) -// //t.Logf("free workers number:%d", p.Free()) -// -// t.Logf("running workers number:%d", p.Running()) -// mem := runtime.MemStats{} -// runtime.ReadMemStats(&mem) -// t.Logf("memory usage:%d", mem.TotalAlloc/1024) -//} +} \ No newline at end of file