made gopherd less CPU-heavy

This commit is contained in:
Izaya 2019-02-20 10:23:47 +11:00
parent 6b058c1efa
commit 38d2478af0

View File

@ -67,11 +67,12 @@ end
local function handleConnect(client) local function handleConnect(client)
client:settimeout(0) client:settimeout(0)
threads[pcount+1] = coroutine.create(function() local w,err = pcall(function() threads[pcount+1] = coroutine.create(function() local w,err = pcall(function()
local host,port = client:getsockname()
repeat repeat
coroutine.yield() coroutine.yield()
line=client:receive() line=client:receive()
until line until line
print(line) print(string.format("%s:%d: %s",host,port,line))
local path,args = config.path .. cleanPath(line) local path,args = config.path .. cleanPath(line)
local attr = fs.attributes(path) local attr = fs.attributes(path)
if attr then if attr then
@ -121,17 +122,23 @@ local function handleConnect(client)
end end
local server = socket.bind("*",config.bindport) local server = socket.bind("*",config.bindport)
server:settimeout(config.timer)
while true do -- totally not a scheduler while true do -- totally not a scheduler
client = server:accept() client = server:accept()
if client then if client then
server:settimeout(config.timer)
handleConnect(client) handleConnect(client)
end end
local c = 0
for k,v in pairs(threads) do for k,v in pairs(threads) do
if coroutine.status(v) == "dead" then if coroutine.status(v) == "dead" then
threads[k] = nil threads[k] = nil
else else
coroutine.resume(v) coroutine.resume(v)
c = c + 1
end end
end end
if c == 0 then
-- if there are no clients connected, make server:accept() block forever
server:settimeout(inf)
end
end end