From d8c882d3604e24430943ecc5416f1d409ccc3154 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sat, 28 Nov 2015 17:23:53 +0200 Subject: [PATCH] scalability improvement: initialize RequestCtx.id only when creating new RequestCtx obect. This should reduce globalCtxID contention --- server.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/server.go b/server.go index 3acb532..aff4be1 100644 --- a/server.go +++ b/server.go @@ -1144,21 +1144,20 @@ func releaseWriter(s *Server, w *bufio.Writer) { s.writerPool.Put(w) } -var globalCtxID uint64 - func (s *Server) acquireCtx(c net.Conn) *RequestCtx { v := s.ctxPool.Get() var ctx *RequestCtx if v == nil { ctx = &RequestCtx{ s: s, + c: c, } + ctx.initID() ctx.v = ctx - v = ctx - } else { - ctx = v.(*RequestCtx) + return ctx } - ctx.initID() + + ctx = v.(*RequestCtx) ctx.c = c return ctx } @@ -1214,6 +1213,8 @@ func (fa *fakeAddrer) Close() error { panic("BUG: unexpected Close call") } +var globalCtxID uint64 + func (ctx *RequestCtx) initID() { ctx.id = (atomic.AddUint64(&globalCtxID, 1)) << 32 }