From abdbcd0aa388a2c386071dbbe7d30aceae9fcd2c Mon Sep 17 00:00:00 2001 From: Chrono Date: Thu, 12 Sep 2024 17:30:18 +0800 Subject: [PATCH] feat(worker): restart event thread to avoid memory leak (#71) * feat(worker): restart event thread to avoid memory leak * use >= * change pop limit --- lualib/resty/events/worker.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lualib/resty/events/worker.lua b/lualib/resty/events/worker.lua index 402d113..9dd1ee7 100644 --- a/lualib/resty/events/worker.lua +++ b/lualib/resty/events/worker.lua @@ -41,6 +41,7 @@ local cjson_encode = cjson.encode local EVENTS_COUNT_LIMIT = 100 +local EVENTS_POP_LIMIT = 2000 local EVENTS_SLEEP_TIME = 0.05 @@ -226,6 +227,8 @@ local function write_thread(self, broker_connection) end local function events_thread(self) + local counter = 0 + while not exiting() do local data, err = self._sub_queue:pop() if err then @@ -240,6 +243,13 @@ local function events_thread(self) -- got an event data, callback self._callback:do_event(data) + counter = counter + 1 + + -- exit and restart timer to avoid memory leak + if counter >= EVENTS_POP_LIMIT then + break + end + -- yield, not block other threads sleep(0)