Browse Source

Merge pull request #91 from HHHartmann/BetterLoggingAndMoreCloseConnections

Improve logging and cleanup when ConnectionThread failes
Marcos 7 years ago
parent
commit
a09b4b4758
1 changed files with 20 additions and 2 deletions
  1. 20 2
      httpserver.lua

+ 20 - 2
httpserver.lua

@@ -31,15 +31,23 @@ return function (port)
                fileServeFunction(bufferedConnection, req, args)
                -- The bufferedConnection may still hold some data that hasn't been sent. Flush it before closing.
                if not bufferedConnection:flush() then
+                  log(connection, "closing connection", "no (more) data")
                   connection:close()
                   connectionThread = nil
+                  collectgarbage()
                end
             end)
 
             local BufferedConnectionClass = dofile("httpserver-connection.lc")
             local bufferedConnection = BufferedConnectionClass:new(connection)
             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 connection", "error")
+               connection:close()
+               connectionThread = nil
+               collectgarbage()
+            end
          end
 
          local function handleRequest(connection, req)
@@ -139,16 +147,26 @@ return function (port)
                if connectionThreadStatus == "suspended" then
                   -- Not finished sending file, resume.
                   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 connection", "error")
+                     connection:close()
+                     connectionThread = nil
+                     collectgarbage()
+                  end
                elseif connectionThreadStatus == "dead" then
                   -- We're done sending file.
+                  log(connection, "closing connection","thread is dead")
                   connection:close()
                   connectionThread = nil
+                  collectgarbage()
                end
             end
          end
 
          local function onDisconnect(connection, payload)
+-- this should rather be a log call, but log is not available here
+--            print("disconnected")
             if connectionThread then
                connectionThread = nil
                collectgarbage()