Skip to content

Commit

Permalink
perf(http): reduce memory usage
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Oct 8, 2023
1 parent c606d6b commit 34e336d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 50 deletions.
23 changes: 13 additions & 10 deletions http/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,18 @@ end
local methods = {}

function methods:close()
local mt = getmetatable(self)
local sock = mt.sock
local sock = self.__sock

if not sock then
return
end

sock:close()
mt.sock = nil
self.sock = nil
end

function methods:sock()
local sock = getmetatable(self).sock
local sock = self.__sock
if sock then
return sock
end
Expand Down Expand Up @@ -410,16 +409,18 @@ function methods:request(method, url, body, opts)

self:close()

getmetatable(self).sock = sock
self.__sock = sock

return do_http_request(sock, method, path, headers, body, opts)
end

local metatable = {
__index = methods,
__gc = methods.close
}

function M.new()
return setmetatable({}, {
__index = methods,
__gc = methods.close
})
return setmetatable({}, metatable)
end

local function body_is_file(body)
Expand Down Expand Up @@ -470,6 +471,8 @@ function M.post(url, body, opts)
return M.request('POST', url, body, opts)
end

local body_file_mt = { name = BODY_FILE_MT }

function M.body_with_file(name)
local st, err = file.stat(name)
if not st then
Expand All @@ -489,7 +492,7 @@ function M.body_with_file(name)
size = st.size
}

return setmetatable(o, { name = BODY_FILE_MT })
return setmetatable(o, body_file_mt)
end

return M
70 changes: 30 additions & 40 deletions http/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,15 @@ local month_abbr_map = {
local methods = {}

function methods:closed()
return getmetatable(self).sock:closed()
return self.sock:closed()
end

function methods:remote_addr()
return getmetatable(self).peer
return self.peer
end

function methods:add_header(name, value)
local resp = getmetatable(self).resp
local resp = self.resp

if resp.head_sent then
error('http head has been sent')
Expand All @@ -233,7 +233,7 @@ function methods:add_header(name, value)
end

function methods:set_status(code, status)
local resp = getmetatable(self).resp
local resp = self.resp

if resp.head_sent then
error('http head has been sent')
Expand Down Expand Up @@ -288,9 +288,7 @@ local function send_http_head(resp)
end

function methods:send_error(code, status, content)
local mt = getmetatable(self)

if mt.sock:closed() then
if self.sock:closed() then
return false, 'closed'
end

Expand All @@ -304,17 +302,16 @@ function methods:send_error(code, status, content)
if content then
self:send(content)
else
send_http_head(mt.resp)
send_http_head(self.resp)
end

return true
end

function methods:send(...)
local mt = getmetatable(self)
local resp = mt.resp
local resp = self.resp

if mt.sock:closed() then
if self.sock:closed() then
return false, 'closed'
end

Expand Down Expand Up @@ -367,9 +364,8 @@ local function http_send_file(sock, fd, size, count, offset)
end

function methods:send_file_fd(fd, size, count, offset)
local mt = getmetatable(self)
local resp = mt.resp
local sock = mt.sock
local resp = self.resp
local sock = self.sock

if count and count < 1 then
return true
Expand All @@ -394,9 +390,7 @@ function methods:send_file_fd(fd, size, count, offset)
end

function methods:send_file(path, count, offset)
local mt = getmetatable(self)

if mt.sock:closed() then
if self.sock:closed() then
return false, 'closed'
end

Expand Down Expand Up @@ -429,9 +423,8 @@ function methods:send_file(path, count, offset)
end

function methods:flush()
local mt = getmetatable(self)
local resp = mt.resp
local sock = mt.sock
local resp = self.resp
local sock = self.sock
local data = resp.data

if not resp.head_sent then
Expand All @@ -454,9 +447,8 @@ function methods:flush()
end

function methods:read_body(count, timeout)
local mt = getmetatable(self)
local body_remain = mt.body_remain
local sock = mt.sock
local body_remain = self.body_remain
local sock = self.sock

if sock:closed() then
return nil, 'closed'
Expand Down Expand Up @@ -487,16 +479,15 @@ function methods:read_body(count, timeout)
return nil, err
end

mt.body_remain = mt.body_remain - #data
self.body_remain = self.body_remain - #data

return data
end

function methods:discard_body()
local mt = getmetatable(self)
local sock = mt.sock
local sock = self.sock

local _, err = sock:discard(mt.body_remain, 3.0)
local _, err = sock:discard(self.body_remain, 3.0)
if err then
sock:close()
return false, err
Expand All @@ -506,11 +497,10 @@ function methods:discard_body()
end

function methods:serve_file(req)
local mt = getmetatable(self)
local options = mt.options
local options = self.options
local path = req.path

if mt.sock:closed() then
if self.sock:closed() then
return false, 'closed'
end

Expand Down Expand Up @@ -606,12 +596,11 @@ function methods:serve_file(req)
end

local function handle_connection(con, handler)
local mt = getmetatable(con)
local sock = mt.sock
local peer = mt.peer
local sock = con.sock
local peer = con.peer

local log_prefix = peer.ipaddr .. ':' .. peer.port .. ': '
local http_keepalive = mt.options.http_keepalive
local http_keepalive = con.options.http_keepalive
local read_timeout = 3.0

local method, path, major_version, minor_version
Expand Down Expand Up @@ -691,7 +680,7 @@ local function handle_connection(con, handler)
end
end

mt.body_remain = tonumber(headers['content-length'] or 0)
con.body_remain = tonumber(headers['content-length'] or 0)

local resp = {
major_version = major_version,
Expand All @@ -709,7 +698,7 @@ local function handle_connection(con, handler)
resp.headers['keep-alive'] = string.format('timeout=%d', http_keepalive)
end

mt.resp = resp
con.resp = resp

local req = {
method = method,
Expand Down Expand Up @@ -788,6 +777,8 @@ local function set_socket_options(sock, options)
end
end

local metatable = { __index = methods }

function M.listen(ipaddr, port, options, handler)
options = options or {}

Expand Down Expand Up @@ -832,7 +823,7 @@ function M.listen(ipaddr, port, options, handler)
log.debug(peer.ipaddr .. ':' .. peer.port .. ': new connection')

eco.run(function(c)
local con = setmetatable({}, {
local con = setmetatable({
sock = c,
resp = {
code = 200,
Expand All @@ -841,9 +832,8 @@ function M.listen(ipaddr, port, options, handler)
}
},
peer = peer,
options = options,
__index = methods
})
options = options
}, metatable)

while not c:closed() do
if not handle_connection(con, handler) then
Expand Down

0 comments on commit 34e336d

Please sign in to comment.