Skip to content

Commit

Permalink
Merge pull request #63 from sourcelair/fix-inexistent-or-invalid-mode
Browse files Browse the repository at this point in the history
Fix inexistent (or invalid) mode for real tho.
  • Loading branch information
parisk committed Mar 7, 2019
2 parents 7bfd9c9 + 403596a commit 7642f56
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
26 changes: 22 additions & 4 deletions ceryx/nginx/lualib/ceryx/routes.lua
Expand Up @@ -6,13 +6,15 @@ function getRouteKeyForSource(source)
return redis.prefix .. ":routes:" .. source
end

function getSettingsKeyForSource(source)
return redis.prefix .. ":settings:" .. source
end

function targetIsInValid(target)
return not target or target == ngx.null
end

function getTargetForSource(source)
local redisClient = redis:client()

function getTargetForSource(source, redisClient)
-- Construct Redis key and then
-- try to get target for host
local key = getRouteKeyForSource(source)
Expand All @@ -35,10 +37,23 @@ function getTargetForSource(source)
return target
end

function getModeForSource(source, redisClient)
ngx.log(ngx.DEBUG, "Get routing mode for " .. source .. ".")
local settings_key = getSettingsKeyForSource(source)
local mode, _ = redisClient:hget(settings_key, "mode")

if mode == ngx.null or not mode then
mode = "proxy"
end

return mode
end

function getRouteForSource(source)
local _
local route = {}
local cache = ngx.shared.ceryx
local redisClient = redis:client()

ngx.log(ngx.DEBUG, "Looking for a route for " .. source)
-- Check if key exists in local cache
Expand All @@ -49,7 +64,7 @@ function getRouteForSource(source)
route.target = cached_value
else
ngx.log(ngx.DEBUG, "Cache miss for " .. source .. ".")
route.target = getTargetForSource(source)
route.target = getTargetForSource(source, redisClient)

if targetIsInValid(route.target) then
return nil
Expand All @@ -58,9 +73,12 @@ function getRouteForSource(source)
ngx.log(ngx.DEBUG, "Caching from " .. source .. " to " .. route.target .. " for 5 seconds.")
end

route.mode = getModeForSource(source, redisClient)

return route
end

exports.getSettingsKeyForSource = getSettingsKeyForSource
exports.getRouteForSource = getRouteForSource
exports.getTargetForSource = getTargetForSource

Expand Down
8 changes: 2 additions & 6 deletions ceryx/nginx/lualib/router.lua
Expand Up @@ -8,7 +8,6 @@ local host = ngx.var.host
local cache = ngx.shared.ceryx

local is_not_https = (ngx.var.scheme ~= "https")
local settings_key = redis.prefix .. ":settings:" .. host

function formatTarget(target)
target = utils.ensure_protocol(target)
Expand Down Expand Up @@ -40,6 +39,7 @@ function routeRequest(source, target, mode)
end

if is_not_https then
local settings_key = routes.getSettingsKeyForSource(host)
local enforce_https, flags = cache:get(host .. ":enforce_https")

if enforce_https == nil then
Expand All @@ -53,10 +53,6 @@ if is_not_https then
end
end

-- Get routing mode (default to "proxy")
local mode, mode_flags = redisClient:hget(settings_key, "mode")
mode = mode or "proxy"

ngx.log(ngx.INFO, "HOST " .. host)
local route = routes.getRouteForSource(host)

Expand All @@ -66,4 +62,4 @@ if route == nil then
end

-- Save found key to local cache for 5 seconds
routeRequest(host, route.target, mode)
routeRequest(host, route.target, route.mode)

0 comments on commit 7642f56

Please sign in to comment.