Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/lukas_reconnect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
livekit: patch
livekit-api: patch
livekit-ffi: patch
livekit-uniffi: patch
---

harden reconnect behaviour - #1148 (@lukasIO)
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 38 additions & 8 deletions livekit-api/src/signal_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,48 @@ impl SignalClient {
if matches!(&err, SignalError::WsError(WsError::Http(e)) if e.status() != 403) {
log::error!("unexpected signal error: {}", err.to_string());
}
let urls = RegionUrlProvider::fetch_region_urls(url, token).await?;
let mut last_err = err;

for url in urls.iter() {
log::info!("fallback connection to: {}", url);
match SignalInner::connect(url, token, options.clone(), publisher_offer.clone())
.await
// Fetching region URLs is best-effort. `fetch_region_urls`
// already returns an empty list for non-cloud (direct /
// self-hosted) URLs, so those skip the fallback entirely. If the
// fetch itself fails (e.g. the region endpoint is unreachable),
// that must NOT be fatal: log a warning and fall back to the
// original connection error rather than masking it with the
// fetch error.
let urls = match RegionUrlProvider::fetch_region_urls(url, token).await {
Ok(urls) => urls,
Err(region_err) => {
log::warn!(
"failed to fetch region urls: {region_err}; surfacing original connection error"
);
return Err(err);
}
};

// With no region URLs to try, this surfaces the original error.
// Otherwise we keep the most recent region attempt error, so that
// if every region fails the caller sees why the last region
// connection failed.
let mut last_err = err;
for region_url in urls.iter() {
log::info!("fallback connection to: {}", region_url);
match SignalInner::connect(
region_url,
token,
options.clone(),
publisher_offer.clone(),
)
.await
{
Ok((inner, join_response, stream_events)) => {
return Ok(handle_success(inner, join_response, stream_events))
}
Err(err) => last_err = err,
Err(region_conn_err) => {
// This region is unreachable; drop it from the cache
// so the next attempt doesn't hand it out again.
RegionUrlProvider::mark_failed(url, region_url);
last_err = region_conn_err;
}
}
}

Expand Down Expand Up @@ -1279,7 +1309,7 @@ mod tests {
let endpoint = format!("http://127.0.0.1:{}/settings/regions", addr.port());
let result = region::fetch_from_endpoint(&endpoint, "fake-token").await;

let urls = result.unwrap();
let (urls, _max_age) = result.unwrap();
assert_eq!(
urls,
vec![
Expand Down
Loading
Loading