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