Skip to content

Commit

Permalink
Merge branch 'bridge-mode-on-isnt-respected-when-bridge-location-does…
Browse files Browse the repository at this point in the history
…nt-des-768'
  • Loading branch information
MarkusPettersson98 committed Apr 8, 2024
2 parents dd47834 + 25e3350 commit dc6847c
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions mullvad-relay-selector/src/relay_selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,10 +958,8 @@ impl RelaySelector {
relays: Vec<Relay>,
location: T,
) -> Option<Relay> {
/// Minimum number of bridges to keep for selection when filtering by distance.
/// Number of bridges to keep for selection by distance.
const MIN_BRIDGE_COUNT: usize = 5;
/// Max distance of bridges to consider for selection (km).
const MAX_BRIDGE_DISTANCE: f64 = 1500f64;
let location = location.into();

#[derive(Clone)]
Expand All @@ -971,26 +969,25 @@ impl RelaySelector {
}

// Filter out all candidate bridges.
let matching_relays: Vec<RelayWithDistance> = relays
let matching_bridges: Vec<RelayWithDistance> = relays
.into_iter()
.map(|relay| RelayWithDistance {
distance: relay.location.as_ref().unwrap().distance_from(&location),
relay,
})
.sorted_unstable_by_key(|relay| relay.distance as usize)
.take(MIN_BRIDGE_COUNT)
.filter(|relay| relay.distance <= MAX_BRIDGE_DISTANCE)
.collect();

// Calculate the maximum distance from `location` among the candidates.
let greatest_distance: f64 = matching_relays
let greatest_distance: f64 = matching_bridges
.iter()
.map(|relay| relay.distance)
.reduce(f64::max)?;
// Define the weight function to prioritize bridges which are closer to `location`.
let weight_fn = |relay: &RelayWithDistance| 1 + (greatest_distance - relay.distance) as u64;

helpers::pick_random_relay_weighted(&matching_relays, weight_fn)
helpers::pick_random_relay_weighted(&matching_bridges, weight_fn)
.cloned()
.map(|relay_with_distance| relay_with_distance.relay)
}
Expand Down

0 comments on commit dc6847c

Please sign in to comment.