geolocation tweak
This commit is contained in:
parent
3d21e9bc77
commit
4b38c8f601
1 changed files with 2 additions and 27 deletions
|
|
@ -18,27 +18,8 @@ defmodule AprsmeWeb.Plugs.IPGeolocation do
|
||||||
if conn.request_path == "/" && conn.method == "GET" do
|
if conn.request_path == "/" && conn.method == "GET" do
|
||||||
case get_session(conn, :ip_geolocation) do
|
case get_session(conn, :ip_geolocation) do
|
||||||
nil ->
|
nil ->
|
||||||
# Check if we've had recent failures to prevent cascading timeouts
|
# No cached geolocation, fetch it
|
||||||
case get_session(conn, :ip_geolocation_failed_at) do
|
perform_geolocation(conn)
|
||||||
nil ->
|
|
||||||
# No recent failures, try geolocation
|
|
||||||
perform_geolocation(conn)
|
|
||||||
|
|
||||||
failed_at when is_integer(failed_at) ->
|
|
||||||
# Check if enough time has passed since last failure (5 minutes)
|
|
||||||
now = System.system_time(:second)
|
|
||||||
|
|
||||||
if now - failed_at > 300 do
|
|
||||||
# Try again
|
|
||||||
perform_geolocation(conn)
|
|
||||||
else
|
|
||||||
Logger.info("IP geolocation: Skipping due to recent failure")
|
|
||||||
conn
|
|
||||||
end
|
|
||||||
|
|
||||||
_ ->
|
|
||||||
perform_geolocation(conn)
|
|
||||||
end
|
|
||||||
|
|
||||||
_cached ->
|
_cached ->
|
||||||
# Already have geolocation in session
|
# Already have geolocation in session
|
||||||
|
|
@ -72,19 +53,13 @@ defmodule AprsmeWeb.Plugs.IPGeolocation do
|
||||||
{:ok, {:error, reason}} ->
|
{:ok, {:error, reason}} ->
|
||||||
duration = System.monotonic_time(:millisecond) - start_time
|
duration = System.monotonic_time(:millisecond) - start_time
|
||||||
Logger.warning("IP geolocation: Failed for #{ip} after #{duration}ms - #{inspect(reason)}")
|
Logger.warning("IP geolocation: Failed for #{ip} after #{duration}ms - #{inspect(reason)}")
|
||||||
# Mark the failure time to implement circuit breaker
|
|
||||||
conn
|
conn
|
||||||
|> put_session(:ip_geolocation_failed_at, System.system_time(:second))
|
|
||||||
|> delete_session(:ip_geolocation)
|
|
||||||
|
|
||||||
nil ->
|
nil ->
|
||||||
# Task timed out
|
# Task timed out
|
||||||
duration = System.monotonic_time(:millisecond) - start_time
|
duration = System.monotonic_time(:millisecond) - start_time
|
||||||
Logger.warning("IP geolocation: Task timeout for #{ip} after #{duration}ms")
|
Logger.warning("IP geolocation: Task timeout for #{ip} after #{duration}ms")
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_session(:ip_geolocation_failed_at, System.system_time(:second))
|
|
||||||
|> delete_session(:ip_geolocation)
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
Logger.info("IP geolocation: Skipping private/local IP #{ip}")
|
Logger.info("IP geolocation: Skipping private/local IP #{ip}")
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue