From 818705250c927d805a58a9b8751af61466be0ee5 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 23 Oct 2023 17:09:09 +0800 Subject: [PATCH 1/3] refactor(pdk): serialize msg with string.buffer --- kong/pdk/log.lua | 52 +++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index a0914e525421..8b42fb035909 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -10,6 +10,7 @@ -- @module kong.log +local buffer = require "string.buffer" local errlog = require "ngx.errlog" local ngx_re = require "ngx.re" local inspect = require "inspect" @@ -137,34 +138,34 @@ end local serializers = { - [1] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) + [1] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))) end, - [2] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) - buf[2] = to_string((select(2, ...))) + [2] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))):put(sep) + :put(to_string((select(2, ...)))) end, - [3] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) - buf[2] = to_string((select(2, ...))) - buf[3] = to_string((select(3, ...))) + [3] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))):put(sep) + :put(to_string((select(2, ...)))):put(sep) + :put(to_string((select(3, ...)))) end, - [4] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) - buf[2] = to_string((select(2, ...))) - buf[3] = to_string((select(3, ...))) - buf[4] = to_string((select(4, ...))) + [4] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))):put(sep) + :put(to_string((select(2, ...)))):put(sep) + :put(to_string((select(3, ...)))):put(sep) + :put(to_string((select(4, ...)))) end, - [5] = function(buf, to_string, ...) - buf[1] = to_string((select(1, ...))) - buf[2] = to_string((select(2, ...))) - buf[3] = to_string((select(3, ...))) - buf[4] = to_string((select(4, ...))) - buf[5] = to_string((select(5, ...))) + [5] = function(buf, sep, to_string, ...) + buf:put(to_string((select(1, ...)))):put(sep) + :put(to_string((select(2, ...)))):put(sep) + :put(to_string((select(3, ...)))):put(sep) + :put(to_string((select(4, ...)))):put(sep) + :put(to_string((select(5, ...)))) end, } @@ -282,7 +283,7 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep) to_string = to_string or tostring stack_level = stack_level or 2 - local variadic_buf = {} + local variadic_buf = buffer.new() return function(...) local sys_log_level = nil @@ -320,15 +321,16 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep) end if serializers[n] then - serializers[n](variadic_buf, to_string, ...) + serializers[n](variadic_buf, sep, to_string, ...) else - for i = 1, n do - variadic_buf[i] = to_string((select(i, ...))) + for i = 1, n - 1 do + variadic_buf:put(to_string((select(i, ...)))):put(sep) end + variadic_buf:put(to_string((select(n, ...)))) end - local msg = concat(variadic_buf, sep, 1, n) + local msg = variadic_buf:get() for i = 1, imm_buf.n_messages do imm_buf[imm_buf.message_idxs[i]] = msg From a1f4a5765fe861b8962aabb8c3be79e178071890 Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 23 Oct 2023 17:23:51 +0800 Subject: [PATCH 2/3] sep or "" --- kong/pdk/log.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index 8b42fb035909..d6b6cff44fc9 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -321,11 +321,11 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep) end if serializers[n] then - serializers[n](variadic_buf, sep, to_string, ...) + serializers[n](variadic_buf, sep or "" , to_string, ...) else for i = 1, n - 1 do - variadic_buf:put(to_string((select(i, ...)))):put(sep) + variadic_buf:put(to_string((select(i, ...)))):put(sep or "" ) end variadic_buf:put(to_string((select(n, ...)))) end From 6a69d259a77957ba4289e1c060ee5dd800bb0b9f Mon Sep 17 00:00:00 2001 From: chronolaw Date: Mon, 23 Oct 2023 17:30:31 +0800 Subject: [PATCH 3/3] style clean --- kong/pdk/log.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/pdk/log.lua b/kong/pdk/log.lua index d6b6cff44fc9..e1cf4892cd8d 100644 --- a/kong/pdk/log.lua +++ b/kong/pdk/log.lua @@ -325,7 +325,7 @@ local function gen_log_func(lvl_const, imm_buf, to_string, stack_level, sep) else for i = 1, n - 1 do - variadic_buf:put(to_string((select(i, ...)))):put(sep or "" ) + variadic_buf:put(to_string((select(i, ...)))):put(sep or "") end variadic_buf:put(to_string((select(n, ...)))) end