aboutsummaryrefslogtreecommitdiff
path: root/workerpool.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2016-03-31 14:56:39 +0300
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2016-03-31 14:56:39 +0300
commita0377f758e7df208307ee8245de40ebea07be539 (patch)
tree90344ea7f8da13e6c62e104c3d51ea609e5e020e /workerpool.go
parentadded workerpool tests (diff)
downloadfasthttp-a0377f758e7df208307ee8245de40ebea07be539.tar.gz
fasthttp-a0377f758e7df208307ee8245de40ebea07be539.tar.bz2
fasthttp-a0377f758e7df208307ee8245de40ebea07be539.zip
workerpool: test cleaner
Diffstat (limited to 'workerpool.go')
-rw-r--r--workerpool.go15
1 files changed, 12 insertions, 3 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()