Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
356fefc
RegionUrlProvider
hiroshihorie Jul 11, 2024
eba6899
Tests
hiroshihorie Jul 11, 2024
652b87f
Test cache interval
hiroshihorie Jul 11, 2024
14c0ffe
Return socket url by default
hiroshihorie Jul 11, 2024
c7901f9
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Jul 30, 2024
6990f8f
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Aug 19, 2024
69cff3e
Fix compile
hiroshihorie Aug 19, 2024
570da75
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Aug 19, 2024
1785583
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Aug 27, 2024
0799efe
Change to category
hiroshihorie Aug 27, 2024
812a294
Optimize
hiroshihorie Aug 27, 2024
9761f24
Remove sort
hiroshihorie Aug 28, 2024
9f9e42b
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Sep 3, 2024
962c703
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Sep 3, 2024
59faa1f
Improvements
hiroshihorie Sep 3, 2024
d886ed6
Prepare
hiroshihorie Sep 4, 2024
4bb928f
Update tests
hiroshihorie Sep 4, 2024
02a6f47
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Sep 9, 2024
16a4a44
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Sep 9, 2024
216adfa
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Sep 29, 2024
0af7d12
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Sep 9, 2025
f887ec2
Merge fixes
hiroshihorie Sep 9, 2025
d68c749
Changes
hiroshihorie Sep 9, 2025
3b50244
Minor adjustments
hiroshihorie Sep 9, 2025
4627d15
Prewarm url
hiroshihorie Sep 9, 2025
eba7b15
Fix error for non-cloud url
hiroshihorie Sep 9, 2025
efa03aa
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Dec 15, 2025
d7d91c6
nit
hiroshihorie Dec 15, 2025
aba6f99
exit retry if invalid token
hiroshihorie Dec 15, 2025
9584047
reset at connect
hiroshihorie Dec 15, 2025
2a7f4c3
handle region update on leave
hiroshihorie Dec 15, 2025
e04ac1b
only retry network errors
hiroshihorie Dec 15, 2025
c7c805e
reset attempts after reconnect
hiroshihorie Dec 15, 2025
257fb2f
preserve regions list
hiroshihorie Dec 15, 2025
068f3b7
check status code
hiroshihorie Dec 15, 2025
43fe2a2
add more tests
hiroshihorie Dec 15, 2025
5e1c2ba
move logic to method
hiroshihorie Dec 15, 2025
9ef5de9
optimize error check
hiroshihorie Dec 15, 2025
9aca9ff
migrate to actor
hiroshihorie Dec 15, 2025
5842d74
refactor
hiroshihorie Dec 16, 2025
841f4dc
Merge branch 'main' into hiroshi/prepare-connection
hiroshihorie Dec 16, 2025
6d678e5
Merge branch 'hiroshi/prepare-connection' of https://github.com/livek…
hiroshihorie Dec 16, 2025
27d7245
fix async warnings
hiroshihorie Dec 16, 2025
1ca5af7
simplify
hiroshihorie Dec 16, 2025
5b12bc4
refactoring
hiroshihorie Dec 16, 2025
29d3698
fix tests
hiroshihorie Dec 16, 2025
452fbd1
swift 6 test fixes
hiroshihorie Dec 16, 2025
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
1 change: 1 addition & 0 deletions .changes/prepare-connection
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="added" "Prepare connection & region pinning"
219 changes: 219 additions & 0 deletions Sources/LiveKit/Core/RegionManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import Foundation

// MARK: - RegionManager

