From d9ec78389489cd4c059e0e92155b9ded447e9569 Mon Sep 17 00:00:00 2001 From: lijunlong Date: Tue, 28 May 2024 09:38:38 +0800 Subject: [PATCH] optimize: avoid pcall when validating the Connection response header Previously the Connection header was lowercased via pcall(str_lower, ...) to tolerate a nil header, but pcall carries non-trivial overhead on the hot response path. Check the header type directly instead: only lowercase and parse it when it is a string. A nil header (no Connection) or a table (duplicated header) falls back to the default keepalive handling, matching the previous pcall behaviour. --- lib/resty/http.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/resty/http.lua b/lib/resty/http.lua index 6deb629..c31bb9a 100644 --- a/lib/resty/http.lua +++ b/lib/resty/http.lua @@ -798,15 +798,16 @@ function _M.read_response(self, params) end -- keepalive is true by default. Determine if this is correct or not. - local ok, connection = pcall(str_lower, res_headers["Connection"]) - if ok then - if (version == 1.1 and str_find(connection, "close", 1, true)) or - (version == 1.0 and not str_find(connection, "keep-alive", 1, true)) then + local connection = res_headers["Connection"] + if type(connection) ~= "string" then + -- no connection header (or duplicated as a table) + if version == 1.0 then self.keepalive = false end else - -- no connection header - if version == 1.0 then + connection = str_lower(connection) + if (version == 1.1 and str_find(connection, "close", 1, true)) or + (version == 1.0 and not str_find(connection, "keep-alive", 1, true)) then self.keepalive = false end end