diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-01-22 16:07:06 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-01-22 16:07:06 +0800 |
commit | d4b4de10daf18e389fc2b1c5d06e20ed433fcf04 (patch) | |
tree | 5af41a1abf037c433d04bf257f30099cb93c01bd /server/src/async | |
parent | 4ffc826d7ce5ca57c2929e040ed5eefb53f9d115 (diff) | |
download | lua-language-server-d4b4de10daf18e389fc2b1c5d06e20ed433fcf04.zip |
整理代码
Diffstat (limited to 'server/src/async')
-rw-r--r-- | server/src/async/async.lua | 55 | ||||
-rw-r--r-- | server/src/async/proto.lua | 7 | ||||
-rw-r--r-- | server/src/async/scanfiles.lua | 17 |
3 files changed, 48 insertions, 31 deletions
diff --git a/server/src/async/async.lua b/server/src/async/async.lua index 8c75540c..6e700ee0 100644 --- a/server/src/async/async.lua +++ b/server/src/async/async.lua @@ -20,30 +20,26 @@ local response = thread.channel(%q) local errlog = thread.channel 'errlog' local function task() - local dump, env = request:bpop() - if env then - setmetatable(env, { __index = _ENV }) - else - env = _ENV - end + local dump, arg = request:bpop() + local env = setmetatable({ + IN = request, + OUT = response, + ERR = errlog, + }, { __index = _ENV }) local f, err = load(dump, '=task', 't', env) if not f then errlog:push(err) return end - local results = table.pack(pcall(f)) - local ok = table.remove(results, 1) - if not ok then - local err = table.remove(results, 1) - errlog:push(err) - return - end - results.n = results.n - 1 - response:push(results) + local result = f(arg) + response:push(result) end while true do - task() + local ok, result = xpcall(task, debug.traceback) + if not ok then + errlog:push(result) + end end ]]):format(package.cpath, package.path, requestName, responseName) log.debug('Create thread, id: ', id) @@ -55,7 +51,11 @@ end } end -local function call(dump, env, callback) +local function run(name, arg, callback) + local dump = io.load(ROOT / 'src' / 'async' / (name .. '.lua')) + if not dump then + error(('找不到[%s]'):format(name)) + end local task = table.remove(idlePool) if not task then task = createTask() @@ -64,7 +64,9 @@ local function call(dump, env, callback) task = task, callback = callback, } - task.request:push(dump, env) + task.request:push(dump, arg) + -- TODO 线程回收后禁止外部再使用通道 + return task.request, task.response end local function onTick() @@ -73,17 +75,18 @@ local function onTick() log.error(msg) end for id, running in pairs(runningList) do - local ok, results = running.task.response:pop() - if ok then - runningList[id] = nil - idlePool[#idlePool+1] = running.task - xpcall(running.callback, log.debug, table.unpack(results)) + if running.callback then + local ok, result = running.task.response:pop() + if ok then + runningList[id] = nil + idlePool[#idlePool+1] = running.task + xpcall(running.callback, log.error, result) + end end end end return { - onTick = onTick, - call = call, - require = require, + onTick = onTick, + run = run, } diff --git a/server/src/async/proto.lua b/server/src/async/proto.lua index 4570de2e..ef0b3300 100644 --- a/server/src/async/proto.lua +++ b/server/src/async/proto.lua @@ -1,7 +1,4 @@ -local thread = require 'bee.thread' local json = require 'json' -local proto = thread.channel 'proto' -local errlog = thread.channel 'errlog' local function pushError(...) local t = table.pack(...) @@ -9,7 +6,7 @@ local function pushError(...) t[i] = tostring(t[i]) end local buf = table.concat(t, '\t') - errlog:push(buf) + ERR:push(buf) end local function readProtoHeader() @@ -51,7 +48,7 @@ local function readProto() if not data then return end - proto:push(data) + OUT:push(data) end while true do diff --git a/server/src/async/scanfiles.lua b/server/src/async/scanfiles.lua new file mode 100644 index 00000000..900cc0a8 --- /dev/null +++ b/server/src/async/scanfiles.lua @@ -0,0 +1,17 @@ +local root = ... + +require 'utility' +local fs = require 'bee.filesystem' +local list = {} +local ignore = { + ['.git'] = true, + ['node_modules'] = true, +} + +for path in io.scan(fs.path(root), ignore) do + if path:extension():string() == '.lua' then + list[#list+1] = path:string() + end +end + +OUT:push(list) |