Properly track connection start time and per-connection request num

This commit is contained in:
Aliaksandr Valialkin
2015-11-22 04:34:26 +02:00
parent 96b9f2cd84
commit 14fcfcd65b
+9 -6
View File
@@ -205,11 +205,12 @@ type RequestCtx struct {
id uint64
lastReadDuration time.Duration
connRequestNum uint64
connTime time.Time
time time.Time
lastReadDuration time.Duration
time time.Time
logger ctxLogger
s *Server
@@ -686,10 +687,10 @@ func (s *Server) getConcurrency() int {
func (s *Server) serveConn(c net.Conn) error {
currentTime := time.Now()
connTime := currentTime
connRequestNum := uint64(0)
ctx := s.acquireCtx(c)
ctx.connRequestNum = 0
ctx.connTime = currentTime
var br *bufio.Reader
var bw *bufio.Writer
@@ -698,7 +699,7 @@ func (s *Server) serveConn(c net.Conn) error {
var errMsg string
for {
ctx.id++
ctx.connRequestNum++
connRequestNum++
ctx.time = currentTime
if s.ReadTimeout > 0 {
@@ -736,6 +737,8 @@ func (s *Server) serveConn(c net.Conn) error {
break
}
ctx.connRequestNum = connRequestNum
ctx.connTime = connTime
ctx.time = currentTime
ctx.Response.Reset()
s.Handler(ctx)
@@ -744,7 +747,7 @@ func (s *Server) serveConn(c net.Conn) error {
ctx = s.acquireCtx(c)
ctx.Error(errMsg, StatusRequestTimeout)
}
if s.MaxRequestsPerConn > 0 && ctx.connRequestNum >= uint64(s.MaxRequestsPerConn) {
if s.MaxRequestsPerConn > 0 && connRequestNum >= uint64(s.MaxRequestsPerConn) {
ctx.SetConnectionClose()
}