Skip to content

Commit

Permalink
refactor(lib): add common utils
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Aug 20, 2024
1 parent 2dcd1d7 commit b812c0e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
5 changes: 3 additions & 2 deletions lualib/resty/events/broker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 1 addition & 14 deletions lualib/resty/events/protocol.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
}
Expand Down Expand Up @@ -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,
}
Expand Down
18 changes: 18 additions & 0 deletions lualib/resty/events/utils.lua
Original file line number Diff line number Diff line change
@@ -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,
}
3 changes: 2 additions & 1 deletion lualib/resty/events/worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b812c0e

Please sign in to comment.