Skip to content

Commit

Permalink
feat(worker): restart event thread to avoid memory leak (#71)
Browse files Browse the repository at this point in the history
* feat(worker): restart event thread to avoid memory leak

* use >=

* change pop limit
  • Loading branch information
chronolaw authored Sep 12, 2024
1 parent 23160b6 commit abdbcd0
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lualib/resty/events/worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit abdbcd0

Please sign in to comment.