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. --- test/farside_test.exs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'test') diff --git a/test/farside_test.exs b/test/farside_test.exs index 6347691..786c382 100644 --- a/test/farside_test.exs +++ b/test/farside_test.exs @@ -42,15 +42,28 @@ defmodule FarsideTest do IO.puts("") Enum.map(service_names, fn service_name -> - IO.puts("/#{service_name}") conn = :get |> conn("/#{service_name}", "") |> Router.call(@opts) + first_redirect = elem(List.last(conn.resp_headers), 1) + IO.puts(" /#{service_name} (#1) -- #{first_redirect}") assert conn.state == :set assert conn.status == 302 + + + conn = + :get + |> conn("/#{service_name}", "") + |> Router.call(@opts) + + second_redirect = elem(List.last(conn.resp_headers), 1) + IO.puts(" /#{service_name} (#2) -- #{second_redirect}") + assert conn.state == :set + assert conn.status == 302 + assert first_redirect != second_redirect end) end end -- cgit v1.2.3