From fafbf9763f585c1fb7f6a77b38f7958a10045639 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 22 Apr 2013 12:24:42 +0100 Subject: net.server.http: Ensure that event map cannot grow forever (limit to 10K wildcard-only entries) --- net/http/server.lua | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'net') diff --git a/net/http/server.lua b/net/http/server.lua index a983b8d5..3c2b55d5 100644 --- a/net/http/server.lua +++ b/net/http/server.lua @@ -27,6 +27,8 @@ local function is_wildcard_match(wildcard_event, event) return wildcard_event:sub(1, -2) == event:sub(1, #wildcard_event-1); end +local recent_wildcard_events, max_cached_wildcard_events = {}, 10000; + local event_map = events._event_map; setmetatable(events._handlers, { __index = function (handlers, curr_event) @@ -58,6 +60,12 @@ setmetatable(events._handlers, { handlers_array = false; end rawset(handlers, curr_event, handlers_array); + if not event_map[curr_event] then -- Only wildcard handlers match, if any + table.insert(recent_wildcard_events, curr_event); + if #recent_wildcard_events > max_cached_wildcard_events then + rawset(handlers, table.remove(recent_wildcard_events, 1), nil); + end + end return handlers_array; end; __newindex = function (handlers, curr_event, handlers_array) -- cgit v1.2.3 From 2592e23bae846f2044488f01a739e9d210e30f3e Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 22 Apr 2013 12:25:00 +0100 Subject: net.server.http: Add a comment --- net/http/server.lua | 1 + 1 file changed, 1 insertion(+) (limited to 'net') diff --git a/net/http/server.lua b/net/http/server.lua index 3c2b55d5..577b05ef 100644 --- a/net/http/server.lua +++ b/net/http/server.lua @@ -31,6 +31,7 @@ local recent_wildcard_events, max_cached_wildcard_events = {}, 10000; local event_map = events._event_map; setmetatable(events._handlers, { + -- Called when firing an event that doesn't exist (but may match a wildcard handler) __index = function (handlers, curr_event) if is_wildcard_event(curr_event) then return; end -- Wildcard events cannot be fired -- Find all handlers that could match this event, sort them -- cgit v1.2.3 From c63dc9181d8a0b747a297e7b108960eac8e18897 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 22 Apr 2013 12:35:52 +0100 Subject: mod_c2s, mod_s2s, net.http, net.http.server: Improve tracebacks (omit traceback function), to make it clearer where an error occured --- net/http.lua | 8 ++++---- net/http/server.lua | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'net') diff --git a/net/http.lua b/net/http.lua index 639ecf6a..3b783a41 100644 --- a/net/http.lua +++ b/net/http.lua @@ -17,9 +17,9 @@ local ssl_available = pcall(require, "ssl"); local server = require "net.server" local t_insert, t_concat = table.insert, table.concat; -local pairs, ipairs = pairs, ipairs; -local tonumber, tostring, xpcall, select, debug_traceback, char, format = - tonumber, tostring, xpcall, select, debug.traceback, string.char, string.format; +local pairs = pairs; +local tonumber, tostring, xpcall, select, traceback = + tonumber, tostring, xpcall, select, debug.traceback; local log = require "util.logger".init("http"); @@ -101,7 +101,7 @@ local function request_reader(request, data, err) request.parser:feed(data); end -local function handleerr(err) log("error", "Traceback[http]: %s: %s", tostring(err), debug_traceback()); end +local function handleerr(err) log("error", "Traceback[http]: %s", traceback(tostring(err), 2)); end function request(u, ex, callback) local req = url.parse(u); diff --git a/net/http/server.lua b/net/http/server.lua index 577b05ef..dec7da19 100644 --- a/net/http/server.lua +++ b/net/http/server.lua @@ -9,7 +9,7 @@ local pairs = pairs; local s_upper = string.upper; local setmetatable = setmetatable; local xpcall = xpcall; -local debug = debug; +local traceback = debug.traceback; local tostring = tostring; local codes = require "net.http.codes"; @@ -88,7 +88,7 @@ local _1, _2, _3; local function _handle_request() return handle_request(_1, _2, _3); end local last_err; -local function _traceback_handler(err) last_err = err; log("error", "Traceback[http]: %s: %s", tostring(err), debug.traceback()); end +local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end events.add_handler("http-error", function (error) return "Error processing request: "..codes[error.code]..". Check your error log for more information."; end, -1); -- cgit v1.2.3