summaryrefslogtreecommitdiff
path: root/lib/farside.ex
diff options
context:
space:
mode:
authorBen Busby <contact@benbusby.com>2022-03-18 13:51:37 -0600
committerBen Busby <contact@benbusby.com>2022-03-18 13:51:37 -0600
commit22e9135e0ce6d67fdfc3308171c3db321bf154f8 (patch)
tree89ef1f801057097a8606a056b40d9e85819532e8 /lib/farside.ex
parent20347822da52fdcff50924914d3a0928cffd27f3 (diff)
downloadfarside-22e9135e0ce6d67fdfc3308171c3db321bf154f8.tar.gz
Reuse previous instance within rate-limit interval
Rather than blocking <1s back-to-back queries from the same IP, Farside will now re-use the previously selected instance. Fixes #20
Diffstat (limited to 'lib/farside.ex')
-rw-r--r--lib/farside.ex19
1 files changed, 11 insertions, 8 deletions
diff --git a/lib/farside.ex b/lib/farside.ex
index ea574fa..16dc074 100644
--- a/lib/farside.ex
+++ b/lib/farside.ex
@@ -26,6 +26,15 @@ defmodule Farside do
end)
end
+ def last_instance(service) do
+ {:ok, previous} =
+ Redix.command(
+ :redix,
+ ["GET", "#{service}#{@previous_suffix}"]
+ )
+ previous
+ end
+
def pick_instance(service) do
{:ok, instances} =
Redix.command(
@@ -38,7 +47,7 @@ defmodule Farside do
]
)
- # Either pick a random available instance,
+ # Either pick a random available instance,
# or fall back to the default one
instance =
if Enum.count(instances) > 0 do
@@ -48,14 +57,8 @@ defmodule Farside do
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.filter(instances, &(&1 != last_instance(service)))
|> Enum.random()
Redix.command(