Re-pick Plex server address on network change, foreground, and stream failure (dev 36)

The address chosen at sign-in (LAN first) was kept forever, so away from
home streams hit a dead LAN address (TLS error) or the relay (503). Store
every address plex.tv lists, probe them all bypassing the offline cache,
and keep the best one that answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rootandClaude Opus 5.5 committed 2026-11-19 15:56:02 -08:00
1 parent 9f68aa588b
commit e02428ab70
7 files changed
+100 -17

No files matched your search

+41
View File
@@ -21,6 +21,7 @@ final class PlexClient: @unchecked Sendable {
private let lock = NSLock()
private var builtFrom = "\u{0}"
private var built: [MediaSource] = []
private var rerouting = false
init(http: Http, settings: Settings) {
self.http = http
@@ -285,6 +286,45 @@ final class PlexClient: @unchecked Sendable {
return box.url
}
// ---- address re-picking ------------------------------------------------------------
/// Re-picks each Plex server's address. Sign-in picks one (a LAN address at home) and it
/// used to stick forever, so away from home every stream hit a dead LAN address (TLS error)
/// or a relay (503). Probes every known address and keeps the best one that answers;
/// when none answer, asks plex.tv for the current list. Returns true if any address changed.
@discardableResult
func reroute(_ why: String) async -> Bool {
lock.lock()
if rerouting { lock.unlock(); return false }
rerouting = true
lock.unlock()
defer { lock.lock(); rerouting = false; lock.unlock() }
let tv = plexTv()
var changed = false
for srv in settings.servers where srv.kind == .plex && srv.signedIn {
var uris = srv.connections ?? []
if !uris.contains(srv.base) { uris.append(srv.base) }
var best = await tv.firstAlive(uris, machineId: srv.machineId, token: srv.token)
var fresh: [String]?
if best == nil || srv.connections == nil, !srv.machineId.isEmpty,
let r = (try? await tv.resources(token: srv.token))?.first(where: { $0.clientIdentifier == srv.machineId }) {
fresh = PlexServer.ranked(r)
if best == nil, let f = fresh { best = await tv.firstAlive(f, machineId: srv.machineId, token: srv.token) }
}
var s = srv
if let fresh { s.connections = fresh }
if let best { s.url = best }
guard s != srv else { continue }
if s.url != srv.url {
changed = true
Log.shared.info("plex", "\(srv.label) (\(why)): \(srv.base) -> \(s.base)")
}
settings.save(s)
}
return changed
}
// ---- sign-in helpers --------------------------------------------------------------
/// A Plex client for plex.tv calls and for a server that isn't saved yet.
@@ -323,6 +363,7 @@ final class PlexClient: @unchecked Sendable {
?? settings.servers.first { $0.kind == .plex && $0.machineId.isEmpty && $0.base == h.url.trimmingCharacters(in: CharacterSet(charactersIn: "/")) }
var srv = known ?? MediaServer(id: settings.nextServerId(), kind: .plex)
srv.url = h.url
srv.connections = PlexServer.ranked(h.res)
srv.token = h.res.accessToken.isEmpty ? token : h.res.accessToken
srv.machineId = h.res.clientIdentifier
srv.user = user