Skip to content

Commit

Permalink
perf(*): perhaps a slightly faster VIA headers
Browse files Browse the repository at this point in the history
Signed-off-by: Aapo Talvensaari <aapo.talvensaari@gmail.com>
  • Loading branch information
bungle committed Sep 6, 2024
1 parent b861d1f commit 3f5526f
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ local ROUTER_CACHE = lrucache.new(ROUTER_CACHE_SIZE)
local ROUTER_CACHE_NEG = lrucache.new(ROUTER_CACHE_SIZE)


local DEFAULT_PROXY_HTTP_VERSION = "1.1"
local DEFAULT_PROXY_HTTP_VERSION = 1.1


local NOOP = function() end
Expand Down Expand Up @@ -109,6 +109,8 @@ local RECONFIGURE_OPTS
local GLOBAL_QUERY_OPTS = { workspace = ngx.null, show_ws_id = true }

local SERVER_HEADER = meta._SERVER_TOKENS
local DEFAULT_PROXY_GRPC_VIA_HEADER = "2 " .. SERVER_HEADER
local DEFAULT_PROXY_HTTP_VIA_HEADER = "1.1 " .. SERVER_HEADER


local STREAM_TLS_TERMINATE_SOCK
Expand Down Expand Up @@ -912,6 +914,9 @@ return {
reports.init_worker(kong.configuration)
end

DEFAULT_PROXY_HTTP_VIA_HEADER = (tonumber(kong.configuration.proxy_http_version, 10) or "1.1")
.. " " .. SERVER_HEADER

update_lua_mem(true)

if kong.configuration.role == "control_plane" then
Expand Down Expand Up @@ -1290,13 +1295,9 @@ return {
var.upstream_x_forwarded_path = forwarded_path
var.upstream_x_forwarded_prefix = forwarded_prefix

do
local req_via = get_header(constants.HEADERS.VIA, ctx)
local kong_inbound_via = protocol_version and protocol_version .. " " .. SERVER_HEADER
or SERVER_HEADER
var.upstream_via = req_via and req_via .. ", " .. kong_inbound_via
or kong_inbound_via
end
local req_via = get_header(constants.HEADERS.VIA, ctx)
local kong_via = protocol_version and (protocol_version .. " " .. SERVER_HEADER) or SERVER_HEADER
var.upstream_via = req_via and (req_via .. ", " .. kong_via) or kong_via

-- At this point, the router and `balancer_setup_stage1` have been
-- executed; detect requests that need to be redirected from `proxy_pass`
Expand Down Expand Up @@ -1487,25 +1488,11 @@ return {
end

if enabled_headers[headers.VIA] then
-- Kong does not support injected directives like 'nginx_location_proxy_http_version',
-- so we skip checking them.

local proxy_http_version

local upstream_scheme = var.upstream_scheme
if upstream_scheme == "grpc" or upstream_scheme == "grpcs" then
proxy_http_version = "2"
end
if not proxy_http_version then
proxy_http_version = ctx.proxy_http_version or
kong.configuration.proxy_http_version or
DEFAULT_PROXY_HTTP_VERSION
end

local kong_outbound_via = proxy_http_version .. " " .. SERVER_HEADER
local resp_via = var["upstream_http_" .. headers.VIA]
header[headers.VIA] = resp_via and resp_via .. ", " .. kong_outbound_via
or kong_outbound_via
local resp_via = var.upstream_http_via
local kong_via = (upstream_scheme == "grpc" or upstream_scheme == "grpcs") and
DEFAULT_PROXY_GRPC_VIA_HEADER or DEFAULT_PROXY_HTTP_VIA_HEADER
header[headers.VIA] = resp_via and (resp_via .. ", " .. kong_via) or kong_via
end

-- If upstream does not provide the 'Server' header, an 'openresty' header
Expand Down

0 comments on commit 3f5526f

Please sign in to comment.