From 3300f4d19bb9d3ca08ea6e3e17ede71cea7eeb80 Mon Sep 17 00:00:00 2001 From: Ayeshmantha Perera Date: Mon, 11 May 2020 10:28:24 +0200 Subject: [PATCH] bugfix: removed stale object in sys log. (#1557) --- apisix/plugins/kafka-logger.lua | 2 +- apisix/plugins/syslog.lua | 26 ++++++++++++++++++++++++++ apisix/plugins/tcp-logger.lua | 2 +- apisix/plugins/udp-logger.lua | 2 +- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/kafka-logger.lua b/apisix/plugins/kafka-logger.lua index 1641c5e1..e88cf7ca 100644 --- a/apisix/plugins/kafka-logger.lua +++ b/apisix/plugins/kafka-logger.lua @@ -93,7 +93,7 @@ local function send_kafka_data(conf, log_message) end end - +-- remove stale objects from the memory after timer expires local function remove_stale_objects(premature) if premature then return diff --git a/apisix/plugins/syslog.lua b/apisix/plugins/syslog.lua index 270b719b..f633f3ce 100644 --- a/apisix/plugins/syslog.lua +++ b/apisix/plugins/syslog.lua @@ -21,6 +21,10 @@ local logger_socket = require("resty.logger.socket") local plugin_name = "syslog" local ngx = ngx local buffers = {} +local ipairs = ipairs +local stale_timer_running = false; +local timer_at = ngx.timer.at +local tostring = tostring local schema = { type = "object", @@ -105,6 +109,22 @@ local function send_syslog_data(conf, log_message) return res, err_msg end +-- remove stale objects from the memory after timer expires +local function remove_stale_objects(premature) + if premature then + return + end + + for key, batch in ipairs(buffers) do + if #batch.entry_buffer.entries == 0 and #batch.batch_to_process == 0 then + core.log.debug("removing batch processor stale object, route id:", tostring(key)) + buffers[key] = nil + end + end + + stale_timer_running = false +end + -- log phase in APISIX function _M.log(conf) local entry = log_util.get_full_log(ngx) @@ -116,6 +136,12 @@ function _M.log(conf) local log_buffer = buffers[entry.route_id] + if not stale_timer_running then + -- run the timer every 30 mins if any log is present + timer_at(1800, remove_stale_objects) + stale_timer_running = true + end + if log_buffer then log_buffer:push(entry) return diff --git a/apisix/plugins/tcp-logger.lua b/apisix/plugins/tcp-logger.lua index 424b6933..197f424d 100644 --- a/apisix/plugins/tcp-logger.lua +++ b/apisix/plugins/tcp-logger.lua @@ -97,7 +97,7 @@ local function send_tcp_data(conf, log_message) return res, err_msg end - +-- remove stale objects from the memory after timer expires local function remove_stale_objects(premature) if premature then return diff --git a/apisix/plugins/udp-logger.lua b/apisix/plugins/udp-logger.lua index 957e732f..119d3159 100644 --- a/apisix/plugins/udp-logger.lua +++ b/apisix/plugins/udp-logger.lua @@ -80,7 +80,7 @@ local function send_udp_data(conf, log_message) return res, err_msg end - +-- remove stale objects from the memory after timer expires local function remove_stale_objects(premature) if premature then return