diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2016-03-29 11:05:14 +0300 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2016-03-29 11:05:14 +0300 |
commit | 023e3807b8743d27799e6b5797143da83a7b0e4f (patch) | |
tree | 46bd9e47ba84d7e3d1eb7b03355c7453827e31b2 /workerpool.go | |
parent | Server: write error response when concurrency limit or per-ip conn limit is e... (diff) | |
download | fasthttp-023e3807b8743d27799e6b5797143da83a7b0e4f.tar.gz fasthttp-023e3807b8743d27799e6b5797143da83a7b0e4f.tar.bz2 fasthttp-023e3807b8743d27799e6b5797143da83a7b0e4f.zip |
workerPool optimization: determine the current time outside lock
Diffstat (limited to 'workerpool.go')
-rw-r--r-- | workerpool.go | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/workerpool.go b/workerpool.go index 97f81bd..9f2cfa7 100644 --- a/workerpool.go +++ b/workerpool.go @@ -35,8 +35,8 @@ type workerPool struct { } type workerChan struct { - t time.Time - ch chan net.Conn + lastUseTime time.Time + ch chan net.Conn } func (wp *workerPool) Start() { @@ -82,9 +82,11 @@ const maxIdleWorkerDuration = 10 * time.Second func (wp *workerPool) clean() { // Clean least recently used workers if they didn't serve connections // for more than maxIdleWorkerDuration. + currentTime := time.Now() + wp.lock.Lock() ready := wp.ready - for len(ready) > 1 && time.Since(ready[0].t) > maxIdleWorkerDuration { + for len(ready) > 1 && currentTime.Sub(ready[0].lastUseTime) > maxIdleWorkerDuration { // notify the worker to stop. ready[0].ch <- nil @@ -162,7 +164,7 @@ func (wp *workerPool) getCh() *workerChan { } func (wp *workerPool) release(ch *workerChan) bool { - ch.t = time.Now() + ch.lastUseTime = time.Now() wp.lock.Lock() if wp.mustStop { wp.lock.Unlock() |