chore(relay): connect to portal in the background during startup (#4594)

In a prior design of the relay and the `phoenix-channel`, connecting to
the portal was a blocking operation, i.e. we weren't meant to start the
relaying operations before the portal connection succeeded.

Since then, `phoenix-channel` got refactored to have an internal
(re)-connection mechanism, meaning we don't actually need to `.await`
anything to obtain a `PhoenixChannel` instance that we can use to
initialize the `Server`. Furthermore, we changed the health-check to
return 200 OK prior to the portal connection being established in #4553.

Taking both of these into account, there is no more need to block on the
portal connection being established, which allows us to remove the use
of `phoenix_channel::init` and connect in the background whilst we
already accept STUN & TURN traffic.
This commit is contained in:
Thomas Eizinger
2024-04-12 13:48:09 +10:00
committed by GitHub
parent b2eba1e89a
commit 31eec1aac7

View File

@@ -18,7 +18,7 @@ use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::Poll;
use std::time::{Duration, Instant};
use tracing::{level_filters::LevelFilter, Instrument, Subscriber};
use tracing::{level_filters::LevelFilter, Subscriber};
use tracing_core::Dispatch;
use tracing_stackdriver::CloudTraceConfiguration;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
@@ -123,14 +123,27 @@ async fn main() -> Result<()> {
));
let channel = if let Some(token) = args.token.as_ref() {
let base_url = args.api_url.clone();
let stamp_secret = server.auth_secret();
use secrecy::ExposeSecret;
let span = tracing::debug_span!("connect_to_portal", config_url = %base_url);
let login = LoginUrl::relay(
args.api_url.clone(),
token,
args.name.clone(),
args.public_ip4_addr,
args.public_ip6_addr,
)?;
connect_to_portal(&args, token, base_url, stamp_secret)
.instrument(span)
.await?
Some(PhoenixChannel::connect(
Secret::new(login),
format!("relay/{}", env!("CARGO_PKG_VERSION")),
"relay",
JoinMessage {
stamp_secret: server.auth_secret().expose_secret().to_string(),
},
ExponentialBackoffBuilder::default()
.with_max_elapsed_time(Some(MAX_PARTITION_TIME))
.build(),
))
} else {
tracing::warn!(target: "relay", "No portal token supplied, starting standalone mode");
@@ -249,38 +262,6 @@ fn env_filter() -> EnvFilter {
.from_env_lossy()
}
async fn connect_to_portal(
args: &Args,
token: &SecretString,
url: Url,
stamp_secret: &SecretString,
) -> Result<Option<PhoenixChannel<JoinMessage, (), ()>>> {
use secrecy::ExposeSecret;
let login = LoginUrl::relay(
url,
token,
args.name.clone(),
args.public_ip4_addr,
args.public_ip6_addr,
)?;
let (channel, Init {}) = phoenix_channel::init::<_, Init, _, _>(
Secret::new(login),
format!("relay/{}", env!("CARGO_PKG_VERSION")),
"relay",
JoinMessage {
stamp_secret: stamp_secret.expose_secret().to_string(),
},
ExponentialBackoffBuilder::default()
.with_max_elapsed_time(Some(MAX_PARTITION_TIME))
.build(),
)
.await??;
Ok(Some(channel))
}
#[derive(serde::Deserialize, Debug)]
struct Init {}