From a0377f758e7df208307ee8245de40ebea07be539 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 31 Mar 2016 14:56:39 +0300 Subject: [PATCH] workerpool: test cleaner --- workerpool.go | 15 ++++++++++++--- workerpool_test.go | 5 +++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/workerpool.go b/workerpool.go index 222adbe..e0dd8a6 100644 --- a/workerpool.go +++ b/workerpool.go @@ -23,6 +23,8 @@ type workerPool struct { LogAllErrors bool + MaxIdleWorkerDuration time.Duration + Logger Logger lock sync.Mutex @@ -50,13 +52,13 @@ func (wp *workerPool) Start() { go func() { var scratch []*workerChan for { + wp.clean(&scratch) select { case <-stopCh: return default: - time.Sleep(10 * time.Second) + time.Sleep(wp.getMaxIdleWorkerDuration()) } - wp.clean(&scratch) } }() } @@ -82,9 +84,16 @@ func (wp *workerPool) Stop() { wp.lock.Unlock() } -const maxIdleWorkerDuration = 60 * time.Second +func (wp *workerPool) getMaxIdleWorkerDuration() time.Duration { + if wp.MaxIdleWorkerDuration <= 0 { + return 10 * time.Second + } + return wp.MaxIdleWorkerDuration +} func (wp *workerPool) clean(scratch *[]*workerChan) { + maxIdleWorkerDuration := wp.getMaxIdleWorkerDuration() + // Clean least recently used workers if they didn't serve connections // for more than maxIdleWorkerDuration. currentTime := time.Now() diff --git a/workerpool_test.go b/workerpool_test.go index dbe230c..93ca62b 100644 --- a/workerpool_test.go +++ b/workerpool_test.go @@ -204,8 +204,9 @@ func testWorkerPoolPanicErrorMulti(t *testing.T) { } return nil }, - MaxWorkersCount: 1000, - Logger: &customLogger{}, + MaxWorkersCount: 1000, + MaxIdleWorkerDuration: time.Millisecond, + Logger: &customLogger{}, } for i := 0; i < 10; i++ {