This is an automated email from the ASF dual-hosted git repository.

ashishtiwari pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new d2fb33829 feat: support multiple json.delay_encode objects in single 
log (#12395)
d2fb33829 is described below

commit d2fb338296b1b249b80bb75b96c8f043deff0c4f
Author: Ashish Tiwari <ashishjaitiwari15112...@gmail.com>
AuthorDate: Mon Jul 7 12:29:44 2025 +0530

    feat: support multiple json.delay_encode objects in single log (#12395)
---
 apisix/core/json.lua      | 33 +++++++++++++++++++++------------
 apisix/utils/upstream.lua |  4 ++--
 t/core/json.t             | 14 ++------------
 3 files changed, 25 insertions(+), 26 deletions(-)

diff --git a/apisix/core/json.lua b/apisix/core/json.lua
index 05541b71d..4341c4682 100644
--- a/apisix/core/json.lua
+++ b/apisix/core/json.lua
@@ -87,18 +87,23 @@ local function encode(data, force)
 end
 _M.encode = encode
 
+local max_delay_encode_items = 16
+local delay_tab_idx = 0
+local delay_tab_arr = {}
+for i = 1, max_delay_encode_items do
+    delay_tab_arr[i] = setmetatable({data = "", force = false}, {
+        __tostring = function(self)
+            local res, err = encode(self.data, self.force)
+            if not res then
+                ngx.log(ngx.WARN, "failed to encode: ", err,
+                        " force: ", self.force)
+            end
 
-local delay_tab = setmetatable({data = "", force = false}, {
-    __tostring = function(self)
-        local res, err = encode(self.data, self.force)
-        if not res then
-            ngx.log(ngx.WARN, "failed to encode: ", err,
-                    " force: ", self.force)
+            return res
         end
+    })
+end
 
-        return res
-    end
-})
 
 
 ---
@@ -114,9 +119,13 @@ local delay_tab = setmetatable({data = "", force = false}, 
{
 -- @usage
 -- core.log.info("conf  : ", core.json.delay_encode(conf))
 function _M.delay_encode(data, force)
-    delay_tab.data = data
-    delay_tab.force = force
-    return delay_tab
+    delay_tab_idx = delay_tab_idx+1
+    if delay_tab_idx > max_delay_encode_items then
+        delay_tab_idx = 1
+    end
+    delay_tab_arr[delay_tab_idx].data = data
+    delay_tab_arr[delay_tab_idx].force = force
+    return delay_tab_arr[delay_tab_idx]
 end
 
 
diff --git a/apisix/utils/upstream.lua b/apisix/utils/upstream.lua
index 52c14d046..3c0b9a381 100644
--- a/apisix/utils/upstream.lua
+++ b/apisix/utils/upstream.lua
@@ -47,8 +47,8 @@ local function compare_upstream_node(up_conf, new_t)
 
     -- slow path
     core.log.debug("compare upstream nodes by value, ",
-                    "old: ", tostring(old_t) , " ", 
core.json.delay_encode(old_t, true))
-    core.log.debug("new: ", tostring(new_t) , " ", 
core.json.delay_encode(new_t, true))
+                    "old: ", tostring(old_t) , " ", 
core.json.delay_encode(old_t, true),
+                    "new: ", tostring(new_t) , " ", 
core.json.delay_encode(new_t, true))
 
     if up_conf.original_nodes then
         -- if original_nodes is set, it means that the upstream nodes
diff --git a/t/core/json.t b/t/core/json.t
index a7caf8dae..e946bc64a 100644
--- a/t/core/json.t
+++ b/t/core/json.t
@@ -56,21 +56,11 @@ data: test
     location /t {
         content_by_lua_block {
             local core = require("apisix.core")
-            local data1 = core.json.delay_encode({test="test1"})
-            local data2 = core.json.delay_encode({test="test2"})
-
-            ngx.say("delay encode: ", data1 == data2)
-            ngx.say("data1 type: ", type(data1))
-            ngx.log(ngx.ERR, "data1 val: ", data1)
-            ngx.log(ngx.ERR, "data2 val: ", data2)
+            ngx.log(ngx.ERR, "val: ", 
core.json.delay_encode({test="test1"}),core.json.delay_encode({test="test2"}))
         }
     }
---- response_body
-delay encode: true
-data1 type: table
 --- error_log
-data1 val: {"test":"test2"}
-data2 val: {"test":"test2"}
+val: {"test":"test1"}{"test":"test2"}
 
 
 

Reply via email to