Skip to content

Commit 0187ddf

Browse files
committed
Fixed a CPU usage issue again
1 parent dada769 commit 0187ddf

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
## Unreleased
44
<!-- Add all new changes here. They will be moved under a version at release -->
55

6+
## 3.17.1
7+
`2026-01-20`
8+
* `FIX` Fixed a CPU usage issue again
9+
610
## 3.17.0
711
`2026-01-19`
812
* `NEW` Support `fun<T>` syntax for inline generic function types in `@field` and `@type` annotations [#1170](https://github.com/LuaLS/lua-language-server/issues/1170)

script/brave/brave.lua

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
local thread = require 'bee.thread'
2-
local channelMod = require 'bee.channel'
3-
local selectMod = require 'bee.select'
1+
local channel = require 'bee.channel'
2+
local epoll = require 'bee.epoll'
43

54
local reqPad
65
local resPad
@@ -18,8 +17,8 @@ m.queue = {}
1817
function m.register(id, taskChName, replyChName)
1918
m.id = id
2019

21-
reqPad = channelMod.query(taskChName)
22-
resPad = channelMod.query(replyChName)
20+
reqPad = channel.query(taskChName)
21+
resPad = channel.query(replyChName)
2322

2423
assert(reqPad, 'task channel not found: ' .. taskChName)
2524
assert(resPad, 'reply channel not found: ' .. replyChName)
@@ -53,37 +52,32 @@ end
5352

5453
--- 开始找工作
5554
function m.start()
56-
local selector = selectMod.create()
57-
selector:event_add(reqPad:fd(), selectMod.SELECT_READ)
55+
local epfd <close> = assert(epoll.create(16))
56+
epfd:event_add(reqPad:fd(), epoll.EPOLLIN)
5857

5958
m.push('mem', collectgarbage 'count')
6059
while true do
61-
-- 使用 select 实现阻塞等待
62-
local name, id, params
63-
while true do
64-
local ok, n, i, p = reqPad:pop()
65-
if ok then
66-
name, id, params = n, i, p
67-
break
60+
for _, event in epfd:wait() do
61+
if event & epoll.EPOLLIN ~= 0 then
62+
local ok, name, id, params = reqPad:pop()
63+
if ok then
64+
local ability = m.ability[name]
65+
if not ability then
66+
resPad:push(id)
67+
log.error('Brave can not handle this work: ' .. name)
68+
goto CONTINUE
69+
end
70+
local suc, res = xpcall(ability, log.error, params)
71+
if suc then
72+
resPad:push(id, res)
73+
else
74+
resPad:push(id)
75+
end
76+
m.push('mem', collectgarbage 'count')
77+
::CONTINUE::
78+
end
6879
end
69-
selector:wait(-1)
7080
end
71-
72-
local ability = m.ability[name]
73-
-- TODO
74-
if not ability then
75-
resPad:push(id)
76-
log.error('Brave can not handle this work: ' .. name)
77-
goto CONTINUE
78-
end
79-
local ok, res = xpcall(ability, log.error, params)
80-
if ok then
81-
resPad:push(id, res)
82-
else
83-
resPad:push(id)
84-
end
85-
m.push('mem', collectgarbage 'count')
86-
::CONTINUE::
8781
end
8882
end
8983

script/pub/pub.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ function m.pushTask(info)
133133
-- 找到空闲 brave,直接推送
134134
brave.busy = true
135135
brave.currentTask = info.id
136+
log.debug('push task to brave:', brave.id, info.name, info.id)
136137
brave.taskCh:push(info.name, info.id, info.params)
137138
m.taskMap[info.id] = info
138139
return true
@@ -145,13 +146,8 @@ function m.popTask(brave, id, result)
145146
log.warn(('Brave pushed unknown task result: # %d => [%d]'):format(brave.id, id))
146147
return
147148
end
149+
log.debug('pop task from brave:', brave.id, info.name, id)
148150
m.taskMap[id] = nil
149-
if not info.removed then
150-
info.removed = true
151-
if info.callback then
152-
xpcall(info.callback, log.error, result)
153-
end
154-
end
155151

156152
-- 任务完成,标记为空闲
157153
brave.busy = false
@@ -165,10 +161,18 @@ function m.popTask(brave, id, result)
165161
table.remove(queue, i)
166162
brave.busy = true
167163
brave.currentTask = nextTask.id
164+
log.debug('push task to brave:', brave.id, nextTask.name, nextTask.id)
168165
brave.taskCh:push(nextTask.name, nextTask.id, nextTask.params)
169166
break
170167
end
171168
end
169+
170+
if not info.removed then
171+
info.removed = true
172+
if info.callback then
173+
xpcall(info.callback, log.error, result)
174+
end
175+
end
172176
end
173177

174178
--- 从勇者处接收报告

script/service/service.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ function m.eventLoop()
193193

194194
while true do
195195
net.update()
196-
local clock = os.clock()
197-
while os.clock() - clock < 0.1 do
196+
log.debug('net update')
197+
local clock = time.monotonic()
198+
while time.monotonic() - clock < 100 do
198199
doSomething()
199200
end
200201
if doSomething() then

0 commit comments

Comments
 (0)