diff options
| author | Ben Busby <noreply+git@benbusby.com> | 2021-11-10 11:50:19 -0700 |
|---|---|---|
| committer | Ben Busby <noreply+git@benbusby.com> | 2021-11-10 11:50:19 -0700 |
| commit | 71fb89e02889ec64db3659ddf13740ac2fe22407 (patch) | |
| tree | 400456f625ff41b723f10b5f64a3609a75bd80bd /lib/farside.ex | |
| parent | d334fc76950b54eb641627d016b28a4824cf2657 (diff) | |
| download | farside-71fb89e02889ec64db3659ddf13740ac2fe22407.tar.gz | |
Move instance selection logic out of router
The process of selecting a random (working) instance for a specified
service has been moved out of the router and into lib/farside.ex. Moving
forward, the router itself should have very simple and easy to follow
logic for all paths.
Diffstat (limited to 'lib/farside.ex')
| -rw-r--r-- | lib/farside.ex | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/farside.ex b/lib/farside.ex index f87706a..0595e33 100644 --- a/lib/farside.ex +++ b/lib/farside.ex @@ -1,5 +1,6 @@ defmodule Farside do @service_prefix Application.fetch_env!(:farside, :service_prefix) + @fallback_str Application.fetch_env!(:farside, :fallback_str) def get_services_map do {:ok, service_list} = Redix.command(:redix, ["KEYS", "#{@service_prefix}*"]) @@ -24,6 +25,36 @@ defmodule Farside do end) end + def pick_instance(service) do + {:ok, instances} = + Redix.command( + :redix, + [ + "LRANGE", + "#{@service_prefix}#{service}", + "0", + "-1" + ] + ) + + # Either pick a random available instance, + # or fall back to the default one + instance = + if Enum.count(instances) > 0 do + Enum.random(instances) + else + {:ok, result} = + Redix.command( + :redix, + ["GET", "#{service}#{@fallback_str}"] + ) + + result + end + + instance + end + def get_last_updated do {:ok, last_updated} = Redix.command( |
