mirror of
https://github.com/benbusby/farside.git
synced 2025-04-20 02:48:42 +00:00
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.
This commit is contained in:
parent
71fb89e028
commit
8ee4f308a4
4 changed files with 45 additions and 8 deletions
|
@ -2,8 +2,9 @@ import Config
|
||||||
|
|
||||||
config :farside,
|
config :farside,
|
||||||
redis_conn: "redis://localhost:6379",
|
redis_conn: "redis://localhost:6379",
|
||||||
fallback_str: "-fallback",
|
|
||||||
update_file: ".update-results",
|
update_file: ".update-results",
|
||||||
service_prefix: "service-",
|
service_prefix: "service-",
|
||||||
|
fallback_suffix: "-fallback",
|
||||||
|
previous_suffix: "-previous",
|
||||||
services_json: "services.json",
|
services_json: "services.json",
|
||||||
index: "index.eex"
|
index: "index.eex"
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
defmodule Farside do
|
defmodule Farside do
|
||||||
@service_prefix Application.fetch_env!(:farside, :service_prefix)
|
@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
|
def get_services_map do
|
||||||
{:ok, service_list} = Redix.command(:redix, ["KEYS", "#{@service_prefix}*"])
|
{:ok, service_list} = Redix.command(:redix, ["KEYS", "#{@service_prefix}*"])
|
||||||
|
@ -41,12 +42,34 @@ defmodule Farside do
|
||||||
# or fall back to the default one
|
# or fall back to the default one
|
||||||
instance =
|
instance =
|
||||||
if Enum.count(instances) > 0 do
|
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
|
else
|
||||||
{:ok, result} =
|
{:ok, result} =
|
||||||
Redix.command(
|
Redix.command(
|
||||||
:redix,
|
:redix,
|
||||||
["GET", "#{service}#{@fallback_str}"]
|
["GET", "#{service}#{@fallback_suffix}"]
|
||||||
)
|
)
|
||||||
|
|
||||||
result
|
result
|
||||||
|
|
|
@ -42,15 +42,28 @@ defmodule FarsideTest do
|
||||||
IO.puts("")
|
IO.puts("")
|
||||||
|
|
||||||
Enum.map(service_names, fn service_name ->
|
Enum.map(service_names, fn service_name ->
|
||||||
IO.puts("/#{service_name}")
|
|
||||||
|
|
||||||
conn =
|
conn =
|
||||||
:get
|
:get
|
||||||
|> conn("/#{service_name}", "")
|
|> conn("/#{service_name}", "")
|
||||||
|> Router.call(@opts)
|
|> 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.state == :set
|
||||||
assert conn.status == 302
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
defmodule Instances do
|
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)
|
@update_file Application.fetch_env!(:farside, :update_file)
|
||||||
@services_json Application.fetch_env!(:farside, :services_json)
|
@services_json Application.fetch_env!(:farside, :services_json)
|
||||||
@service_prefix Application.fetch_env!(:farside, :service_prefix)
|
@service_prefix Application.fetch_env!(:farside, :service_prefix)
|
||||||
|
@ -59,13 +59,13 @@ defmodule Instances do
|
||||||
if Enum.count(instances) > 0 do
|
if Enum.count(instances) > 0 do
|
||||||
Redix.command(:redix, [
|
Redix.command(:redix, [
|
||||||
"SET",
|
"SET",
|
||||||
"#{service.type}#{@fallback_str}",
|
"#{service.type}#{@fallback_suffix}",
|
||||||
Enum.random(instances)
|
Enum.random(instances)
|
||||||
])
|
])
|
||||||
else
|
else
|
||||||
Redix.command(:redix, [
|
Redix.command(:redix, [
|
||||||
"SET",
|
"SET",
|
||||||
"#{service.type}#{@fallback_str}",
|
"#{service.type}#{@fallback_suffix}",
|
||||||
service.fallback
|
service.fallback
|
||||||
])
|
])
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue