Skip to content

Commit 64f8d57

Browse files
committed
http/cookie: Optimisation for adding new cookies
1 parent 92d5ba8 commit 64f8d57

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

http/cookie.lua

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,20 +192,18 @@ local function add_to_store(self, cookie, req_is_http, now)
192192
-- This was all just a trigger to delete the old cookie
193193
self:remove(cookie.domain, cookie.path, cookie.name)
194194
else
195-
-- Insert the newly created cookie into the cookie store.
196-
local domain_cookies = self.domains[cookie.domain]
197-
if domain_cookies == nil then
198-
domain_cookies = {}
199-
self.domains[cookie.domain] = domain_cookies
200-
self.n_cookies_per_domain[cookie.domain] = 0
201-
end
202-
local path_cookies = domain_cookies[cookie.path]
203-
if path_cookies == nil then
204-
path_cookies = {}
205-
domain_cookies[cookie.path] = path_cookies
195+
local name = cookie.name
196+
local domain = cookie.domain
197+
local domain_cookies = self.domains[domain]
198+
local path_cookies
199+
local old_cookie
200+
if domain_cookies ~= nil then
201+
path_cookies = domain_cookies[cookie.path]
202+
if path_cookies ~= nil then
203+
old_cookie = path_cookies[name]
204+
end
206205
end
207206

208-
local old_cookie = path_cookies[cookie.name]
209207
-- If the cookie store contains a cookie with the same name,
210208
-- domain, and path as the newly created cookie:
211209
if old_cookie then
@@ -223,15 +221,35 @@ local function add_to_store(self, cookie, req_is_http, now)
223221
-- Remove the old-cookie from the cookie store.
224222
self.expiry_heap:remove(old_cookie)
225223
else
226-
if self.n_cookies >= self.max_cookies
227-
or self.n_cookies_per_domain[cookie.domain] >= self.max_cookies_per_domain then
224+
if self.n_cookies >= self.max_cookies or self.max_cookies_per_domain < 1 then
228225
return false
229226
end
230-
self.n_cookies_per_domain[cookie.domain] = self.n_cookies_per_domain[cookie.domain] + 1
227+
228+
-- Cookie will be added
229+
if domain_cookies == nil then
230+
path_cookies = {}
231+
domain_cookies = {
232+
[cookie.path] = path_cookies;
233+
}
234+
self.domains[domain] = domain_cookies
235+
self.n_cookies_per_domain[domain] = 1
236+
else
237+
local n_cookies_per_domain = self.n_cookies_per_domain[domain]
238+
if n_cookies_per_domain >= self.max_cookies_per_domain then
239+
return false
240+
end
241+
path_cookies = domain_cookies[cookie.path]
242+
if path_cookies == nil then
243+
path_cookies = {}
244+
domain_cookies[cookie.path] = path_cookies
245+
end
246+
self.n_cookies_per_domain[domain] = n_cookies_per_domain
247+
end
248+
231249
self.n_cookies = self.n_cookies + 1
232250
end
233251

234-
path_cookies[cookie.name] = cookie
252+
path_cookies[name] = cookie
235253
self.expiry_heap:insert(cookie.expiry_time, cookie)
236254
end
237255

0 commit comments

Comments
 (0)