这个句子的中文翻译是:“这是如何路由到不同节点的?”

huangapple go评论61阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月12日 06:32:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75709979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定