Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

perf(app-proxy): refresh branding off the request path

Hampus8b658b74ca9b
diff --git a/fluxer_app_proxy/src/branding_images.rs b/fluxer_app_proxy/src/branding_images.rs
index 903acd02b..9f805e4e0 100644
--- a/fluxer_app_proxy/src/branding_images.rs
+++ b/fluxer_app_proxy/src/branding_images.rs
@@ -312,14 +312,8 @@ impl InstanceBranding {
Ok(())
}
- pub async fn refresh_if_due(
- &self,
- client: &reqwest::Client,
- upstream_url: &str,
- interval: Duration,
- ) {
- let Ok(mut gate) = tokio::time::timeout(DISCOVERY_FETCH_TIMEOUT, self.gate.lock()).await
- else {
+ pub fn refresh_if_due(&self, client: &reqwest::Client, upstream_url: &str, interval: Duration) {
+ let Ok(mut gate) = self.gate.clone().try_lock_owned() else {
return;
};
if gate
@@ -328,27 +322,32 @@ impl InstanceBranding {
{
return;
}
+ let branding = self.clone();
+ let client = client.clone();
+ let upstream_url = upstream_url.to_owned();
let interval = interval.max(REFRESH_INTERVAL_MIN);
- let wait = match self.refresh(client, upstream_url).await {
- Ok(()) => {
- gate.consecutive_failures = 0;
- interval
- }
- Err(error) => {
- let backoff = interval
- .saturating_mul(1 << gate.consecutive_failures.min(5))
- .min(REFRESH_BACKOFF_MAX);
- gate.consecutive_failures = gate.consecutive_failures.saturating_add(1);
- tracing::warn!(
- %error,
- url = %upstream_url,
- consecutive_failures = gate.consecutive_failures,
- backoff_ms = backoff.as_millis() as u64,
- "instance branding refresh failed, keeping the previous branding"
- );
- backoff
- }
- };
- gate.next_attempt = Some(Instant::now() + wait);
+ tokio::spawn(async move {
+ let wait = match branding.refresh(&client, &upstream_url).await {
+ Ok(()) => {
+ gate.consecutive_failures = 0;
+ interval
+ }
+ Err(error) => {
+ let backoff = interval
+ .saturating_mul(1 << gate.consecutive_failures.min(5))
+ .min(REFRESH_BACKOFF_MAX);
+ gate.consecutive_failures = gate.consecutive_failures.saturating_add(1);
+ tracing::warn!(
+ %error,
+ url = %upstream_url,
+ consecutive_failures = gate.consecutive_failures,
+ backoff_ms = backoff.as_millis() as u64,
+ "instance branding refresh failed, keeping the previous branding"
+ );
+ backoff
+ }
+ };
+ gate.next_attempt = Some(Instant::now() + wait);
+ });
}
}
diff --git a/fluxer_app_proxy/src/routes/spa_index.rs b/fluxer_app_proxy/src/routes/spa_index.rs
index c7639e11e..cada948ec 100644
--- a/fluxer_app_proxy/src/routes/spa_index.rs
+++ b/fluxer_app_proxy/src/routes/spa_index.rs
@@ -201,7 +201,7 @@ async fn serve_spa_index(state: &AppState, headers: &HeaderMap, request_path: &s
Ok(content) => content,
Err(response) => return response,
};
- let branding = state.branding_snapshot().await;
+ let branding = state.branding_snapshot();
let raw_html = if state.config.self_hosted {
apply_instance_branding(strip_link_preview_metadata(&raw_html), &branding)
} else {
diff --git a/fluxer_app_proxy/src/routes/spa_static.rs b/fluxer_app_proxy/src/routes/spa_static.rs
index 16874f424..ef5490b00 100644
--- a/fluxer_app_proxy/src/routes/spa_static.rs
+++ b/fluxer_app_proxy/src/routes/spa_static.rs
@@ -29,11 +29,7 @@ pub async fn version_json(State(state): State<AppState>, headers: HeaderMap) ->
}
pub async fn manifest_json(State(state): State<AppState>, headers: HeaderMap) -> Response {
- let branding = if state.config.self_hosted {
- Some(state.branding_snapshot().await)
- } else {
- None
- };
+ let branding = state.config.self_hosted.then(|| state.branding_snapshot());
serve_static_text_file_with_substitutions(
&state,
&headers,
@@ -114,7 +110,7 @@ pub async fn browserconfig_xml(State(state): State<AppState>, headers: HeaderMap
}
pub async fn favicon_ico(State(state): State<AppState>) -> Response {
- let location = match state.branding_snapshot().await.favicon_url.as_deref() {
+ let location = match state.branding_snapshot().favicon_url.as_deref() {
Some(favicon_url) => favicon_url.to_owned(),
None => format!(
"{}/web/favicon.ico",
diff --git a/fluxer_app_proxy/src/state.rs b/fluxer_app_proxy/src/state.rs
index 05a004e0f..08e2b1dc5 100644
--- a/fluxer_app_proxy/src/state.rs
+++ b/fluxer_app_proxy/src/state.rs
@@ -111,15 +111,13 @@ impl AppState {
})
}
- pub async fn branding_snapshot(&self) -> Arc<BrandingSnapshot> {
+ pub fn branding_snapshot(&self) -> Arc<BrandingSnapshot> {
if self.config.self_hosted {
- self.branding
- .refresh_if_due(
- &self.http_client,
- &self.config.discovery_upstream_url,
- Duration::from_millis(self.config.discovery_refresh_interval_ms),
- )
- .await;
+ self.branding.refresh_if_due(
+ &self.http_client,
+ &self.config.discovery_upstream_url,
+ Duration::from_millis(self.config.discovery_refresh_interval_ms),
+ );
}
self.branding.snapshot()
}