Skip to content

Commit fd38226

Browse files
sshniroSaberMaster
authored andcommitted
bugfix: Adding function to remove stale objects from kafka logger (apache#1526)
1 parent 70feb0d commit fd38226

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

apisix/plugins/kafka-logger.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ local batch_processor = require("apisix.utils.batch-processor")
2121
local pairs = pairs
2222
local type = type
2323
local table = table
24+
local ipairs = ipairs
2425
local plugin_name = "kafka-logger"
26+
local stale_timer_running = false;
27+
local timer_at = ngx.timer.at
28+
local tostring = tostring
2529
local ngx = ngx
2630
local buffers = {}
2731

@@ -90,6 +94,22 @@ local function send_kafka_data(conf, log_message)
9094
end
9195

9296

97+
local function remove_stale_objects(premature)
98+
if premature then
99+
return
100+
end
101+
102+
for key, batch in ipairs(buffers) do
103+
if #batch.entry_buffer.entries == 0 and #batch.batch_to_process == 0 then
104+
core.log.debug("removing batch processor stale object, route id:", tostring(key))
105+
buffers[key] = nil
106+
end
107+
end
108+
109+
stale_timer_running = false
110+
end
111+
112+
93113
function _M.log(conf)
94114
local entry = log_util.get_full_log(ngx)
95115

@@ -100,6 +120,12 @@ function _M.log(conf)
100120

101121
local log_buffer = buffers[entry.route_id]
102122

123+
if not stale_timer_running then
124+
-- run the timer every 30 mins if any log is present
125+
timer_at(1800, remove_stale_objects)
126+
stale_timer_running = true
127+
end
128+
103129
if log_buffer then
104130
log_buffer:push(entry)
105131
return

0 commit comments

Comments
 (0)