Skip to content

Commit cecc5cd

Browse files
committed
Refactor away Daemon::app_version_info
1 parent 839eaf6 commit cecc5cd

File tree

2 files changed

+42
-50
lines changed

2 files changed

+42
-50
lines changed

mullvad-daemon/src/lib.rs

+13-26
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,6 @@ pub struct Daemon<L: EventListener> {
640640
relay_selector: RelaySelector,
641641
relay_list_updater: RelayListUpdaterHandle,
642642
parameters_generator: tunnel::ParametersGenerator,
643-
app_version_info: Option<AppVersionInfo>,
644643
shutdown_tasks: Vec<Pin<Box<dyn Future<Output = ()>>>>,
645644
tunnel_state_machine_handle: TunnelStateMachineHandle,
646645
#[cfg(target_os = "windows")]
@@ -862,7 +861,6 @@ where
862861
settings.show_beta_releases,
863862
)
864863
.await;
865-
let app_version_info = version_updater.last_app_version_info().cloned();
866864
tokio::spawn(version_updater.run());
867865

868866
// Attempt to download a fresh relay list
@@ -898,7 +896,6 @@ where
898896
relay_selector,
899897
relay_list_updater,
900898
parameters_generator,
901-
app_version_info,
902899
shutdown_tasks: vec![],
903900
tunnel_state_machine_handle,
904901
#[cfg(target_os = "windows")]
@@ -1311,7 +1308,6 @@ where
13111308
}
13121309

13131310
fn handle_new_app_version_info(&mut self, app_version_info: AppVersionInfo) {
1314-
self.app_version_info = Some(app_version_info.clone());
13151311
self.event_listener.notify_app_version(app_version_info);
13161312
}
13171313

@@ -1708,32 +1704,23 @@ where
17081704
}
17091705

17101706
fn on_get_version_info(&mut self, tx: oneshot::Sender<Option<AppVersionInfo>>) {
1711-
if self.app_version_info.is_none() {
1712-
log::debug!("No version cache found. Fetching new info");
1713-
let mut handle = self.version_updater_handle.clone();
1714-
tokio::spawn(async move {
1715-
Self::oneshot_send(
1716-
tx,
1717-
handle
1718-
.run_version_check()
1719-
.await
1720-
.map_err(|error| {
1721-
log::error!(
1722-
"{}",
1723-
error.display_chain_with_msg("Error running version check")
1724-
)
1725-
})
1726-
.ok(),
1727-
"get_version_info response",
1728-
);
1729-
});
1730-
} else {
1707+
let mut handle = self.version_updater_handle.clone();
1708+
tokio::spawn(async move {
17311709
Self::oneshot_send(
17321710
tx,
1733-
self.app_version_info.clone(),
1711+
handle
1712+
.get_version_info()
1713+
.await
1714+
.map_err(|error| {
1715+
log::error!(
1716+
"{}",
1717+
error.display_chain_with_msg("Error running version check")
1718+
)
1719+
})
1720+
.ok(),
17341721
"get_version_info response",
17351722
);
1736-
}
1723+
});
17371724
}
17381725

