aboutsummaryrefslogtreecommitdiff
path: root/server.go
diff options
context:
space:
mode:
authorGravatar Sergey Ponomarev <stokito@gmail.com> 2022-06-05 14:05:46 +0300
committerGravatar GitHub <noreply@github.com> 2022-06-05 13:05:46 +0200
commitde188244b4559d33b136653e7588125965d31689 (patch)
treee2c69c1acdfc32ce2b794dcc548a65ec823a1de0 /server.go
parentAdd an option to allow empty root in the fsHandler (#1299) (diff)
downloadfasthttp-de188244b4559d33b136653e7588125965d31689.tar.gz
fasthttp-de188244b4559d33b136653e7588125965d31689.tar.bz2
fasthttp-de188244b4559d33b136653e7588125965d31689.zip
Optimize server connection close logic (#1310)
* server.go Make code more clear Inline isHTTP11 var. Use direct Header.SetConnectionClose() and Header.ConnectionClose() * server.go Use direct SetConnectionClose() The SetCanonical(strConnection, strClose) call internally will anyway call SetConnectionClose(). The "Connection: close" will be printed in the ResponseHeader.AppendBytes() * server.go Simplify connectionClose evaluation The conn limit check merged into connectionClose evaluation. This improves performance for most cases: 1. If the connectionClose already true then the conn limit check won't be performed. 2. The SetConnectionClose() was duplicated 3. First check conn limit and only then check for resp.connClose because most users don't close conns manually. 4. We may optimize more: If the resp.connClose = true then SetConnectionClose() not needed but as mentioned above this is a rare case.
Diffstat (limited to 'server.go')
-rw-r--r--server.go18
1 files changed, 8 insertions, 10 deletions
diff --git a/server.go b/server.go
index 396efde..e04c4ec 100644
--- a/server.go
+++ b/server.go
@@ -2127,7 +2127,6 @@ func (s *Server) serveConn(c net.Conn) (err error) {
hijackNoResponse bool
connectionClose bool
- isHTTP11 bool
continueReadingRequest bool = true
)
@@ -2323,8 +2322,8 @@ func (s *Server) serveConn(c net.Conn) (err error) {
}
}
+ // store req.ConnectionClose so even if it was changed inside of handler
connectionClose = s.DisableKeepalive || ctx.Request.Header.ConnectionClose()
- isHTTP11 = ctx.Request.Header.IsHTTP11()
if serverName != nil {
ctx.Response.Header.SetServerBytes(serverName)
@@ -2354,10 +2353,6 @@ func (s *Server) serveConn(c net.Conn) (err error) {
hijackNoResponse = ctx.hijackNoResponse && hijackHandler != nil
ctx.hijackNoResponse = false
- if s.MaxRequestsPerConn > 0 && connRequestNum >= uint64(s.MaxRequestsPerConn) {
- ctx.SetConnectionClose()
- }
-
if writeTimeout > 0 {
if err := c.SetWriteDeadline(time.Now().Add(writeTimeout)); err != nil {
panic(fmt.Sprintf("BUG: error in SetWriteDeadline(%v): %v", writeTimeout, err))
@@ -2371,11 +2366,14 @@ func (s *Server) serveConn(c net.Conn) (err error) {
previousWriteTimeout = 0
}
- connectionClose = connectionClose || ctx.Response.ConnectionClose() || (s.CloseOnShutdown && atomic.LoadInt32(&s.stop) == 1)
+ connectionClose = connectionClose ||
+ (s.MaxRequestsPerConn > 0 && connRequestNum >= uint64(s.MaxRequestsPerConn)) ||
+ ctx.Response.Header.ConnectionClose() ||
+ (s.CloseOnShutdown && atomic.LoadInt32(&s.stop) == 1)
if connectionClose {
- ctx.Response.Header.SetCanonical(strConnection, strClose)
- } else if !isHTTP11 {
- // Set 'Connection: keep-alive' response header for non-HTTP/1.1 request.
+ ctx.Response.Header.SetConnectionClose()
+ } else if !ctx.Request.Header.IsHTTP11() {
+ // Set 'Connection: keep-alive' response header for HTTP/1.0 request.
// There is no need in setting this header for http/1.1, since in http/1.1
// connections are keep-alive by default.
ctx.Response.Header.SetCanonical(strConnection, strKeepAlive)