actor RegionManager: Loggable {
struct State: Sendable {
var lastRequested: Date?
var all: [RegionInfo] = []
var remaining: [RegionInfo] = []
}

static let cacheInterval: TimeInterval = 30

nonisolated let providedUrl: URL
private var state = State()
private var settingsFetchTask: Task<Void, Error>?
private var settingsFetchTaskId: UUID?

init(providedUrl: URL) {
self.providedUrl = providedUrl
}

func cancel() {
settingsFetchTask?.cancel()
settingsFetchTask = nil
settingsFetchTaskId = nil
}

func resetAttemptsIfExhausted() {
guard state.remaining.isEmpty, !state.all.isEmpty else { return }
state.remaining = state.all
}

func resetAttempts() {
state.remaining = state.all
}

func resetAll() {
state = State()
}

func markFailed(region: RegionInfo) {
state.remaining.removeAll { $0 == region }
}

func shouldRequestSettings() -> Bool {
guard providedUrl.isCloud else { return false }
guard let lastRequested = state.lastRequested else { return true }
return Date().timeIntervalSince(lastRequested) > Self.cacheInterval
}

func prepareSettingsFetch(token: String) {
guard shouldRequestSettings() else { return }
startSettingsFetchIfNeeded(token: token)
}

func tryResolveBest(token: String) async -> RegionInfo? {
do {
return try await resolveBest(token: token)
} catch {
log("[Region] Failed to resolve best region: \(error)", .warning)
return nil
}
}

func resolveBest(token: String) async throws -> RegionInfo {
try await requestSettingsIfNeeded(token: token)
guard let selected = state.remaining.first else {
throw LiveKitError(.regionManager, message: "No more remaining regions.")
}

log("[Region] Resolved region: \(String(describing: selected))", .debug)
return selected
}

func updateFromServerReportedRegions(_ regions: Livekit_RegionSettings) {
guard providedUrl.isCloud else { return }

let allRegions = regions.regions.compactMap { $0.toLKType() }
guard !allRegions.isEmpty else { return }

// Keep previously failed regions excluded when updating the list.
let allIds = Set(state.all.map(\.regionId))
let remainingIds = Set(state.remaining.map(\.regionId))
let failedRegionIds = allIds.subtracting(remainingIds)

let remainingRegions = allRegions.filter { !failedRegionIds.contains($0.regionId) }
log("[Region] Updating regions from server-reported settings (\(allRegions.count)), remaining: \(remainingRegions.count)", .info)

state.all = allRegions
state.remaining = remainingRegions
state.lastRequested = Date()
}

// MARK: - Testing

func snapshot() -> State { state }

func setStateForTesting(_ state: State) {
self.state = state
}

// MARK: - Private

private func startSettingsFetchIfNeeded(token: String) {
if settingsFetchTask != nil { return }

let taskId = UUID()
settingsFetchTaskId = taskId

let task = Task { [providedUrl, token, taskId] in
do {
let data = try await Self.fetchRegionSettings(providedUrl: providedUrl, token: token)
let allRegions = try Self.parseRegionSettings(data: data)
try Task.checkCancellation()
applyFetchedRegions(allRegions)
clearSettingsFetchTask(if: taskId)
} catch {
log("[Region] Failed to fetch region settings: \(error)", .error)
clearSettingsFetchTask(if: taskId)
throw error
}
}

settingsFetchTask = task

Task { [weak self] in
_ = try? await task.value
// If the task failed before it could clear itself.
await self?.clearSettingsFetchTask(if: taskId)
}
}

private func requestSettingsIfNeeded(token: String) async throws {
guard providedUrl.isCloud else {
throw LiveKitError(.onlyForCloud)
}

guard shouldRequestSettings() else { return }
startSettingsFetchIfNeeded(token: token)
if let task = settingsFetchTask {
try await task.value
}
}

private func applyFetchedRegions(_ allRegions: [RegionInfo]) {
log("[Region] all regions: \(String(describing: allRegions))", .debug)
state.all = allRegions
state.remaining = allRegions
state.lastRequested = Date()
}

private func clearSettingsFetchTask(if taskID: UUID) {
guard settingsFetchTaskId == taskID else { return }
settingsFetchTaskId = nil
settingsFetchTask = nil
}

// MARK: - Static helpers (non-isolated)

private nonisolated static func fetchRegionSettings(providedUrl: URL, token: String) async throws -> Data {
var request = URLRequest(url: providedUrl.regionSettingsUrl(),
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)
request.addValue("Bearer \(token)", forHTTPHeaderField: "authorization")

let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
throw LiveKitError(.regionManager, message: "Failed to fetch region settings")
}

let statusCode = httpResponse.statusCode
guard (200 ..< 300).contains(statusCode) else {
let rawBody = String(data: data, encoding: .utf8)?
.trimmingCharacters(in: .whitespacesAndNewlines)
let body = if let rawBody, !rawBody.isEmpty {
rawBody.count > 1024 ? String(rawBody.prefix(1024)) + "..." : rawBody
} else {
"(No server message)"
}

if (400 ..< 500).contains(statusCode) {
throw LiveKitError(.validation, message: "Region settings error: HTTP \(statusCode): \(body)")
}

throw LiveKitError(.regionManager, message: "Failed to fetch region settings: HTTP \(statusCode): \(body)")
}

return data
}

private nonisolated static func parseRegionSettings(data: Data) throws -> [RegionInfo] {
do {
let regionSettings = try Livekit_RegionSettings(jsonUTF8Data: data)
let allRegions = regionSettings.regions.compactMap { $0.toLKType() }
guard !allRegions.isEmpty else {
throw LiveKitError(.regionManager, message: "Fetched region data is empty.")
}
return allRegions
} catch {
throw LiveKitError(.regionManager, message: "Failed to parse region settings with error: \(error)")
}
}
}
37 changes: 31 additions & 6 deletions Sources/LiveKit/Core/Room+Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ extension Room {
throw LiveKitError(.invalidState)
}

guard let url = _state.url, let token = _state.token else {
guard let url = _state.providedUrl, let token = _state.token else {
log("[Connect] Url or token is nil", .error)
throw LiveKitError(.invalidState)
}
Expand Down Expand Up @@ -344,16 +344,36 @@ extension Room {
$0.connectionState = .reconnecting
}

await cleanUp(isFullReconnect: true)
let (providedUrl, connectedUrl, token) = _state.read { ($0.providedUrl, $0.connectedUrl, $0.token) }

guard let url = _state.url,
let token = _state.token
else {
guard let providedUrl, let connectedUrl, let token else {
log("[Connect] Url or token is nil")
throw LiveKitError(.invalidState)
}

try await fullConnectSequence(url, token)
let finalUrl: URL
if providedUrl.isCloud {
guard let regionManager = await regionManager(for: providedUrl) else {
throw LiveKitError(.onlyForCloud)
}

finalUrl = try await connectWithCloudRegionFailover(regionManager: regionManager,
initialUrl: connectedUrl,
initialRegion: nil,
token: token,
prepareBeforeFirstAttempt: { [weak self] in
await self?.cleanUp(isFullReconnect: true)
},
prepareAfterFailure: { [weak self] in
await self?.cleanUp(isFullReconnect: true)
})
} else {
await cleanUp(isFullReconnect: true)
try await fullConnectSequence(connectedUrl, token)
finalUrl = connectedUrl
}

_state.mutate { $0.connectedUrl = finalUrl }
}

do {
Expand Down Expand Up @@ -419,6 +439,11 @@ extension Room {
$0.isReconnectingWithMode = nil
$0.nextReconnectMode = nil
}

if let providedUrl = _state.providedUrl, providedUrl.isCloud, let regionManager = await regionManager(for: providedUrl) {
// Clear failed region attempts after a successful reconnect.
await regionManager.resetAttempts()
}
} catch {
log("[Connect] Sequence failed with error: \(error)")

Expand Down
Loading
Loading