mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-14 15:56:44 +03:00
Added Server.ConnID() for associating distinct requests that come from the same client connection
This commit is contained in:
@@ -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")
|
||||
|
||||
+2
-2
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user