summaryrefslogtreecommitdiff
path: root/lib/farside.ex
diff options
context:
space:
mode:
authorBen Busby <noreply+git@benbusby.com>2021-11-10 12:19:37 -0700
committerBen Busby <noreply+git@benbusby.com>2021-11-10 12:19:37 -0700
commit8ee4f308a47ea613a97493b8afbe1aed02f80036 (patch)
treecbbd9c7a47ac807931e164f04a87bd55552d8a33 /lib/farside.ex
parent71fb89e02889ec64db3659ddf13740ac2fe22407 (diff)
downloadfarside-8ee4f308a47ea613a97493b8afbe1aed02f80036.tar.gz
Prevent same instance from being selected twice in a row
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.
Diffstat (limited to 'lib/farside.ex')
-rw-r--r--lib/farside.ex29
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