diff options
author | Aliaksandr Valialkin <valyala@gmail.com> | 2015-11-26 12:38:18 +0200 |
---|---|---|
committer | Aliaksandr Valialkin <valyala@gmail.com> | 2015-11-26 12:38:18 +0200 |
commit | bc85e2b572889922b1d5387b4bc4f8a3896412f8 (patch) | |
tree | 5e042d6fee8259b885d6f674f47e4b16abb4afae /workerpool.go | |
parent | Skip br.Buffered() verification, since it is guaranteed it is higher than 0 i... (diff) | |
download | fasthttp-bc85e2b572889922b1d5387b4bc4f8a3896412f8.tar.gz fasthttp-bc85e2b572889922b1d5387b4bc4f8a3896412f8.tar.bz2 fasthttp-bc85e2b572889922b1d5387b4bc4f8a3896412f8.zip |
workerpool: immediately switch to connection processing if GOMAXPROCS=1. This improves single-threaded server performance by 1-2%
Diffstat (limited to 'workerpool.go')
-rw-r--r-- | workerpool.go | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/workerpool.go b/workerpool.go index 2a57c53..cbab951 100644 --- a/workerpool.go +++ b/workerpool.go @@ -2,6 +2,7 @@ package fasthttp import ( "net" + "runtime" "runtime/debug" "strings" "sync" @@ -98,6 +99,20 @@ func (wp *workerPool) Serve(c net.Conn) bool { return true } +var workerChanCap = func() int { + // Use blocking workerChan if GOMAXPROCS=1. + // This immediately switches Serve to WorkerFunc, which results + // in higher performance (under go1.5 at least). + if runtime.GOMAXPROCS(0) == 1 { + return 0 + } + + // Use non-blocking workerChan if GOMAXPROCS>1, + // since otherwise the Serve caller (Acceptor) may lag accepting + // new connections if WorkerFunc is CPU-bound. + return 1 +}() + func (wp *workerPool) getCh() *workerChan { var ch *workerChan createWorker := false @@ -123,7 +138,7 @@ func (wp *workerPool) getCh() *workerChan { vch := workerChanPool.Get() if vch == nil { vch = &workerChan{ - ch: make(chan net.Conn, 1), + ch: make(chan net.Conn, workerChanCap), } } ch = vch.(*workerChan) |