17391726
fn on_get_current_version(&mut self, tx: oneshot::Sender<AppVersion>) {

mullvad-daemon/src/version_check.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const DOWNLOAD_TIMEOUT: Duration = Duration::from_secs(15);
3535
const UPDATE_INTERVAL: Duration = Duration::from_secs(60 * 60 * 24);
3636
/// Wait this long until next try if an update failed
3737
const UPDATE_INTERVAL_ERROR: Duration = Duration::from_secs(60 * 60 * 6);
38-
/// Retry strategy for `RunVersionCheck`.
38+
/// Retry strategy for `GetVersionInfo`.
3939
const IMMEDIATE_RETRY_STRATEGY: ConstantInterval = ConstantInterval::new(Duration::ZERO, Some(3));
4040

4141
#[cfg(target_os = "linux")]
@@ -104,8 +104,8 @@ pub(crate) struct VersionUpdater {
104104
rx: Option<mpsc::Receiver<VersionUpdaterCommand>>,
105105
availability_handle: ApiAvailabilityHandle,
106106

107-
/// Oneshot channels for responding to [VersionUpdaterCommand::RunVersionCheck].
108-
run_version_check_responders: Vec<oneshot::Sender<AppVersionInfo>>,
107+
/// Oneshot channels for responding to [VersionUpdaterCommand::GetVersionInfo].
108+
get_version_info_responders: Vec<oneshot::Sender<AppVersionInfo>>,
109109
}
110110

111111
#[derive(Clone)]
@@ -115,7 +115,7 @@ pub(crate) struct VersionUpdaterHandle {
115115

116116
enum VersionUpdaterCommand {
117117
SetShowBetaReleases(bool),
118-
RunVersionCheck(oneshot::Sender<AppVersionInfo>),
118+
GetVersionInfo(oneshot::Sender<AppVersionInfo>),
119119
}
120120

121121
impl VersionUpdaterHandle {
@@ -132,11 +132,15 @@ impl VersionUpdaterHandle {
132132
}
133133
}
134134

135-
pub async fn run_version_check(&mut self) -> Result<AppVersionInfo, Error> {
135+
/// Get the latest cached [AppVersionInfo].
136+
///
137+
/// If the cache is stale or missing, this will immediately query the API for the latest
138+
/// version. This may take a few seconds.
139+
pub async fn get_version_info(&mut self) -> Result<AppVersionInfo, Error> {
136140
let (done_tx, done_rx) = oneshot::channel();
137141
if self
138142
.tx
139-
.send(VersionUpdaterCommand::RunVersionCheck(done_tx))
143+
.send(VersionUpdaterCommand::GetVersionInfo(done_tx))
140144
.await
141145
.is_err()
142146
{
@@ -174,7 +178,7 @@ impl VersionUpdater {
174178
show_beta_releases,
175179
rx: Some(rx),
176180
availability_handle,
177-
run_version_check_responders: vec![],
181+
get_version_info_responders: vec![],
178182
},
179183
VersionUpdaterHandle { tx },
180184
)
@@ -186,7 +190,7 @@ impl VersionUpdater {
186190
}
187191

188192
/// Immediately query the API for the latest [AppVersionInfo].
189-
fn query_app_version(
193+
fn do_version_check(
190194
&mut self,
191195
) -> Pin<
192196
Box<dyn Future<Output = Result<mullvad_api::AppVersionResponse, Error>> + Send + 'static>,
@@ -226,7 +230,7 @@ impl VersionUpdater {
226230
/// [ApiAvailability](mullvad_api::availability::ApiAvailability).
227231
///
228232
/// On any error, this function retries repeatedly every [UPDATE_INTERVAL_ERROR] until success.
229-
fn query_app_version_in_background(
233+
fn do_version_check_in_background(
230234
&self,
231235
) -> Pin<
232236
Box<dyn Future<Output = Result<mullvad_api::AppVersionResponse, Error>> + Send + 'static>,
@@ -328,9 +332,9 @@ impl VersionUpdater {
328332

329333
/// Update [Self::last_app_version_info] and write it to disk cache.
330334
///
331-
/// Also, if we are currently have a pending [RunVersionCheck][rvc] command, respond to it.
335+
/// Also, if we are currently have a pending [GetVersionInfo][rvc] command, respond to it.
332336
///
333-
/// [rvc]: VersionUpdaterCommand::RunVersionCheck
337+
/// [rvc]: VersionUpdaterCommand::GetVersionInfo
334338
async fn update_version_info(&mut self, new_version_info: AppVersionInfo) {
335339
// if daemon can't be reached, return immediately
336340
if self.update_sender.send(new_version_info.clone()).is_err() {
@@ -346,7 +350,7 @@ impl VersionUpdater {
346350
/// Get the time left until [Self::last_app_version_info] becomes stale, and should be
347351
/// refreshed, or [Duration::ZERO] if it already is stale.
348352
///
349-
/// This happens [UPDATE_INTERVAL] after the last version query.
353+
/// This happens [UPDATE_INTERVAL] after the last version check.
350354
fn time_until_version_is_stale(&self) -> Duration {
351355
let now = SystemTime::now();
352356
self
@@ -360,13 +364,14 @@ impl VersionUpdater {
360364
.unwrap_or(Duration::ZERO)
361365
}
362366

367+
/// Is [Self::last_app_version_info] stale?
363368
fn version_is_stale(&self) -> bool {
364369
self.time_until_version_is_stale().is_zero()
365370
}
366371

367372
/// Wait until [Self::last_app_version_info] becomes stale and needs to be refreshed.
368373
///
369-
/// This happens [UPDATE_INTERVAL] after the last version query.
374+
/// This happens [UPDATE_INTERVAL] after the last version check.
370375
fn wait_until_version_is_stale(&self) -> Pin<Box<impl FusedFuture<Output = ()>>> {
371376
let time_until_stale = self.time_until_version_is_stale();
372377

@@ -375,21 +380,21 @@ impl VersionUpdater {
375380
Box::pin(talpid_time::sleep(time_until_stale).fuse())
376381
}
377382

378-
/// Returns true if we are currently handling one or more `RunVersionCheck` commands.
383+
/// Returns true if we are currently handling one or more `GetVersionInfo` commands.
379384
fn is_running_version_check(&self) -> bool {
380-
!self.run_version_check_responders.is_empty()
385+
!self.get_version_info_responders.is_empty()
381386
}
382387

383388
pub async fn run(mut self) {
384-
let mut rx = self.rx.take().unwrap().fuse();
389+
let mut rx = self.rx.take().unwrap();
385390
let mut version_is_stale = self.wait_until_version_is_stale();
386391
let mut version_check = futures::future::Fuse::terminated();
387392

388393
// If this is a dev build, there's no need to pester the API for version checks.
389394
if *IS_DEV_BUILD {
390395
log::warn!("Not checking for updates because this is a development build");
391396
while let Some(cmd) = rx.next().await {
392-
if let VersionUpdaterCommand::RunVersionCheck(done_tx) = cmd {
397+
if let VersionUpdaterCommand::GetVersionInfo(done_tx) = cmd {
393398
log::info!("Version check is disabled in dev builds");
394399
let _ = done_tx.send(dev_version_cache());
395400
}
@@ -423,7 +428,7 @@ impl VersionUpdater {
423428
}
424429
}
425430

426-
Some(VersionUpdaterCommand::RunVersionCheck(done_tx)) => {
431+
Some(VersionUpdaterCommand::GetVersionInfo(done_tx)) => {
427432
if self.update_sender.is_closed() {
428433
return;
429434
}
@@ -435,9 +440,9 @@ impl VersionUpdater {
435440
_ => {
436441
// otherwise, start a foreground query to get the latest version_info.
437442
if !self.is_running_version_check() {
438-
version_check = self.query_app_version().fuse();
443+
version_check = self.do_version_check().fuse();
439444
}
440-
self.run_version_check_responders.push(done_tx);
445+
self.get_version_info_responders.push(done_tx);
441446
}
442447
}
443448
}
@@ -455,7 +460,7 @@ impl VersionUpdater {
455460
if self.is_running_version_check() {
456461
continue;
457462
}
458-
version_check = self.query_app_version_in_background().fuse();
463+
version_check = self.do_version_check_in_background().fuse();
459464
},
460465

461466
response = version_check => {
@@ -468,8 +473,8 @@ impl VersionUpdater {
468473
let new_version_info =
469474
self.response_to_version_info(version_info_response);
470475

471-
// Respond to all pending RunVersionCheck commands
472-
for done_tx in self.run_version_check_responders.drain(..) {
476+
// Respond to all pending GetVersionInfo commands
477+
for done_tx in self.get_version_info_responders.drain(..) {
473478
let _ = done_tx.send(new_version_info.clone());
474479
}
475480

@@ -478,7 +483,7 @@ impl VersionUpdater {
478483
}
479484
Err(err) => {
480485
log::error!("Failed to fetch version info: {err:#}");
481-
self.run_version_check_responders.clear();
486+
self.get_version_info_responders.clear();
482487
}
483488
}
484489

0 commit comments

Comments
 (0)