Comparison

net/http/server.lua @ 5503:91052e59375c

net.server.http: Ensure that event map cannot grow forever (limit to 10K wildcard-only entries)
author Matthew Wild <mwild1@gmail.com>
date Mon, 22 Apr 2013 12:24:42 +0100
parent 5487:03ddf2375d48
child 5504:b760b5f0c2b0
comparison
equal deleted inserted replaced
5496:7a0b81b5ca71 5503:91052e59375c
24 return event:sub(-2, -1) == "/*"; 24 return event:sub(-2, -1) == "/*";
25 end 25 end
26 local function is_wildcard_match(wildcard_event, event) 26 local function is_wildcard_match(wildcard_event, event)
27 return wildcard_event:sub(1, -2) == event:sub(1, #wildcard_event-1); 27 return wildcard_event:sub(1, -2) == event:sub(1, #wildcard_event-1);
28 end 28 end
29
30 local recent_wildcard_events, max_cached_wildcard_events = {}, 10000;
29 31
30 local event_map = events._event_map; 32 local event_map = events._event_map;
31 setmetatable(events._handlers, { 33 setmetatable(events._handlers, {
32 __index = function (handlers, curr_event) 34 __index = function (handlers, curr_event)
33 if is_wildcard_event(curr_event) then return; end -- Wildcard events cannot be fired 35 if is_wildcard_event(curr_event) then return; end -- Wildcard events cannot be fired
56 end); 58 end);
57 else 59 else
58 handlers_array = false; 60 handlers_array = false;
59 end 61 end
60 rawset(handlers, curr_event, handlers_array); 62 rawset(handlers, curr_event, handlers_array);
63 if not event_map[curr_event] then -- Only wildcard handlers match, if any
64 table.insert(recent_wildcard_events, curr_event);
65 if #recent_wildcard_events > max_cached_wildcard_events then
66 rawset(handlers, table.remove(recent_wildcard_events, 1), nil);
67 end
68 end
61 return handlers_array; 69 return handlers_array;
62 end; 70 end;
63 __newindex = function (handlers, curr_event, handlers_array) 71 __newindex = function (handlers, curr_event, handlers_array)
64 if handlers_array == nil 72 if handlers_array == nil
65 and is_wildcard_event(curr_event) then 73 and is_wildcard_event(curr_event) then