Skip to content

Commit feb28b1

Browse files
authored
region cache (#1150)
### Before you submit your PR Make sure the following is true before submitting your PR: - [ ] I have read the [contributing guidelines](https://github.com/livekit/rust-sdks/blob/main/CONTRIBUTING.md) and validated that this PR will be accepted. - [ ] I have read and followed the principles regarding breaking changes, testing, and code quality. ### PR description Describe the changes in this PR. Explain what the PR is meant to solve and how to reproduce the issue in the first place. ### Breaking changes If this PR introduces breaking changes, list them here and document the rationale for introducing such a change. ### MSRV If the PR modifies the crate's MSRV (Minimum Supported Rust Version), document it here. ### Testing Ideally, unit test the code you add, but ensure you're not repeating existing test cases. Use as many already written scaffolding, utilities as possible; write your own, when needed. If external services, APIs, tokens are required (e.g., running an LK server instance), provide the necessary information. Make sure your tests perform useful, context-aware assertions and do not simply emulate "happy paths". ### Async We want the project to be runtime-agnostic, so please reuse what's already in [livekit-runtime](https://github.com/livekit/rust-sdks/blob/main/livekit-runtime/) and feel free to add anything missing. It's ok to use Tokio directly, when writing unit tests, if necessary. When testing, do not use artificial delays for the state to "catch up"; instead, respect the event flow and subscribe properly using channels or other mechanisms.
1 parent 5b7306c commit feb28b1

3 files changed

Lines changed: 361 additions & 18 deletions

File tree

livekit-api/src/signal_client/mod.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,25 @@ impl SignalClient {
238238
// if every region fails the caller sees why the last region
239239
// connection failed.
240240
let mut last_err = err;
241-
for url in urls.iter() {
242-
log::info!("fallback connection to: {}", url);
243-
match SignalInner::connect(url, token, options.clone(), publisher_offer.clone())
244-
.await
241+
for region_url in urls.iter() {
242+
log::info!("fallback connection to: {}", region_url);
243+
match SignalInner::connect(
244+
region_url,
245+
token,
246+
options.clone(),
247+
publisher_offer.clone(),
248+
)
249+
.await
245250
{
246251
Ok((inner, join_response, stream_events)) => {
247252
return Ok(handle_success(inner, join_response, stream_events))
248253
}
249-
Err(region_conn_err) => last_err = region_conn_err,
254+
Err(region_conn_err) => {
255+
// This region is unreachable; drop it from the cache
256+
// so the next attempt doesn't hand it out again.
257+
RegionUrlProvider::mark_failed(url, region_url);
258+
last_err = region_conn_err;
259+
}
250260
}
251261
}
252262

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

1302-
let urls = result.unwrap();
1312+
let (urls, _max_age) = result.unwrap();
13031313
assert_eq!(
13041314
urls,
13051315
vec![

0 commit comments

Comments
 (0)