-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: improve the implementation of pubsub module #7043
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f8e496
chore: adjust test order to match the alphabet
bzp2010 662cff6
chore: adjust centos7 ci test order
bzp2010 701c55c
feat: improve pubsub logic
bzp2010 57088bc
chore: adjust tparam style
bzp2010 618d327
fix: get_cmd called logic error
bzp2010 335f97c
fix: typo
bzp2010 a62da65
fix: variable name error
bzp2010 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,23 +58,42 @@ local function init_pb_state() | |
end | ||
|
||
|
||
-- parse command name and parameters from client message | ||
local function get_cmd(data) | ||
for key, value in pairs(data) do | ||
-- There are sequence and command properties in the data, | ||
-- select the handler according to the command value. | ||
if key ~= "sequence" then | ||
return key, value | ||
end | ||
end | ||
end | ||
|
||
|
||
-- send generic response to client | ||
local function send_resp(ws, sequence, data) | ||
data.sequence = sequence | ||
local ok, encoded = pcall(pb.encode, "PubSubResp", data) | ||
if not ok or not encoded then | ||
log.error("failed to encode response message, err: ", encoded) | ||
return | ||
end | ||
|
||
local _, err = ws:send_binary(encoded) | ||
if err then | ||
log.error("failed to send response to client, err: ", err) | ||
end | ||
end | ||
|
||
|
||
-- send error response to client | ||
local function send_error(ws, sequence, err_msg) | ||
local ok, data = pcall(pb.encode, "PubSubResp", { | ||
sequence = sequence, | ||
return send_resp(ws, sequence, { | ||
error_resp = { | ||
code = 0, | ||
message = err_msg, | ||
}, | ||
}) | ||
if not ok or not data then | ||
log.error("failed to encode error response message, err: ", data) | ||
end | ||
|
||
local _, err = ws:send_binary(data) | ||
if err then | ||
log.error("failed to send response to client, err: ", err) | ||
end | ||
end | ||
|
||
|
||
|
@@ -119,8 +138,8 @@ end | |
-- no error exists. | ||
-- | ||
-- @function core.pubsub.on | ||
-- @tparam string command to add callback | ||
-- @tparam function handler callback on receipt of command | ||
-- @tparam string command The command to add callback. | ||
-- @tparam func handler The callback function on receipt of command. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
-- @usage | ||
-- pubsub:on(command, function (params) | ||
-- return data, err | ||
|
@@ -180,40 +199,28 @@ function _M.wait(self) | |
-- command sequence code | ||
local sequence = data.sequence | ||
|
||
-- call command handler to generate response data | ||
for key, value in pairs(data) do | ||
-- There are sequence and command properties in the data, | ||
-- select the handler according to the command value. | ||
if key ~= "sequence" then | ||
local handler = self.cmd_handler[key] | ||
if not handler then | ||
log.error("pubsub callback handler not registered for the", | ||
" command, command: ", key) | ||
send_error(ws, sequence, "unknown command: " .. key) | ||
break | ||
end | ||
|
||
local resp, err = handler(value) | ||
if not resp then | ||
send_error(ws, sequence, err) | ||
break | ||
end | ||
|
||
-- write back the sequence | ||
resp.sequence = sequence | ||
local ok, data = pcall(pb.encode, "PubSubResp", resp) | ||
if not ok or not data then | ||
log.error("failed to encode response message, err: ", data) | ||
break | ||
end | ||
local _, err = ws:send_binary(data) | ||
if err then | ||
log.error("failed to send response to client, err: ", err) | ||
end | ||
break | ||
end | ||
local cmd, params = get_cmd(data) | ||
if not cmd and not params then | ||
log.warn("pubsub server receives empty command") | ||
goto continue | ||
end | ||
|
||
-- find the handler for the current command | ||
local handler = self.cmd_handler[cmd] | ||
if not handler then | ||
log.error("pubsub callback handler not registered for the", | ||
" command, command: ", cmd) | ||
send_error(ws, sequence, "unknown command") | ||
goto continue | ||
end | ||
|
||
-- call command handler to generate response data | ||
local resp, err = handler(params) | ||
if not resp then | ||
send_error(ws, sequence, err) | ||
goto continue | ||
end | ||
send_resp(ws, sequence, resp) | ||
|
||
::continue:: | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pubsub < node
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little confused, according to the principle you mentioned at #6995 (comment),
p
should be in the middle ofn
andr
in alphabetical order except forplugin
. lmnopqrst