Skip to content

Commit

Permalink
perf(router) build router from only the routes that have a matching s…
Browse files Browse the repository at this point in the history
…ubsystem
  • Loading branch information
bungle committed Mar 11, 2019
1 parent 07438e8 commit 6cfa094
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local concurrency = require "kong.concurrency"


local kong = kong
local ipairs = ipairs
local tostring = tostring
local tonumber = tonumber
local sub = string.sub
Expand All @@ -40,9 +41,11 @@ local DEBUG = ngx.DEBUG


local CACHE_ROUTER_OPTS = { ttl = 0 }
local SUBSYSTEMS = constants.PROTOCOLS_WITH_SUBSYSTEM
local EMPTY_T = {}



local get_router, build_router
local server_header = meta._SERVER_TOKENS
local _set_check_router_rebuild
Expand All @@ -58,16 +61,42 @@ end

do
-- Given a protocol, return the subsystem that handles it
local protocol_subsystem = {
http = "http",
https = "http",
tcp = "stream",
tls = "stream",
}

local router
local router_version

local function should_process_route(route)
for _, protocol in ipairs(route.protocols) do
if SUBSYSTEMS[protocol] == subsystem then
return true
end
end

return false
end

local function get_service_for_route(db, route)
local service_pk = route.service
if not service_pk then
return nil
end

local service, err = db.services:select(service_pk)
if not service then
return nil, "could not find service for route (" .. route.id .. "): " ..
err
end

-- TODO: this should not be needed as the schema should check it already
if SUBSYSTEMS[service.protocol] ~= subsystem then
log(WARN, "service with protocol '", service.protocol,
"' cannot be used with '", subsystem, "' subsystem")

return nil
end

return service
end

build_router = function(db, version)
local routes, i = {}, 0

Expand All @@ -76,26 +105,25 @@ do
return nil, "could not load routes: " .. err
end

local service_pk = route.service

if not service_pk then
return nil, "route (" .. route.id .. ") is not associated with service"
end

local service, err = db.services:select(service_pk)
if not service then
return nil, "could not find service for route (" .. route.id .. "): " ..
err
end
if should_process_route(route) then
local service, err = get_service_for_route(db, route)
if err then
return nil, err
end

local stype = protocol_subsystem[service.protocol]
if subsystem == stype then
local r = {
route = route,
service = service,
}

if stype == "http" and route.hosts then
local service_subsystem
if service then
service_subsystem = SUBSYSTEMS[service.protocol]
else
service_subsystem = subsystem
end

if service_subsystem == "http" and route.hosts then
-- TODO: headers should probably be moved to route
r.headers = {
host = route.hosts,
Expand Down

0 comments on commit 6cfa094

Please sign in to comment.