mirror of
https://github.com/benbusby/farside.git
synced 2025-03-15 11:46:32 -04:00

Introduces a new db key "<service>-previous" to track which instance was last selected for a particular service. This allows for filtering the list of available instances to exclude the instance that was last picked, to ensure a (slightly) more even distribution of traffic. There's still the possiblity of the following scenario, however: :service instances > 2 /:service request #1 -> instance #1 /:service request #2 -> instance #2 /:service request #3 -> instance #1 /:service request #4 -> instance #2 where there are many ignored instances for a particular service. One possible solution would be to implement the "<service>-previous" value to be a list, rather than a single value, and push to that list until only one element is left in the original "instance" array after filtering, and then delete the "<service>-previous" key.
91 lines
2.1 KiB
Elixir
91 lines
2.1 KiB
Elixir
defmodule Farside do
|
|
@service_prefix Application.fetch_env!(:farside, :service_prefix)
|
|
@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}*"])
|
|
|
|
# Match service name to list of available instances
|
|
Enum.reduce(service_list, %{}, fn service, acc ->
|
|
{:ok, instance_list} =
|
|
Redix.command(
|
|
:redix,
|
|
["LRANGE", service, "0", "-1"]
|
|
)
|
|
|
|
Map.put(
|
|
acc,
|
|
String.replace_prefix(
|
|
service,
|
|
@service_prefix,
|
|
""
|
|
),
|
|
instance_list
|
|
)
|
|
end)
|
|
end
|
|
|
|
def pick_instance(service) do
|
|
{:ok, instances} =
|
|
Redix.command(
|
|
:redix,
|
|
[
|
|
"LRANGE",
|
|
"#{@service_prefix}#{service}",
|
|
"0",
|
|
"-1"
|
|
]
|
|
)
|
|
|
|
# Either pick a random available instance,
|
|
# or fall back to the default one
|
|
instance =
|
|
if Enum.count(instances) > 0 do
|
|
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_suffix}"]
|
|
)
|
|
|
|
result
|
|
end
|
|
|
|
instance
|
|
end
|
|
|
|
def get_last_updated do
|
|
{:ok, last_updated} =
|
|
Redix.command(
|
|
:redix,
|
|
["GET", "last_updated"]
|
|
)
|
|
|
|
last_updated
|
|
end
|
|
end
|