bitwalker / swarm

Easy clustering, registration, and distribution of worker processes for Erlang/Elixir
MIT License
1.21k stars 103 forks source link

Violation of the Swarm.Distribution.Strategy definition is actually accepted #140

Open balena opened 4 years ago

balena commented 4 years ago

https://github.com/bitwalker/swarm/blob/4aee63d83ad5ee6ee095b38b3ff93a4dbb7c3400/lib/swarm/tracker/tracker.ex#L1187

By definition, Swarm.Distribution.Strategy.key_to_node/2 can return node() | :undefined. However this exact line is actually accepting {:error, {:invalid_ring, :no_nodes}} as a valid return value, which is actually what HashRing.key_to_node/2 returns in case of error.

So Swarm.Distribution.Ring.key_to_node/2 should actually return :undefined instead of {:error, reason} to honour the strategy definition:

defmodule Swarm.Distribution.Ring do
  ...
  def key_to_node(ring, key) do
    case HashRing.key_to_node(ring, key) do
      {:error, _reason} -> :undefined
      node -> node
    end
  end
end