diff --git a/kong/db/declarative/init.lua b/kong/db/declarative/init.lua index b2eaef749fe..03169dd795e 100644 --- a/kong/db/declarative/init.lua +++ b/kong/db/declarative/init.lua @@ -10,6 +10,7 @@ local constants = require "kong.constants" local txn = require "resty.lmdb.transaction" local lmdb = require "resty.lmdb" + local setmetatable = setmetatable local loadstring = loadstring local tostring = tostring @@ -957,6 +958,7 @@ do return nil, "exiting" end + local reconfigure_data local worker_events = kong.worker_events local ok, err, default_ws = declarative.load_into_cache(entities, meta, hash) @@ -980,12 +982,14 @@ do end end - ok, err = worker_events.post("declarative", "reconfigure", { + reconfigure_data = { default_ws, router_hash, plugins_hash, balancer_hash - }) + } + + ok, err = worker_events.post("declarative", "reconfigure", reconfigure_data) if ok ~= "done" then return nil, "failed to broadcast reconfigure event: " .. (err or ok) end @@ -1000,6 +1004,11 @@ do if SUBSYS == "http" and #kong.configuration.stream_listeners > 0 then -- update stream if necessary + local json, err = cjson.encode(reconfigure_data) + if not json then + return nil, err + end + local sock = ngx_socket_tcp() ok, err = sock:connect("unix:" .. PREFIX .. "/stream_config.sock") if not ok then @@ -1007,15 +1016,15 @@ do end local bytes - bytes, err = sock:send(default_ws) + bytes, err = sock:send(json) sock:close() if not bytes then return nil, err end - assert(bytes == #default_ws, - "incomplete default workspace id sent to the stream subsystem") + assert(bytes == #json, + "incomplete reconfigure data sent to the stream subsystem") end diff --git a/kong/init.lua b/kong/init.lua index d8be8e325d6..73ad90a72eb 100644 --- a/kong/init.lua +++ b/kong/init.lua @@ -1559,6 +1559,8 @@ end do + local cjson = require "cjson.safe" + function Kong.stream_config_listener() local sock, err = ngx.req.socket() if not sock then @@ -1568,16 +1570,19 @@ do local data, err = sock:receive("*a") if not data then - ngx_log(ngx_CRIT, "unable to receive new config: ", err) + ngx_log(ngx_CRIT, "unable to receive reconfigure data: ", err) return end - kong.core_cache:purge() - kong.cache:purge() + local reconfigure_data, err = cjson.decode(data) + if not reconfigure_data then + ngx_log(ngx_ERR, "failed to json decode reconfigure data: ", err) + return + end - local ok, err = kong.worker_events.post("declarative", "reconfigure", data) + local ok, err = kong.worker_events.post("declarative", "reconfigure", reconfigure_data) if ok ~= "done" then - ngx_log(ngx_ERR, "failed to reboadcast reconfigure event in stream: ", err or ok) + ngx_log(ngx_ERR, "failed to rebroadcast reconfigure event in stream: ", err or ok) end end end