目前,每当上游服务宕机时,kong 将抛出“{“message”:“failure to get a peer from the ring-balancer”}”
我正在尝试创建一个自定义插件来检测连接超时并向客户端返回自定义消息,我现在面临的阻止程序是在我的自定义插件中编写 lua 代码来检测超时。我试过使用
if(kong.response.get_source() == "error")
但这似乎也没有检测到超时。
有谁知道在编写自定义 kong 插件时我应该怎么做来检测连接超时?
目前,每当上游服务宕机时,kong 将抛出“{“message”:“failure to get a peer from the ring-balancer”}”
我正在尝试创建一个自定义插件来检测连接超时并向客户端返回自定义消息,我现在面临的阻止程序是在我的自定义插件中编写 lua 代码来检测超时。我试过使用
if(kong.response.get_source() == "error")
但这似乎也没有检测到超时。
有谁知道在编写自定义 kong 插件时我应该怎么做来检测连接超时?
对不起,我没抓到你。因为我不知道你的函数会返回什么
如果kong.response.get_source()返回一个字符串{"message":"failure to get a peer from the ring-balancer"}
您需要使用 JSON 库来解析它。
local json = require("json")
local res = json.deceode(kong.response.get_source())
if res.message == "xxx" then
end
但是你message的有点长。您可以向 JSON 字符串添加新的键值来表示状态。
例如:"{"status":"error","message":"failure to get a peer from the ring-balancer"}"
然后你可以编写这样的代码。
local json = require("json")
local res = json.deceode(kong.response.get_source())
if res.status == "error" then
end
我只是看了Kong API Docs。
我发现的返回值kong.response.get_source()是HTTP状态码,比如200/500。
你可以试试看print(kong.response.get_source())结果。