chore(portal): Fix flaky test and relays load balancing selection (#4230)

A few gifts for @thomaseizinger 

Unblocks
https://github.com/firezone/firezone/pull/4213#issuecomment-2007187811
Closes #3838
This commit is contained in:
Andrew Dryga
2024-03-20 16:33:47 -06:00
committed by GitHub
parent 8195ac1893
commit 29cc0096a8
4 changed files with 23 additions and 18 deletions

View File

@@ -38,13 +38,11 @@ defmodule Domain.Auth.Identity.Sync do
:actor_ids_by_provider_identifier,
fn _repo,
%{
plan_identities: {_insert, _update, delete},
identities: identities,
update_identities_and_actors: update_identities,
insert_identities: insert_identities
} ->
actor_ids_by_provider_identifier =
for identity <- identities ++ insert_identities,
identity.provider_identifier not in delete,
for identity <- update_identities ++ insert_identities,
into: %{} do
{identity.provider_identifier, identity.actor_id}
end
@@ -140,10 +138,22 @@ defmodule Domain.Auth.Identity.Sync do
|> Enum.filter(fn identity ->
identity.provider_identifier in provider_identifiers_to_update
end)
# make sure that deleted identities are in the end in case of conflicts
|> Enum.sort_by(& &1.deleted_at, :desc)
|> repo.preload(:actor)
|> Map.new(&{&1.provider_identifier, &1})
|> Enum.reduce(%{}, fn identity, acc ->
acc_identity = Map.get(acc, identity.provider_identifier)
# make sure that deleted identities are have the least priority in case of conflicts
cond do
is_nil(acc_identity) ->
Map.put(acc, identity.provider_identifier, identity)
is_nil(acc_identity.deleted_at) ->
acc
true ->
Map.put(acc, identity.provider_identifier, identity)
end
end)
provider_identifiers_to_update
|> Enum.uniq()

View File

@@ -339,7 +339,7 @@ defmodule Domain.Gateways do
# Replace the location with the approximate distance to the client
|> Enum.map(fn
{{gateway_lat, gateway_lon}, gateway} when is_nil(gateway_lat) or is_nil(gateway_lon) ->
{Geo.fetch_radius_of_earth_km!(), gateway}
{nil, gateway}
{{gateway_lat, gateway_lon}, gateway} ->
distance = Geo.distance({lat, lon}, {gateway_lat, gateway_lon})

View File

@@ -1,10 +1,6 @@
defmodule Domain.Geo do
@radius_of_earth_km 6371.0
# Since most of our customers are from US we use
# geographic center of USA as default coordinates
@default_coordinates {39.8283, -98.5795}
@coordinates_by_code %{
"AF" => {33, 65},
"AX" => {60.116667, 19.9},
@@ -259,10 +255,6 @@ defmodule Domain.Geo do
|> Enum.map(fn {code, {lat, lon}} -> {code, {lat * 1.0, lon * 1.0}} end)
|> Map.new()
def fetch_radius_of_earth_km! do
@radius_of_earth_km
end
def distance({lat1, lon1}, {lat2, lon2}) do
d_lat = degrees_to_radians(lat2 - lat1)
d_lon = degrees_to_radians(lon2 - lon1)
@@ -282,7 +274,10 @@ defmodule Domain.Geo do
end
def maybe_put_default_coordinates(country_code, {nil, nil}) do
Map.get(@coordinates_by_code, country_code, @default_coordinates)
case Map.get(@coordinates_by_code, country_code) do
{lat, lon} -> {lat, lon}
nil -> {nil, nil}
end
end
def maybe_put_default_coordinates(_country_code, {lat, lon}) do

View File

@@ -322,7 +322,7 @@ defmodule Domain.Relays do
end)
|> Enum.map(fn
{{nil, nil}, relay} ->
{Geo.fetch_radius_of_earth_km!(), relay}
{nil, relay}
{{relay_lat, relay_lon}, relay} ->
distance = Geo.distance({lat, lon}, {relay_lat, relay_lon})