diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/farside.ex | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/lib/farside.ex b/lib/farside.ex index 0595e33..ea574fa 100644 --- a/lib/farside.ex +++ b/lib/farside.ex @@ -1,6 +1,7 @@ defmodule Farside do @service_prefix Application.fetch_env!(:farside, :service_prefix) - @fallback_str Application.fetch_env!(:farside, :fallback_str) + @fallback_suffix Application.fetch_env!(:farside, :fallback_suffix) + @previous_suffix Application.fetch_env!(:farside, :previous_suffix) def get_services_map do {:ok, service_list} = Redix.command(:redix, ["KEYS", "#{@service_prefix}*"]) @@ -41,12 +42,34 @@ defmodule Farside do # or fall back to the default one instance = if Enum.count(instances) > 0 do - Enum.random(instances) + if Enum.count(instances) == 1 do + # If there's only one instance, just return that one... + List.first(instances) + else + # ...otherwise pick a random one from the list, ensuring + # that the same instance is never picked twice in a row. + {:ok, previous} = + Redix.command( + :redix, + ["GET", "#{service}#{@previous_suffix}"] + ) + + instance = + Enum.filter(instances, &(&1 != previous)) + |> Enum.random() + + Redix.command( + :redix, + ["SET", "#{service}#{@previous_suffix}", instance] + ) + + instance + end else {:ok, result} = Redix.command( :redix, - ["GET", "#{service}#{@fallback_str}"] + ["GET", "#{service}#{@fallback_suffix}"] ) result |
