Software /
code /
prosody
Comparison
net/http/server.lua @ 6950:8ab809358922
net.http.server: Use new util.cache to remember wildcard event handlers
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 03 Dec 2015 14:54:29 +0000 |
parent | 6608:b6e558febb7a |
child | 6963:e3be91ccca05 |
comparison
equal
deleted
inserted
replaced
6949:1c2c3d913172 | 6950:8ab809358922 |
---|---|
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 | 29 |
30 local recent_wildcard_events, max_cached_wildcard_events = {}, 10000; | 30 local _handlers = events._handlers; |
31 local recent_wildcard_events = cache.new(10000, function (key, value) | |
32 rawset(_handlers, key, nil); | |
33 end); | |
31 | 34 |
32 local event_map = events._event_map; | 35 local event_map = events._event_map; |
33 setmetatable(events._handlers, { | 36 setmetatable(events._handlers, { |
34 -- Called when firing an event that doesn't exist (but may match a wildcard handler) | 37 -- Called when firing an event that doesn't exist (but may match a wildcard handler) |
35 __index = function (handlers, curr_event) | 38 __index = function (handlers, curr_event) |
60 else | 63 else |
61 handlers_array = false; | 64 handlers_array = false; |
62 end | 65 end |
63 rawset(handlers, curr_event, handlers_array); | 66 rawset(handlers, curr_event, handlers_array); |
64 if not event_map[curr_event] then -- Only wildcard handlers match, if any | 67 if not event_map[curr_event] then -- Only wildcard handlers match, if any |
65 table.insert(recent_wildcard_events, curr_event); | 68 recent_wildcard_events:set(curr_event, true); |
66 if #recent_wildcard_events > max_cached_wildcard_events then | |
67 rawset(handlers, table.remove(recent_wildcard_events, 1), nil); | |
68 end | |
69 end | 69 end |
70 return handlers_array; | 70 return handlers_array; |
71 end; | 71 end; |
72 __newindex = function (handlers, curr_event, handlers_array) | 72 __newindex = function (handlers, curr_event, handlers_array) |
73 if handlers_array == nil | 73 if handlers_array == nil |