Add timeout to call_sync

Signed-off-by: Rahix <rahix@rahix.de>
dev
rahix 7 years ago
parent 253e04ff5d
commit 2b935b87e4

@ -146,7 +146,7 @@ bc:finalize()
bc:call("some_verb", "param_a_value", "param_b_value") bc:call("some_verb", "param_a_value", "param_b_value")
``` ```
### `BaseControl:call_sync(verb, timeout, ...)` ### `BaseControl:call_sync([timeout], verb, ...)`
Call a verb **synchroneously**. All parameters following `verb` will be given Call a verb **synchroneously**. All parameters following `verb` will be given
to the remote function. `call_sync` will return the remote function's return to the remote function. `call_sync` will return the remote function's return
value. If timeout is not `nil` or `0`, `call_sync` will error upon value. If timeout is not `nil` or `0`, `call_sync` will error upon
@ -161,7 +161,7 @@ end)
bc:finalize() bc:finalize()
-- This call can either happen locally or on another node -- This call can either happen locally or on another node
local res = bc:call_sync("stupid_add", nil, 12, 34) local res = bc:call_sync("stupid_add", 12, 34)
print("12 + 34 = "..res) print("12 + 34 = "..res)
``` ```

@ -352,14 +352,19 @@ function BaseControl:call(verb, ...)
end end
end end
function BaseControl:call_sync(verb, ...) function BaseControl:call_sync(timeout, verb, ...)
local param = {...}
if type(timeout) ~= "number" then
table.insert(param, 1, verb)
timeout, verb = nil, timeout
end
if self.local_verbs[verb] ~= nil then if self.local_verbs[verb] ~= nil then
return self.local_verbs[verb](...) return self.local_verbs[verb](table.unpack(param))
elseif self.remote_verbs[verb] ~= nil then elseif self.remote_verbs[verb] ~= nil then
self.network:send(self.remote_verbs[verb], { self.network:send(self.remote_verbs[verb], {
ty=Message.VerbRequest, ty=Message.VerbRequest,
verb=verb, verb=verb,
param={...}, param=param,
sync=true, sync=true,
}) })
local answer = self.network:pull(function(remote, msg) local answer = self.network:pull(function(remote, msg)

@ -43,7 +43,7 @@ function test_timeout_call()
) )
assert_error_match("timeout", function() assert_error_match("timeout", function()
bc:call_sync("toc1v", 0.1) bc:call_sync(0.1, "toc1v")
end) end)
end end

Loading…
Cancel
Save