diff options
author | sumneko <sumneko@hotmail.com> | 2019-04-12 15:30:28 +0800 |
---|---|---|
committer | sumneko <sumneko@hotmail.com> | 2019-04-12 15:30:28 +0800 |
commit | 9297dd189945e8019c4598ad3eb082059ab4a3e7 (patch) | |
tree | 80ad977e95d5af0068db23d2b8016ba6cb444c7e /server/src/async | |
parent | 2d0a488eb5a871d6f70b3c6288c9c650b6689655 (diff) | |
download | lua-language-server-9297dd189945e8019c4598ad3eb082059ab4a3e7.zip |
统计并回收子线程的内存
Diffstat (limited to 'server/src/async')
-rw-r--r-- | server/src/async/async.lua | 25 | ||||
-rw-r--r-- | server/src/async/proto.lua | 2 |
2 files changed, 26 insertions, 1 deletions
diff --git a/server/src/async/async.lua b/server/src/async/async.lua index b385aac8..3401c346 100644 --- a/server/src/async/async.lua +++ b/server/src/async/async.lua @@ -1,23 +1,30 @@ local thread = require 'bee.thread' local errlog = thread.channel 'errlog' + local TaskId = 0 local IdlePool = {} local RunningList = {} +local GCInfo = {} + +thread.newchannel 'gc' local function createTask() TaskId = TaskId + 1 + GCInfo[TaskId] = false local id = TaskId local requestName = 'request' .. tostring(id) local responseName = 'response' .. tostring(id) thread.newchannel(requestName) thread.newchannel(responseName) local buf = ([[ +ID = %d package.cpath = %q package.path = %q local thread = require 'bee.thread' local request = thread.channel(%q) local response = thread.channel(%q) local errlog = thread.channel 'errlog' +local gc = thread.channel 'gc' local function task() local dump, arg = request:bpop() @@ -25,6 +32,7 @@ local function task() IN = request, OUT = response, ERR = errlog, + GC = gc, }, { __index = _ENV }) local f, err = load(dump, '=task', 't', env) if not f then @@ -40,8 +48,10 @@ while true do if not ok then errlog:push(result) end + collectgarbage() + gc:push(ID, collectgarbage 'count') end -]]):format(package.cpath, package.path, requestName, responseName) +]]):format(id, package.cpath, package.path, requestName, responseName) log.debug('Create thread, id: ', id) return { id = id, @@ -87,6 +97,17 @@ local function callback(id, running) end end +local function checkGC() + local gc = thread.channel 'gc' + while true do + local ok, id, count = gc:pop() + if not ok then + break + end + GCInfo[id] = count + end +end + local function onTick() local ok, msg = errlog:pop() if ok then @@ -95,9 +116,11 @@ local function onTick() for id, running in pairs(RunningList) do callback(id, running) end + checkGC() end return { onTick = onTick, run = run, + info = GCInfo, } diff --git a/server/src/async/proto.lua b/server/src/async/proto.lua index ef0b3300..bc2714af 100644 --- a/server/src/async/proto.lua +++ b/server/src/async/proto.lua @@ -53,4 +53,6 @@ end while true do readProto() + collectgarbage() + GC:push(ID, collectgarbage 'count') end |