From 38d2478af09ff9e25fed61e1137c1a850c828f40 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Wed, 20 Feb 2019 10:23:47 +1100 Subject: [PATCH] made gopherd less CPU-heavy --- gopherd.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gopherd.lua b/gopherd.lua index 1018cd8..96ba8f3 100644 --- a/gopherd.lua +++ b/gopherd.lua @@ -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