Improve Connection Close handling

close connection on coroutine errors and improve logging around closieg
connections.
Release memory warlier
This commit is contained in:
Gregor 2017-05-29 21:25:35 +02:00
parent 90cb4e47aa
commit e4d829b6c9

View File

@ -31,15 +31,23 @@ return function (port)
fileServeFunction(bufferedConnection, req, args) fileServeFunction(bufferedConnection, req, args)
-- The bufferedConnection may still hold some data that hasn't been sent. Flush it before closing. -- The bufferedConnection may still hold some data that hasn't been sent. Flush it before closing.
if not bufferedConnection:flush() then if not bufferedConnection:flush() then
log(connection, "closing connetion", "no (more) data")
connection:close() connection:close()
connectionThread = nil connectionThread = nil
collectgarbage()
end end
end) end)
local BufferedConnectionClass = dofile("httpserver-connection.lc") local BufferedConnectionClass = dofile("httpserver-connection.lc")
local bufferedConnection = BufferedConnectionClass:new(connection) local bufferedConnection = BufferedConnectionClass:new(connection)
local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args) local status, err = coroutine.resume(connectionThread, fileServeFunction, bufferedConnection, req, args)
if not status then log(connection, "Error: "..err) end if not status then
log(connection, "Error: "..err)
log(connection, "closing connetion", "error")
connection:close()
connectionThread = nil
collectgarbage()
end
end end
local function handleRequest(connection, req) local function handleRequest(connection, req)
@ -139,16 +147,25 @@ return function (port)
if connectionThreadStatus == "suspended" then if connectionThreadStatus == "suspended" then
-- Not finished sending file, resume. -- Not finished sending file, resume.
local status, err = coroutine.resume(connectionThread) local status, err = coroutine.resume(connectionThread)
if not status then log(connection:getpeer(), "Error: " .. err) end if not status then
log(connection, "Error: "..err)
log(connection, "closing connetion", "error")
connection:close()
connectionThread = nil
collectgarbage()
end
elseif connectionThreadStatus == "dead" then elseif connectionThreadStatus == "dead" then
-- We're done sending file. -- We're done sending file.
log(connection, "closing connetion","thread is dead")
connection:close() connection:close()
connectionThread = nil connectionThread = nil
collectgarbage()
end end
end end
end end
local function onDisconnect(connection, payload) local function onDisconnect(connection, payload)
print("disconnected")
if connectionThread then if connectionThread then
connectionThread = nil connectionThread = nil
collectgarbage() collectgarbage()