mirror of
https://github.com/benbusby/farside.git
synced 2025-04-20 10:58:42 +00:00
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.
This commit is contained in:
parent
d334fc7695
commit
71fb89e028
2 changed files with 32 additions and 28 deletions
|
@ -1,5 +1,6 @@
|
||||||
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)
|
||||||
|
|
||||||
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}*"])
|
||||||
|
@ -24,6 +25,36 @@ defmodule Farside do
|
||||||
end)
|
end)
|
||||||
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
|
def get_last_updated do
|
||||||
{:ok, last_updated} =
|
{:ok, last_updated} =
|
||||||
Redix.command(
|
Redix.command(
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
defmodule Farside.Router do
|
defmodule Farside.Router do
|
||||||
@index Application.fetch_env!(:farside, :index)
|
@index Application.fetch_env!(:farside, :index)
|
||||||
@fallback_str Application.fetch_env!(:farside, :fallback_str)
|
|
||||||
@service_prefix Application.fetch_env!(:farside, :service_prefix)
|
|
||||||
|
|
||||||
use Plug.Router
|
use Plug.Router
|
||||||
|
|
||||||
|
@ -27,32 +25,7 @@ defmodule Farside.Router do
|
||||||
|
|
||||||
get "/:service/*glob" do
|
get "/:service/*glob" do
|
||||||
path = Enum.join(glob, "/")
|
path = Enum.join(glob, "/")
|
||||||
|
instance = Farside.pick_instance(service)
|
||||||
{: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
|
|
||||||
|
|
||||||
# Redirect to the available instance
|
# Redirect to the available instance
|
||||||
conn
|
conn
|
||||||
|
|
Loading…
Add table
Reference in a new issue