Skip to content

Commit

Permalink
perf(websocket): 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 7135ff3 commit 8f95cab
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions websocket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ local types = {
local methods = {}

function methods:recv_frame(timeout)
local mt = getmetatable(self)
local sock = mt.sock
local opts = mt.opts
local sock = self.sock
local opts = self.opts

local data, err = sock:recvfull(2, timeout)
if not data then
Expand Down Expand Up @@ -221,9 +220,8 @@ local function build_frame(fin, opcode, payload_len, payload, masking)
end

function methods:send_frame(fin, opcode, payload)
local mt = getmetatable(self)
local sock = mt.sock
local opts = mt.opts
local sock = self.sock
local opts = self.opts

if not payload then
payload = ''
Expand All @@ -248,7 +246,7 @@ function methods:send_frame(fin, opcode, payload)
end
end

local frame, err = build_frame(fin, opcode, payload_len, payload, mt.masking)
local frame, err = build_frame(fin, opcode, payload_len, payload, self.masking)
if not frame then
return nil, 'failed to build frame: ' .. err
end
Expand Down Expand Up @@ -288,6 +286,8 @@ function methods:send_pong(data)
return self:send_frame(true, 0xa, data)
end

local metatable = { __index = methods }

function M.upgrade(con, req, opts)
local mt = getmetatable(con)
local resp = mt.resp
Expand Down Expand Up @@ -355,11 +355,10 @@ function M.upgrade(con, req, opts)

opts.max_payload_len = opts.max_payload_len or 65535

return setmetatable({}, {
__index = methods,
return setmetatable({
sock = mt.sock,
opts = opts
})
}, metatable)
end

function M.connect(uri, opts)
Expand Down Expand Up @@ -400,13 +399,12 @@ function M.connect(uri, opts)

opts.max_payload_len = opts.max_payload_len or 65535

return setmetatable({}, {
__index = methods,
return setmetatable({
masking = true,
hc = hc,
sock = hc:sock(),
opts = opts
})
}, metatable)
end

return M

0 comments on commit 8f95cab

Please sign in to comment.