Skip to content

Commit

Permalink
opt max_payload_len
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Jul 12, 2023
1 parent 70cc83f commit 7b00d72
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
11 changes: 11 additions & 0 deletions lualib/resty/events/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local function check_options(opts)
local UNIX_PREFIX = "unix:"
local DEFAULT_UNIQUE_TIMEOUT = 5
local DEFAULT_MAX_QUEUE_LEN = 1024 * 10
local DEFAULT_MAX_PAYLOAD_LEN = 1024 * 64 -- 64KB

opts.broker_id = opts.broker_id or 0

Expand Down Expand Up @@ -66,6 +67,16 @@ local function check_options(opts)
return nil, '"max_queue_len" option is invalid'
end

opts.max_payload_len = opts.max_payload_len or DEFAULT_MAX_PAYLOAD_LEN

if type(opts.max_payload_len) ~= "number" then
return nil, '"max_payload_len" option must be a number'
end

if opts.max_payload_len < 0 then
return nil, '"max_payload_len" option is invalid'
end

opts.testing = opts.testing or false

if type(opts.testing) ~= "boolean" then
Expand Down
10 changes: 7 additions & 3 deletions lualib/resty/events/worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ end

-- posts a new event
local function post_event(self, source, event, data, spec)
local str, ok, err
local str, ok, len, err

EVENT_T.source = source
EVENT_T.event = event
Expand All @@ -303,10 +303,14 @@ local function post_event(self, source, event, data, spec)
return nil, err
end

ok, err = frame_validate(str)
if not ok then
len, err = frame_validate(str)
if not len then
return nil, err
end
if len > self._opts.max_payload_len then
return nil, "payload exceeds the limitation " ..
"(" .. self._opts.max_payload_len .. ")"
end

ok, err = self._pub_queue:push(str)
if not ok then
Expand Down
1 change: 1 addition & 0 deletions t/events.t
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ optional "unique_timeout" option must be a number
local opts = {
--broker_id = 0,
listening = "unix:$TEST_NGINX_HTML_DIR/nginx.sock",
max_payload_len = 1024 * 1024 * 2,
}

local ev = require("resty.events").new(opts)
Expand Down

0 comments on commit 7b00d72

Please sign in to comment.