【问题标题】:How does Kong detect connection timeout?Kong如何检测连接超时?
【发布时间】:2021-07-31 16:15:58
【问题描述】:

目前,只要上游服务关闭,kong 就会抛出 "{"message":"从 ring-balancer 获取节点失败"}"

我正在尝试创建一个自定义插件来检测连接超时并向客户端返回自定义消息,我现在面临的阻止程序是在我的自定义插件中编写 lua 代码来检测超时。我试过使用

if(kong.response.get_source() == "error")

但这似乎也没有检测到超时。

有没有人知道在编写自定义 kong 插件时我应该如何检测连接超时?

【问题讨论】:

    标签: lua api-gateway kong kong-plugin kong-ingress


    【解决方案1】:

    对不起,我没有抓住你。因为我不知道你的函数会返回什么

    如果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())看看结果。

    【讨论】:

    • 嗨@MushroomMogu,我得到的“{“message”:“failure to get a peer from the ring-balancer”}”响应不是来自我编写的任何函数,Idk 来自哪里或者,我想要做的是拦截该消息从显示给客户端,这就是我创建自定义插件的原因。在插件中,我使用了 if(kong.response.get_source() == "error"),根据docs.konghq.com/gateway-oss/2.4.x/pdk/kong.response/…,它应该在处理请求时捕获错误,例如:连接上游服务时超时。但这似乎不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 2012-01-10
    • 1970-01-01
    相关资源
    最近更新 更多