From 8ee4f308a47ea613a97493b8afbe1aed02f80036 Mon Sep 17 00:00:00 2001 From: Ben Busby Date: Wed, 10 Nov 2021 12:19:37 -0700 Subject: Prevent same instance from being selected twice in a row Introduces a new db key "-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 "-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 "-previous" key. --- update.exs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'update.exs') diff --git a/update.exs b/update.exs index 7452e3d..e0c367a 100644 --- a/update.exs +++ b/update.exs @@ -1,5 +1,5 @@ defmodule Instances do - @fallback_str Application.fetch_env!(:farside, :fallback_str) + @fallback_suffix Application.fetch_env!(:farside, :fallback_suffix) @update_file Application.fetch_env!(:farside, :update_file) @services_json Application.fetch_env!(:farside, :services_json) @service_prefix Application.fetch_env!(:farside, :service_prefix) @@ -59,13 +59,13 @@ defmodule Instances do if Enum.count(instances) > 0 do Redix.command(:redix, [ "SET", - "#{service.type}#{@fallback_str}", + "#{service.type}#{@fallback_suffix}", Enum.random(instances) ]) else Redix.command(:redix, [ "SET", - "#{service.type}#{@fallback_str}", + "#{service.type}#{@fallback_suffix}", service.fallback ]) end -- cgit v1.2.3