From 2d5c6fd01aa0eb0a4250f6d9a83d075366d625cb Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 24 Apr 2016 22:01:13 +0300 Subject: [PATCH] Added Server.ConnID() for associating distinct requests that come from the same client connection --- server.go | 38 +++++++++++++++++++++----------------- server_test.go | 4 ++-- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/server.go b/server.go index 75f49a3..6b48a74 100644 --- a/server.go +++ b/server.go @@ -367,10 +367,9 @@ type RequestCtx struct { userValues userData - id uint64 - lastReadDuration time.Duration + connID uint64 connRequestNum uint64 connTime time.Time @@ -535,7 +534,15 @@ var zeroTCPAddr = &net.TCPAddr{ // ID returns unique ID of the request. func (ctx *RequestCtx) ID() uint64 { - return ctx.id + return (ctx.connID << 32) | ctx.connRequestNum +} + +// ConnID returns unique connection ID. +// +// This ID may be used to match distinct requests to the same incoming +// connection. +func (ctx *RequestCtx) ConnID() uint64 { + return ctx.connID } // Time returns RequestHandler call time. @@ -1336,10 +1343,17 @@ func (s *Server) getConcurrency() int { return n } +var globalConnID uint64 + +func nextConnID() uint64 { + return atomic.AddUint64(&globalConnID, 1) +} + func (s *Server) serveConn(c net.Conn) error { + connRequestNum := uint64(0) + connID := nextConnID() currentTime := time.Now() connTime := currentTime - connRequestNum := uint64(0) ctx := s.acquireCtx(c) ctx.connTime = connTime @@ -1358,7 +1372,6 @@ func (s *Server) serveConn(c net.Conn) error { isHTTP11 bool ) for { - ctx.id++ connRequestNum++ ctx.time = currentTime @@ -1432,6 +1445,7 @@ func (s *Server) serveConn(c net.Conn) error { connectionClose = s.DisableKeepalive || ctx.Request.Header.connectionCloseFast() isHTTP11 = ctx.Request.Header.IsHTTP11() + ctx.connID = connID ctx.connRequestNum = connRequestNum ctx.connTime = connTime ctx.time = currentTime @@ -1751,14 +1765,10 @@ func (s *Server) acquireCtx(c net.Conn) *RequestCtx { v := s.ctxPool.Get() var ctx *RequestCtx if v == nil { - ctx = &RequestCtx{ + v = &RequestCtx{ s: s, - c: c, } - ctx.initID() - return ctx } - ctx = v.(*RequestCtx) ctx.c = c return ctx @@ -1779,9 +1789,9 @@ func (ctx *RequestCtx) Init(req *Request, remoteAddr net.Addr, logger Logger) { if logger == nil { logger = defaultLogger } + ctx.connID = nextConnID() ctx.logger.logger = logger ctx.s = &fakeServer - ctx.initID() req.CopyTo(&ctx.Request) ctx.Response.Reset() ctx.connRequestNum = 0 @@ -1816,12 +1826,6 @@ func (fa *fakeAddrer) Close() error { panic("BUG: unexpected Close call") } -var globalCtxID uint64 - -func (ctx *RequestCtx) initID() { - ctx.id = (atomic.AddUint64(&globalCtxID, 1)) << 32 -} - func (s *Server) releaseCtx(ctx *RequestCtx) { if ctx.timeoutResponse != nil { panic("BUG: cannot release timed out RequestCtx") diff --git a/server_test.go b/server_test.go index f465d6c..42e32c8 100644 --- a/server_test.go +++ b/server_test.go @@ -1430,7 +1430,7 @@ func TestRequestCtxHijack(t *testing.T) { func TestRequestCtxInit(t *testing.T) { var ctx RequestCtx var logger customLogger - globalCtxID = 0x123456 + globalConnID = 0x123456 ctx.Init(&ctx.Request, zeroTCPAddr, &logger) ip := ctx.RemoteIP() if !ip.IsUnspecified() { @@ -1979,7 +1979,7 @@ func TestServerLogger(t *testing.T) { }, } - globalCtxID = 0 + globalConnID = 0 ch := make(chan error) go func() { ch <- s.ServeConn(rwx)