diff --git a/lualib/resty/events/init.lua b/lualib/resty/events/init.lua index bdab4729..8fc9a27e 100644 --- a/lualib/resty/events/init.lua +++ b/lualib/resty/events/init.lua @@ -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 @@ -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 diff --git a/lualib/resty/events/worker.lua b/lualib/resty/events/worker.lua index a0311f4a..1b16cba3 100644 --- a/lualib/resty/events/worker.lua +++ b/lualib/resty/events/worker.lua @@ -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 @@ -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 diff --git a/t/events.t b/t/events.t index 3a861db6..1175b495 100644 --- a/t/events.t +++ b/t/events.t @@ -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)