Skip to content

Commit ab7eee3

Browse files
zhuizhuhaomengsriemer
authored andcommitted
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.
1 parent 7837f60 commit ab7eee3

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

lib/resty/http.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -798,15 +798,16 @@ function _M.read_response(self, params)
798798
end
799799

800800
-- keepalive is true by default. Determine if this is correct or not.
801-
local ok, connection = pcall(str_lower, res_headers["Connection"])
802-
if ok then
803-
if (version == 1.1 and str_find(connection, "close", 1, true)) or
804-
(version == 1.0 and not str_find(connection, "keep-alive", 1, true)) then
801+
local connection = res_headers["Connection"]
802+
if type(connection) ~= "string" then
803+
-- no connection header (or duplicated as a table)
804+
if version == 1.0 then
805805
self.keepalive = false
806806
end
807807
else
808-
-- no connection header
809-
if version == 1.0 then
808+
connection = str_lower(connection)
809+
if (version == 1.1 and str_find(connection, "close", 1, true)) or
810+
(version == 1.0 and not str_find(connection, "keep-alive", 1, true)) then
810811
self.keepalive = false
811812
end
812813
end

0 commit comments

Comments
 (0)