Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding function to remove stale objects from kafka logger #1526

Merged
merged 4 commits into from
May 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions apisix/plugins/kafka-logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ local batch_processor = require("apisix.utils.batch-processor")
local pairs = pairs
local type = type
local table = table
local ipairs = ipairs
local plugin_name = "kafka-logger"
local stale_timer_running = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: ; is useless, we should remove it

we can fix it in a new PR

local timer_at = ngx.timer.at
local tostring = tostring
local ngx = ngx
local buffers = {}

Expand Down Expand Up @@ -90,6 +94,22 @@ local function send_kafka_data(conf, log_message)
end


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


function _M.log(conf)
local entry = log_util.get_full_log(ngx)

Expand All @@ -100,6 +120,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
Expand Down