Skip to content

Commit

Permalink
refactor(clustreing/rpc): constant for json rpc version (#13902)
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw authored Nov 29, 2024
1 parent eff5e8f commit 78577cc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
8 changes: 4 additions & 4 deletions kong/clustering/rpc/concentrator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ function _M:_event_loop(lconn)
if n.channel == rpc_resp_channel_name then
-- an response for a previous RPC call we asked for
local payload = cjson_decode(n.payload)
assert(payload.jsonrpc == "2.0")
assert(payload.jsonrpc == jsonrpc.VERSION)

-- response
local cb = self.interest[payload.id]
Expand Down Expand Up @@ -158,7 +158,7 @@ function _M:_event_loop(lconn)
if res then
-- call success
res, err = self:_enqueue_rpc_response(reply_to, {
jsonrpc = "2.0",
jsonrpc = jsonrpc.VERSION,
id = payload.id,
result = res,
})
Expand All @@ -169,7 +169,7 @@ function _M:_event_loop(lconn)
else
-- call failure
res, err = self:_enqueue_rpc_response(reply_to, {
jsonrpc = "2.0",
jsonrpc = jsonrpc.VERSION,
id = payload.id,
error = {
code = jsonrpc.SERVER_ERROR,
Expand Down Expand Up @@ -292,7 +292,7 @@ function _M:call(node_id, method, params, callback)
self.interest[id] = callback

return self:_enqueue_rpc_request(node_id, {
jsonrpc = "2.0",
jsonrpc = jsonrpc.VERSION,
method = method,
params = params,
id = id,
Expand Down
3 changes: 2 additions & 1 deletion kong/clustering/rpc/future.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local _MT = { __index = _M, }


local semaphore = require("ngx.semaphore")
local jsonrpc = require("kong.clustering.rpc.json_rpc_v2")


local STATE_NEW = 1
Expand Down Expand Up @@ -34,7 +35,7 @@ function _M:start()
self.state = STATE_IN_PROGRESS

local callback = function(resp)
assert(resp.jsonrpc == "2.0")
assert(resp.jsonrpc == jsonrpc.VERSION)

if resp.result then
-- succeeded
Expand Down
4 changes: 3 additions & 1 deletion kong/clustering/rpc/json_rpc_v2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ local _M = {
INVALID_PARAMS = -32602,
INTERNAL_ERROR = -32603,
SERVER_ERROR = -32000,

VERSION = "2.0",
}


Expand Down Expand Up @@ -40,7 +42,7 @@ function _M.new_error(id, code, msg)
end

return {
jsonrpc = "2.0",
jsonrpc = _M.VERSION,
id = id,
error = {
code = code,
Expand Down
9 changes: 5 additions & 4 deletions kong/clustering/rpc/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local client = require("resty.websocket.client")
local socket = require("kong.clustering.rpc.socket")
local future = require("kong.clustering.rpc.future")
local utils = require("kong.clustering.rpc.utils")
local jsonrpc = require("kong.clustering.rpc.json_rpc_v2")
local callbacks = require("kong.clustering.rpc.callbacks")
local clustering_tls = require("kong.clustering.tls")
local constants = require("kong.constants")
Expand Down Expand Up @@ -154,7 +155,7 @@ function _M:_handle_meta_call(c)
end

local payload = cjson_decode(data)
assert(payload.jsonrpc == "2.0")
assert(payload.jsonrpc == jsonrpc.VERSION)

if payload.method ~= RPC_MATA_V1 .. ".hello" then
return nil, "wrong RPC meta call: " .. tostring(payload.method)
Expand All @@ -181,7 +182,7 @@ function _M:_handle_meta_call(c)
assert(type(info.kong_conf) == "table")

local payload = {
jsonrpc = "2.0",
jsonrpc = jsonrpc.VERSION,
result = {
rpc_capabilities = self.callbacks:get_capabilities_list(),
-- now we only support snappy
Expand Down Expand Up @@ -232,7 +233,7 @@ function _M:_meta_call(c, meta_cap, node_id)
}

local payload = {
jsonrpc = "2.0",
jsonrpc = jsonrpc.VERSION,
method = meta_cap .. ".hello",
params = { info },
id = 1,
Expand All @@ -253,7 +254,7 @@ function _M:_meta_call(c, meta_cap, node_id)
end

local payload = cjson_decode(data)
assert(payload.jsonrpc == "2.0")
assert(payload.jsonrpc == jsonrpc.VERSION)

-- now we only support snappy
if payload.result.rpc_frame_encoding ~= RPC_SNAPPY_FRAMED then
Expand Down
6 changes: 3 additions & 3 deletions kong/clustering/rpc/socket.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function _M._dispatch(premature, self, cb, payload)

-- success
res, err = self.outgoing:push({
jsonrpc = "2.0",
jsonrpc = jsonrpc.VERSION,
id = payload.id,
result = res,
})
Expand Down Expand Up @@ -143,7 +143,7 @@ function _M:start()
assert(typ == "binary")

local payload = decompress_payload(data)
assert(payload.jsonrpc == "2.0")
assert(payload.jsonrpc == jsonrpc.VERSION)

if payload.method then
-- invoke
Expand Down Expand Up @@ -276,7 +276,7 @@ function _M:call(node_id, method, params, callback)
self.interest[id] = callback

return self.outgoing:push({
jsonrpc = "2.0",
jsonrpc = jsonrpc.VERSION,
method = method,
params = params,
id = id,
Expand Down

1 comment on commit 78577cc

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:78577cca8c717e7561e59385e23ec979f0de3ec8
Artifacts available https://github.com/Kong/kong/actions/runs/12080310158

Please sign in to comment.