diff --git a/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataDatabase.swift b/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataDatabase.swift index 42c3d052f..8e69b498e 100644 --- a/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataDatabase.swift +++ b/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataDatabase.swift @@ -28,9 +28,17 @@ final actor HTMLMetadataDatabase { /// True when the last contact for this URL returned a 4xx — a dead or /// missing homepage we shouldn’t keep re-requesting. - var isFailure: Bool { + var isPersistentFailure: Bool { (400...499).contains(statusCode) } + /// True when the last contact failed before getting a response (DNS, + /// TLS, network) — likely transient, retry sooner than persistent failures. + var isTransientFailure: Bool { + statusCode == 0 + } + var isFailure: Bool { + isPersistentFailure || isTransientFailure + } } private static let tableCreationStatements = """ @@ -44,7 +52,12 @@ final actor HTMLMetadataDatabase { private static let maximumDaysWithoutCheck = 30 /// A homepage that returned a 4xx isn't retried until this many days pass. - private static let failureRetryDays = 11 + private static let persistentFailureRetryDays = 11 + + /// A homepage that failed before getting a response (DNS, TLS, network) + /// isn't retried until this many hours pass. Shorter than the persistent + /// window since these failures are often transient. + private static let transientFailureRetryHours = 5 private init(databasePath: String) { let database = FMDatabase.openAndSetUpDatabase(path: databasePath) @@ -110,13 +123,19 @@ final actor HTMLMetadataDatabase { return cached.record } - /// True if the last contact for this URL was a recent 4xx, meaning we should - /// skip re-downloading it for now. + /// True if the last contact for this URL was a recent failure of any kind + /// (4xx or transient), and we shouldn't re-attempt yet. func recentlyFailed(for url: String) -> Bool { - guard let cached = cachedOrFetched(url), cached.isFailure else { + guard let cached = cachedOrFetched(url) else { return false } - return Date().timeIntervalSince(cached.lastChecked) < TimeInterval(days: Self.failureRetryDays) + if cached.isPersistentFailure { + return Date().timeIntervalSince(cached.lastChecked) < TimeInterval(days: Self.persistentFailureRetryDays) + } + if cached.isTransientFailure { + return Date().timeIntervalSince(cached.lastChecked) < TimeInterval(hours: Self.transientFailureRetryHours) + } + return false } func save(_ record: HTMLMetadataRecord, statusCode: Int) { @@ -124,10 +143,11 @@ final actor HTMLMetadataDatabase { HTMLMetadataTable.insertOrReplace(record: record, statusCode: statusCode, database: database) } - /// Records a 4xx outcome for a URL so we stop re-requesting it across launches. - /// Mirrors the SQLite guard in HTMLMetadataTable.noteFailure: never downgrade a - /// successful entry — keep serving its metadata. Only record the failure when - /// there’s no successful entry to protect. + /// Records a failure outcome for a URL so we stop re-requesting it across + /// launches. Used for both 4xx responses and transient pre-response errors + /// (DNS, TLS, network) — the latter via `noteTransientFailure`. The WHERE + /// guard in the underlying SQL preserves any existing successful entry — + /// keep serving its metadata, don't downgrade. func noteFailure(url: String, statusCode: Int) { if cache[url]?.isFailure ?? true { cache[url] = CachedRecord(record: nil, lastChecked: Date(), statusCode: statusCode) @@ -135,6 +155,12 @@ final actor HTMLMetadataDatabase { HTMLMetadataTable.noteFailure(url: url, statusCode: statusCode, database: database) } + /// Records a pre-response failure (DNS, TLS, network unreachable) so we + /// back off for `transientFailureRetryHours` before trying again. + func noteTransientFailure(url: String) { + noteFailure(url: url, statusCode: 0) + } + func emptyCache() { cache.removeAll() } diff --git a/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataDownloader.swift b/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataDownloader.swift index 9a05515bc..8624d83f9 100644 --- a/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataDownloader.swift +++ b/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataDownloader.swift @@ -148,7 +148,9 @@ nonisolated private extension HTMLMetadataDownloader { activityLog.didFail(.htmlMetadataDownloader, kind: kind, error: statusError) } catch { - // Download failed — try returning stale cached data. + // Pre-response failure (DNS, TLS, network). Record so we back off + // across launches, then try returning stale cached data. + await HTMLMetadataDatabase.shared.noteTransientFailure(url: url) await returnStaleCacheIfAvailable(url) activityLog.didFail(.htmlMetadataDownloader, kind: kind, error: error) diff --git a/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataTable.swift b/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataTable.swift index a3d11e03e..4fc57155d 100644 --- a/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataTable.swift +++ b/Modules/HTMLMetadata/Sources/HTMLMetadata/HTMLMetadataTable.swift @@ -55,12 +55,13 @@ struct HTMLMetadataTable { return (record, lastChecked, statusCode) } - /// Records a 4xx outcome for a URL: inserts a failure marker, or updates an - /// existing failure row's status and date. The `WHERE` guard preserves rows - /// that hold a successful (2xx) fetch, so a transient 4xx never erases good - /// cached metadata. + /// Records a failure outcome for a URL: inserts a failure marker, or updates + /// an existing failure row's status and date. The `WHERE` guard preserves + /// rows that hold a successful (2xx) fetch — a transient failure never + /// erases good cached metadata. Accepts both 4xx (persistent) and 0 + /// (transient pre-response failure). static func noteFailure(url: String, statusCode: Int, database: FMDatabase) { - let sql = "INSERT INTO \(name) (\(Column.url), \(Column.lastChecked), \(Column.statusCode)) VALUES (?, ?, ?) ON CONFLICT(\(Column.url)) DO UPDATE SET \(Column.lastChecked) = excluded.\(Column.lastChecked), \(Column.statusCode) = excluded.\(Column.statusCode) WHERE \(Column.statusCode) >= 400;" + let sql = "INSERT INTO \(name) (\(Column.url), \(Column.lastChecked), \(Column.statusCode)) VALUES (?, ?, ?) ON CONFLICT(\(Column.url)) DO UPDATE SET \(Column.lastChecked) = excluded.\(Column.lastChecked), \(Column.statusCode) = excluded.\(Column.statusCode) WHERE \(Column.statusCode) >= 400 OR \(Column.statusCode) = 0;" database.executeUpdate(sql, withArgumentsIn: [url, Date().timeIntervalSince1970, statusCode]) }