Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 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
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"
42 changes: 36 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,46 @@ 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)
var nextUrl = connectedUrl
var nextRegion: RegionInfo?

while true {
do {
// Prepare for next connect attempt.
await cleanUp(isFullReconnect: true)

try await fullConnectSequence(nextUrl, token)
_state.mutate { $0.connectedUrl = nextUrl }
// Exit loop on successful connection
break
} catch {
// Re-throw if is cancel.
if error is CancellationError {
throw error
}

if let region = nextRegion {
nextRegion = nil
log("Connect failed with region: \(region)")
regionManager(addFailedRegion: region)
}

try Task.checkCancellation()

if providedUrl.isCloud {
let region = try await regionManagerResolveBest()
nextUrl = region.url
nextRegion = region
}
}
}
}

do {
Expand Down
188 changes: 188 additions & 0 deletions Sources/LiveKit/Core/Room+Region.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/*
* 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: - Room+Region

extension Room {
static let regionManagerCacheInterval: TimeInterval = 30

// MARK: - Public

// prepareConnection should be called as soon as the page is loaded, in order
// to speed up the connection attempt.
//
// With LiveKit Cloud, it will also determine the best edge data center for
// the current client to connect to if a token is provided.
public func prepareConnection(url providedUrlString: String, token: String? = nil) async {
// Must be in disconnected state.
guard _state.connectionState == .disconnected else {
log("Room is not in disconnected state", .info)
return
}

guard let providedUrl = URL(string: providedUrlString), providedUrl.isValidForConnect else {
log("URL parse failed", .error)
return
}

log("Preparing connection to \(providedUrlString)")

do {
if providedUrl.isCloud, let token {
_state.mutate {
$0.providedUrl = providedUrl
$0.token = token
}

// Try to get the best region and warm that connection
if let bestRegion = try await regionManagerTryResolveBest() {
// Warm connection to the best region
await HTTP.prewarmConnection(url: bestRegion.url)
log("Prepared connection to \(bestRegion.url)")
} else {
// Fallback to warming the provided URL
await HTTP.prewarmConnection(url: providedUrl)
log("Prepared connection to \(providedUrl)")
}
} else {
// Not cloud or no token, just warm the provided URL
await HTTP.prewarmConnection(url: providedUrl)
log("Prepared connection to \(providedUrl)")
}
} catch {
log("Error while preparing connection: \(error)")
// Still try to warm the provided URL as fallback
await HTTP.prewarmConnection(url: providedUrl)
log("Prepared fallback connection to \(providedUrl)")
}
}

// MARK: - Internal

func regionManagerTryResolveBest() async throws -> RegionInfo? {
do {
return try await regionManagerResolveBest()
} catch {
log("[Region] Failed to resolve best region: \(error)")
return nil
}
}

func regionManagerResolveBest() async throws -> RegionInfo {
try await regionManagerRequestSettings()

guard let selectedRegion = _regionState.remaining.first else {
throw LiveKitError(.regionUrlProvider, message: "No more remaining regions.")
}

log("[Region] Resolved region: \(String(describing: selectedRegion))")

return selectedRegion
}

func regionManager(addFailedRegion region: RegionInfo) {
_regionState.mutate {
$0.remaining.removeAll { $0 == region }
}
}

func regionManagerResetAttempts() {
_regionState.mutate {
$0.remaining = $0.all
}
}

func regionManagerPrepareRegionSettings() {
Task.detached { [weak self] in
do {
try await self?.regionManagerRequestSettings()
} catch {
self?.log("[Region] Failed to prepare region settings: \(error)", .warning)
}
}
}

func regionManager(shouldRequestSettingsForUrl providedUrl: URL) -> Bool {
guard providedUrl.isCloud else { return false }
return _regionState.read {
guard providedUrl == $0.url, let regionSettingsUpdated = $0.lastRequested else { return true }
let interval = Date().timeIntervalSince(regionSettingsUpdated)
return interval > Self.regionManagerCacheInterval
}
}

// MARK: - Private

private func regionManagerRequestSettings() async throws {
let (providedUrl, token) = _state.read { ($0.providedUrl, $0.token) }

guard let providedUrl, let token else {
throw LiveKitError(.invalidState)
}

// Ensure url is for cloud.
guard providedUrl.isCloud else {
throw LiveKitError(.onlyForCloud)
}

guard regionManager(shouldRequestSettingsForUrl: providedUrl) else {
return
}

// Make a request which ignores cache.
var request = URLRequest(url: providedUrl.regionSettingsUrl(),
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData)

request.addValue("Bearer \(token)", forHTTPHeaderField: "authorization")

log("[Region] Requesting region settings...")

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

// Check the status code.
guard httpResponse.isStatusCodeOK else {
log("[Region] Failed to fetch region settings, error: \(String(describing: httpResponse))", .error)
throw LiveKitError(.regionUrlProvider, message: "Failed to fetch region settings with status code: \(httpResponse.statusCode)")
}

do {
// Try to parse the JSON data.
let regionSettings = try Livekit_RegionSettings(jsonUTF8Data: data)
let allRegions = regionSettings.regions.compactMap { $0.toLKType() }

if allRegions.isEmpty {
throw LiveKitError(.regionUrlProvider, message: "Fetched region data is empty.")
}

log("[Region] all regions: \(String(describing: allRegions))")

_regionState.mutate {
$0.url = providedUrl
$0.all = allRegions
$0.remaining = allRegions
$0.lastRequested = Date()
}
} catch {
throw LiveKitError(.regionUrlProvider, message: "Failed to parse region settings with error: \(error)")
}
}
}
Loading
Loading