diff --git a/kong/runloop/plugin_servers/init.lua b/kong/runloop/plugin_servers/init.lua index 89418eb31547..43381bf168f8 100644 --- a/kong/runloop/plugin_servers/init.lua +++ b/kong/runloop/plugin_servers/init.lua @@ -200,7 +200,7 @@ function get_instance_id(plugin_name, conf) -- some other thread is already starting an instance -- prevent busy-waiting ngx_sleep(SLEEP_STEP) - + -- to prevent a potential dead loop when someone failed to release the ID wait_count = wait_count + 1 if wait_count > MAX_WAIT_STEPS then @@ -386,6 +386,7 @@ end function plugin_servers.start() + -- only worker 0 managers the plugin server if worker_id() == 0 then local pluginserver_timer = proc_mgmt.pluginserver_timer @@ -396,6 +397,8 @@ function plugin_servers.start() end end + -- workers != 0 still need to get plugin servers definitions + -- local connection_check_timer = proc_mgmt.connection_check_timer for _, server_def in ipairs(proc_mgmt.get_server_defs()) do @@ -403,6 +406,11 @@ function plugin_servers.start() native_timer_at(0, connection_check_timer, server_def) end end + + -- in case plugin server restarts, all workers need to update their defs + kong.worker_events.register(function (data) + reset_instance(data.plugin_name, data.conf) + end, "plugin_server", "reset_instances") end function plugin_servers.stop() diff --git a/kong/runloop/plugin_servers/pb_rpc.lua b/kong/runloop/plugin_servers/pb_rpc.lua index 676f6e9f5fa1..10872c33effb 100644 --- a/kong/runloop/plugin_servers/pb_rpc.lua +++ b/kong/runloop/plugin_servers/pb_rpc.lua @@ -400,15 +400,23 @@ function Rpc:handle_event(plugin_name, conf, phase) end if not res or res == "" then - if err == "not ready" then - self.reset_instance(plugin_name, conf) - return handle_not_ready(plugin_name) - end - if err and (str_find(err:lower(), "no plugin instance", 1, true) - or str_find(err:lower(), "closed", 1, true)) then - kong.log.warn(err) - self.reset_instance(plugin_name, conf) - return self:handle_event(plugin_name, conf, phase) + if err then + local ok, err = kong.worker_events.post("plugin_server", "reset_instances", + { plugin_name = plugin_name, conf = conf }) + if not ok then + kong.log.err("failed to post plugin_server reset_instances event: ", err) + end + + if err == "not ready" then + self.reset_instance(plugin_name, conf) + return handle_not_ready(plugin_name) + end + if err and (str_find(err:lower(), "no plugin instance", 1, true) + or str_find(err:lower(), "closed", 1, true)) then + kong.log.warn(err) + self.reset_instance(plugin_name, conf) + return self:handle_event(plugin_name, conf, phase) + end end kong.log.err(err) end