Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/stale-region-cache-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'livekit-client': patch
---

If the region settings endpoint is unreachable, use the expired region list instead of failing the connection.
36 changes: 28 additions & 8 deletions src/room/RegionUrlProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ describe('RegionUrlProvider', () => {
expect(region2).toBeTruthy();
});

it('preserves cache when one instance fails to fetch', async () => {
it('falls back to the expired cache when the refresh fails', async () => {
const mockSettings = createMockRegionSettings([
{ region: 'us-west', url: 'wss://us-west.livekit.cloud' },
]);
Expand All @@ -759,21 +759,41 @@ describe('RegionUrlProvider', () => {
.mockResolvedValueOnce(
createMockResponse(200, mockSettings, { 'Cache-Control': 'max-age=3600' }),
)
.mockResolvedValueOnce(createMockResponse(500));
.mockResolvedValueOnce(createMockResponse(500))
.mockRejectedValueOnce(new TypeError('Failed to fetch'));

// First provider populates cache
const provider1 = new RegionUrlProvider('wss://test.livekit.cloud', 'token1');
await provider1.getNextBestRegionUrl();

// Advance time to expire cache
vi.setSystemTime(Date.now() + 4000000);

// Second provider tries to fetch but fails
const provider2 = new RegionUrlProvider('wss://test.livekit.cloud', 'token2');
await expect(provider2.getNextBestRegionUrl()).rejects.toThrow();
expect(await provider2.getNextBestRegionUrl()).toBe('wss://us-west.livekit.cloud');

// Cache should still be accessible by a third instance if still valid
expect(fetchMock).toHaveBeenCalledTimes(2);
const provider3 = new RegionUrlProvider('wss://test.livekit.cloud', 'token3');
expect(await provider3.getNextBestRegionUrl()).toBe('wss://us-west.livekit.cloud');
expect(fetchMock).toHaveBeenCalledTimes(3);
});

it('does not fall back to the expired cache when the token is rejected', async () => {
const mockSettings = createMockRegionSettings([
{ region: 'us-west', url: 'wss://us-west.livekit.cloud' },
]);

fetchMock
.mockResolvedValueOnce(
createMockResponse(200, mockSettings, { 'Cache-Control': 'max-age=3600' }),
)
.mockResolvedValueOnce(createMockResponse(401));

await new RegionUrlProvider('wss://test.livekit.cloud', 'token1').getNextBestRegionUrl();

vi.setSystemTime(Date.now() + 4000000);

const provider2 = new RegionUrlProvider('wss://test.livekit.cloud', 'token2');
await expect(provider2.getNextBestRegionUrl()).rejects.toMatchObject({
reason: ConnectionErrorReason.NotAllowed,
});
});

it('handles token update correctly', async () => {
Expand Down
18 changes: 16 additions & 2 deletions src/room/RegionUrlProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,22 @@ export class RegionUrlProvider {
let cachedSettings = RegionUrlProvider.cache.get(this.serverUrl.hostname);

if (!cachedSettings || Date.now() - cachedSettings.updatedAtInMs > cachedSettings.maxAgeInMs) {
cachedSettings = await this.fetchRegionSettings(abortSignal);
RegionUrlProvider.updateCachedRegionSettings(this.serverUrl, this.token, cachedSettings);
try {
const freshSettings = await this.fetchRegionSettings(abortSignal);
RegionUrlProvider.updateCachedRegionSettings(this.serverUrl, this.token, freshSettings);
cachedSettings = freshSettings;
} catch (error: unknown) {
// A stale region list beats no region list, but a cancel or a rejected token is final.
if (
!cachedSettings ||
(error instanceof ConnectionError &&
(error.reason === ConnectionErrorReason.Cancelled ||
error.reason === ConnectionErrorReason.NotAllowed))
) {
throw error;
}
log.warn('could not refresh region settings, using expired cache', { error });
}
}

const regionsLeft = cachedSettings.regionSettings.regions.filter(
Expand Down
Loading