Skip to content

Commit

Permalink
Get rid of SpawnResult
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusPettersson98 committed Dec 20, 2023
1 parent 49b6107 commit 3767424
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
18 changes: 3 additions & 15 deletions mullvad-daemon/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,21 +182,14 @@ pub struct AccessModeSelector {
current: ResolvedConnectionMode,
}

// TODO(markus): Document this! It was created to get an initial api endpoint in
// a more straight-forward way.
pub(crate) struct SpawnResult {
pub handle: AccessModeSelectorHandle,
pub initial_api_endpoint: AllowedEndpoint,
}

impl AccessModeSelector {
pub(crate) async fn spawn(
cache_dir: PathBuf,
relay_selector: RelaySelector,
connection_modes: Vec<AccessMethodSetting>,
listener: impl Sender<NewAccessMethodEvent> + Send + 'static,
address_cache: AddressCache,
) -> SpawnResult {
) -> Result<AccessModeSelectorHandle> {
let (cmd_tx, cmd_rx) = mpsc::unbounded();

let mut connection_modes = match ConnectionModesIterator::new(connection_modes) {
Expand All @@ -212,11 +205,9 @@ impl AccessModeSelector {
};

let initial_connection_mode = {
// TODO(markus): Unwrap? :no-thanks:
let next = connection_modes.next().unwrap();
let next = connection_modes.next().ok_or(Error::NoAccessMethods)?;
Self::resolve_internal(next, &relay_selector, &address_cache).await
};
let initial_api_endpoint = initial_connection_mode.endpoint.clone();

let selector = AccessModeSelector {
cmd_rx,
Expand All @@ -230,10 +221,7 @@ impl AccessModeSelector {

tokio::spawn(selector.into_future());

SpawnResult {
handle: AccessModeSelectorHandle { cmd_tx },
initial_api_endpoint,
}
Ok(AccessModeSelectorHandle { cmd_tx })
}

async fn into_future(mut self) {
Expand Down
14 changes: 9 additions & 5 deletions mullvad-daemon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub enum Error {
#[error(display = "Access method error")]
AccessMethodError(#[error(source)] access_method::Error),

// TODO(markus): This error variant should probably be re-named.
#[error(display = "API connection mode error")]
ApiConnectionModeError(#[error(source)] api::Error),

Expand Down Expand Up @@ -696,17 +697,15 @@ where
let connection_modes = settings.api_access_methods.collect_enabled();
let connection_modes_address_cache = api_runtime.address_cache.clone();

let api::SpawnResult {
handle: connection_modes_handler,
initial_api_endpoint,
} = api::AccessModeSelector::spawn(
let connection_modes_handler = api::AccessModeSelector::spawn(
cache_dir.clone(),
relay_selector.clone(),
connection_modes,
internal_event_tx.to_specialized_sender(),
connection_modes_address_cache.clone(),
)
.await;
.await
.map_err(Error::ApiConnectionModeError)?;

let api_handle = api_runtime
.mullvad_rest_handle(Box::pin(connection_modes_handler.clone().into_stream()))
Expand Down Expand Up @@ -778,6 +777,11 @@ where
let _ = param_gen_tx.unbounded_send(settings.tunnel_options.to_owned());
});

let initial_api_endpoint = connection_modes_handler
.get_current()
.await
.map_err(Error::ApiConnectionModeError)?
.endpoint;
let (offline_state_tx, offline_state_rx) = mpsc::unbounded();
#[cfg(target_os = "windows")]
let (volume_update_tx, volume_update_rx) = mpsc::unbounded();
Expand Down

0 comments on commit 3767424

Please sign in to comment.