Skip to content

Commit

Permalink
future
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Dec 3, 2024
1 parent 4410d9f commit 667675e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions kong/clustering/rpc/future.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,35 @@ function _M.new(node_id, socket, method, params, is_notification)
local self = {
method = method,
params = params,
sema = semaphore.new(),
socket = socket,
node_id = node_id,
id = nil,
result = nil,
error = nil,
state = STATE_NEW, -- STATE_*
is_notification = is_notification,
}

if not is_notification then
self.id = nil
self.result = nil
self.error = nil
self.state = STATE_NEW -- STATE_*
self.sema = semaphore.new()
end

return setmetatable(self, _MT)
end


-- start executing the future
function _M:start()
assert(self.state == STATE_NEW)
self.state = STATE_IN_PROGRESS

-- notification has no callback
if self.is_notification then
self.state = STATE_SUCCEED

return self.socket:call(self.node_id,
self.method,
self.params)
end

assert(self.state == STATE_NEW)
self.state = STATE_IN_PROGRESS

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

Expand All @@ -70,6 +71,7 @@ end


function _M:wait(timeout)
assert(not self.is_notification)
assert(self.state == STATE_IN_PROGRESS)

local res, err = self.sema:wait(timeout)
Expand Down

0 comments on commit 667675e

Please sign in to comment.