英文:
How does this route to different nodes?
问题
我正在查看这个官方教程。我不明白它是如何路由到不同的节点的。看起来它只会再次调用route
函数并等待。
你能解释一下吗?
def route(bucket, mod, fun, args) do
# 获取二进制的第一个字节
first = :binary.first(bucket)
# 尝试在table()中查找条目,如果找不到则引发错误
entry =
Enum.find(table(), fn {enum, _node} ->
first in enum
end) || no_entry_error(bucket)
# 如果条目的节点是当前节点
if elem(entry, 1) == node() do
apply(mod, fun, args)
else
{KV.RouterTasks, elem(entry, 1)}
|> Task.Supervisor.async(KV.Router, :route, [bucket, mod, fun, args])
|> Task.await()
end
end
英文:
I am looking at this official tutorial. I don't understand how this will route to different nodes. It looks like it will simply invoke the route
function again and wait.
Can you please explain?
def route(bucket, mod, fun, args) do
# Get the first byte of the binary
first = :binary.first(bucket)
# Try to find an entry in the table() or raise
entry =
Enum.find(table(), fn {enum, _node} ->
first in enum
end) || no_entry_error(bucket)
# If the entry node is the current node
if elem(entry, 1) == node() do
apply(mod, fun, args)
else
{KV.RouterTasks, elem(entry, 1)}
|> Task.Supervisor.async(KV.Router, :route, [bucket, mod, fun, args])
|> Task.await()
end
end
答案1
得分: 0
Task.Supervisor.async/5
在第一个参数的第二个元素是 {atom(), node()}
元组时,在指定的节点上启动任务。
在上面的代码片段中,在最后一个 else
下的代码将 {KV.RouterTasks, elem(entry, 1)}
作为第一个参数传递,其中 elem(entry, 1)
是要在其上启动任务的节点。
英文:
Task.Supervisor.async/5
spawns the task on the node passed as a second element of the first argument when it’s an {atom(), node()}
tuple.
In the snippet above, the code under last else
is passing {KV.RouterTasks, elem(entry, 1)}
as the first argument, where elem(entry, 1)
is the node to spawn the task on.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论