From b812c0e7675a7bd4e7ad31cfbfc800bf298d813d Mon Sep 17 00:00:00 2001 From: chronolaw Date: Tue, 20 Aug 2024 14:05:42 +0800 Subject: [PATCH] refactor(lib): add common utils --- lualib/resty/events/broker.lua | 5 +++-- lualib/resty/events/protocol.lua | 15 +-------------- lualib/resty/events/utils.lua | 18 ++++++++++++++++++ lualib/resty/events/worker.lua | 3 ++- 4 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 lualib/resty/events/utils.lua diff --git a/lualib/resty/events/broker.lua b/lualib/resty/events/broker.lua index a7ddbdd..26e4661 100644 --- a/lualib/resty/events/broker.lua +++ b/lualib/resty/events/broker.lua @@ -2,9 +2,10 @@ local cjson = require "cjson.safe" local codec = require "resty.events.codec" local lrucache = require "resty.lrucache" local queue = require "resty.events.queue" +local utils = require "resty.events.utils" local server = require("resty.events.protocol").server -local is_timeout = server.is_timeout -local is_closed = server.is_closed +local is_timeout = utils.is_timeout +local is_closed = utils.is_closed local setmetatable = setmetatable local random = math.random diff --git a/lualib/resty/events/protocol.lua b/lualib/resty/events/protocol.lua index 392cdbe..d629dc3 100644 --- a/lualib/resty/events/protocol.lua +++ b/lualib/resty/events/protocol.lua @@ -6,7 +6,7 @@ local _send_frame = frame.send local encode = codec.encode local decode = codec.decode -local ngx = ngx +local ngx = ngx -- luacheck: ignore local worker_id = ngx.worker.id local worker_pid = ngx.worker.pid local tcp = ngx.socket.tcp @@ -28,15 +28,6 @@ local WORKER_INFO = { pid = 0, } -local function is_timeout(err) - return err and str_sub(err, -7) == "timeout" -end - -local function is_closed(err) - return err and (str_sub(err, -6) == "closed" or - str_sub(err, -11) == "broken pipe") -end - local function recv_frame(self) local sock = self.sock if not sock then @@ -56,8 +47,6 @@ local function send_frame(self, payload) end local _Server = { - is_closed = is_closed, - is_timeout = is_timeout, recv_frame = recv_frame, send_frame = send_frame, } @@ -109,8 +98,6 @@ function _Server.new() end local _Client = { - is_closed = is_closed, - is_timeout = is_timeout, recv_frame = recv_frame, send_frame = send_frame, } diff --git a/lualib/resty/events/utils.lua b/lualib/resty/events/utils.lua new file mode 100644 index 0000000..a9ed3ff --- /dev/null +++ b/lualib/resty/events/utils.lua @@ -0,0 +1,18 @@ +local str_sub = string.sub + + +local function is_timeout(err) + return err and str_sub(err, -7) == "timeout" +end + + +local function is_closed(err) + return err and (str_sub(err, -6) == "closed" or + str_sub(err, -11) == "broken pipe") +end + + +return { + is_timeout = is_timeout, + is_closed = is_closed, +} diff --git a/lualib/resty/events/worker.lua b/lualib/resty/events/worker.lua index 9e034f6..7e70a8b 100644 --- a/lualib/resty/events/worker.lua +++ b/lualib/resty/events/worker.lua @@ -2,10 +2,11 @@ local cjson = require "cjson.safe" local codec = require "resty.events.codec" local queue = require "resty.events.queue" local callback = require "resty.events.callback" +local utils = require "resty.events.utils" local frame_validate = require("resty.events.frame").validate local client = require("resty.events.protocol").client -local is_timeout = client.is_timeout +local is_timeout = utils.is_timeout local type = type local assert = assert