Skip to content

Commit e204cc7

Browse files
Merge branch 'remove-mullvad-relay-selector-dependencies'
2 parents bde5c5d + 16831a2 commit e204cc7

File tree

7 files changed

+191
-136
lines changed

7 files changed

+191
-136
lines changed

Cargo.lock

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mullvad-daemon/src/lib.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod macos;
1717
#[cfg(not(target_os = "android"))]
1818
pub mod management_interface;
1919
mod migrations;
20+
mod relay_list;
2021
#[cfg(not(target_os = "android"))]
2122
pub mod rpc_uniqueness_check;
2223
pub mod runtime;
@@ -36,10 +37,7 @@ use futures::{
3637
StreamExt,
3738
};
3839
use geoip::GeoIpHandler;
39-
use mullvad_relay_selector::{
40-
updater::{RelayListUpdater, RelayListUpdaterHandle},
41-
RelaySelector, SelectorConfig,
42-
};
40+
use mullvad_relay_selector::{RelaySelector, SelectorConfig};
4341
#[cfg(target_os = "android")]
4442
use mullvad_types::account::{PlayPurchase, PlayPurchasePaymentToken};
4543
use mullvad_types::{
@@ -58,6 +56,7 @@ use mullvad_types::{
5856
version::{AppVersion, AppVersionInfo},
5957
wireguard::{PublicKey, QuantumResistantState, RotationInterval},
6058
};
59+
use relay_list::updater::{self, RelayListUpdater, RelayListUpdaterHandle};
6160
use settings::SettingsPersister;
6261
#[cfg(target_os = "android")]
6362
use std::os::unix::io::RawFd;
@@ -698,7 +697,11 @@ where
698697
let app_version_info = version_check::load_cache(&cache_dir).await;
699698

700699
let initial_selector_config = new_selector_config(&settings);
701-
let relay_selector = RelaySelector::new(initial_selector_config, &resource_dir, &cache_dir);
700+
let relay_selector = RelaySelector::new(
701+
initial_selector_config,
702+
resource_dir.join(updater::RELAYS_FILENAME),
703+
cache_dir.join(updater::RELAYS_FILENAME),
704+
);
702705

703706
let settings_relay_selector = relay_selector.clone();
704707
settings.register_change_listener(move |settings| {
@@ -1569,7 +1572,7 @@ where
15691572
}
15701573

15711574
fn on_get_relay_locations(&mut self, tx: oneshot::Sender<RelayList>) {
1572-
Self::oneshot_send(tx, self.relay_selector.get_locations(), "relay locations");
1575+
Self::oneshot_send(tx, self.relay_selector.get_relays(), "relay locations");
15731576
}
15741577

15751578
async fn on_update_relay_locations(&mut self) {

mullvad-daemon/src/relay_list/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! Relay list
2+
3+
pub mod updater;

mullvad-relay-selector/src/updater.rs mullvad-daemon/src/relay_list/updater.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
use super::{Error, ParsedRelays};
21
use futures::{
32
channel::mpsc,
43
future::{Fuse, FusedFuture},
54
Future, FutureExt, SinkExt, StreamExt,
65
};
7-
use mullvad_api::{availability::ApiAvailabilityHandle, rest::MullvadRestHandle, RelayListProxy};
8-
use mullvad_types::relay_list::RelayList;
9-
use parking_lot::Mutex;
106
use std::{
117
path::{Path, PathBuf},
12-
sync::Arc,
138
time::{Duration, SystemTime, UNIX_EPOCH},
149
};
10+
use tokio::fs::File;
11+
12+
use mullvad_api::{availability::ApiAvailabilityHandle, rest::MullvadRestHandle, RelayListProxy};
13+
use mullvad_relay_selector::{Error, RelaySelector};
14+
use mullvad_types::relay_list::RelayList;
1515
use talpid_future::retry::{retry_future, ExponentialBackoff, Jittered};
1616
use talpid_types::ErrorExt;
17-
use tokio::fs::File;
1817

1918
/// How often the updater should wake up to check the cache of the in-memory cache of relays.
2019
/// This check is very cheap. The only reason to not have it very often is because if downloading
@@ -28,6 +27,9 @@ const DOWNLOAD_RETRY_STRATEGY: Jittered<ExponentialBackoff> = Jittered::jitter(
2827
.max_delay(Some(Duration::from_secs(2 * 60 * 60))),
2928
);
3029

30+
/// Where the relay list is cached on disk.
31+
pub(crate) const RELAYS_FILENAME: &str = "relays.json";
32+
3133
#[derive(Clone)]
3234
pub struct RelayListUpdaterHandle {
3335
tx: mpsc::Sender<()>,
@@ -52,15 +54,15 @@ impl RelayListUpdaterHandle {
5254
pub struct RelayListUpdater {
5355
api_client: RelayListProxy,
5456
cache_path: PathBuf,
55-
parsed_relays: Arc<Mutex<ParsedRelays>>,
57+
relay_selector: RelaySelector,
5658
on_update: Box<dyn Fn(&RelayList) + Send + 'static>,
5759
last_check: SystemTime,
5860
api_availability: ApiAvailabilityHandle,
5961
}
6062

6163
impl RelayListUpdater {
6264
pub fn spawn(
63-
selector: super::RelaySelector,
65+
selector: RelaySelector,
6466
api_handle: MullvadRestHandle,
6567
cache_dir: &Path,
6668
on_update: impl Fn(&RelayList) + Send + 'static,
@@ -70,8 +72,8 @@ impl RelayListUpdater {
7072
let api_client = RelayListProxy::new(api_handle);
7173
let updater = RelayListUpdater {
7274
api_client,
73-
cache_path: cache_dir.join(super::RELAYS_FILENAME),
74-
parsed_relays: selector.parsed_relays,
75+
cache_path: cache_dir.join(RELAYS_FILENAME),
76+
relay_selector: selector,
7577
on_update: Box::new(on_update),
7678
last_check: UNIX_EPOCH,
7779
api_availability,
@@ -91,7 +93,7 @@ impl RelayListUpdater {
9193
futures::select! {
9294
_check_update = next_check => {
9395
if download_future.is_terminated() && self.should_update() {
94-
let tag = self.parsed_relays.lock().parsed_list.etag.clone();
96+
let tag = self.relay_selector.etag();
9597
download_future = Box::pin(Self::download_relay_list(self.api_availability.clone(), self.api_client.clone(), tag).fuse());
9698
self.last_check = SystemTime::now();
9799
}
@@ -104,7 +106,7 @@ impl RelayListUpdater {
104106
cmd = cmd_rx.next() => {
105107
match cmd {
106108
Some(()) => {
107-
let tag = self.parsed_relays.lock().parsed_list.etag.clone();
109+
let tag = self.relay_selector.etag();
108110
download_future = Box::pin(Self::download_relay_list(self.api_availability.clone(), self.api_client.clone(), tag).fuse());
109111
self.last_check = SystemTime::now();
110112
},
@@ -139,7 +141,7 @@ impl RelayListUpdater {
139141

140142
/// Returns true if the current parsed_relays is older than UPDATE_INTERVAL
141143
fn should_update(&mut self) -> bool {
142-
let last_check = std::cmp::max(self.parsed_relays.lock().last_updated(), self.last_check);
144+
let last_check = std::cmp::max(self.relay_selector.last_updated(), self.last_check);
143145
match SystemTime::now().duration_since(last_check) {
144146
Ok(duration) => duration >= UPDATE_INTERVAL,
145147
// If the clock is skewed we have no idea by how much or when the last update
@@ -178,9 +180,8 @@ impl RelayListUpdater {
178180
);
179181
}
180182

181-
let mut parsed_relays = self.parsed_relays.lock();
182-
parsed_relays.update(new_relay_list);
183-
(self.on_update)(&parsed_relays.original_list);
183+
self.relay_selector.set_relays(new_relay_list.clone());
184+
(self.on_update)(&new_relay_list);
184185
Ok(())
185186
}
186187

mullvad-relay-selector/Cargo.toml

-5
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ workspace = true
1313
[dependencies]
1414
chrono = { workspace = true }
1515
err-derive = { workspace = true }
16-
futures = "0.3"
1716
ipnetwork = "0.16"
1817
log = { workspace = true }
19-
parking_lot = "0.12.0"
2018
rand = "0.8.5"
2119
serde_json = "1.0"
22-
tokio = { workspace = true, features = ["fs", "io-util", "time"] }
2320

24-
talpid-future = { path = "../talpid-future" }
2521
talpid-types = { path = "../talpid-types" }
26-
mullvad-api = { path = "../mullvad-api" }
2722
mullvad-types = { path = "../mullvad-types" }
2823

2924
[dev-dependencies]

0 commit comments

Comments
 (0)