Revise comments.

This commit is contained in:
Brent Simmons committed 2026-05-29 13:09:38 -07:00
1 parent e4deea8781
commit 7b6e5fcbd6
3 files changed
+13 -41

No files matched your search

@@ -26,13 +26,11 @@ final actor HTMLMetadataDatabase {
let lastChecked: Date
let statusCode: Int
/// True when the last contact for this URL returned a 4xx — a dead or
/// missing homepage we shouldn’t keep re-requesting.
/// True when the last contact returned a 4xx.
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.
/// True when the last contact failed before getting a response (DNS, TLS, network).
var isTransientFailure: Bool {
statusCode == 0
}
@@ -48,15 +46,8 @@ final actor HTMLMetadataDatabase {
/// 73 hours — prime number close to 3 days.
private static let cacheExpirationHours = 73
/// Entries that haven't been checked in a number of days are deleted on init.
private static let maximumDaysWithoutCheck = 30
/// A homepage that returned a 4xx isn't retried until this many days pass.
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) {
@@ -70,7 +61,6 @@ final actor HTMLMetadataDatabase {
name: .appDidGoToBackground, object: nil
)
// Run maintenance off the main thread (init may be evaluated there).
Task {
await performStartupMaintenance()
}
@@ -87,8 +77,7 @@ final actor HTMLMetadataDatabase {
}
}
/// Returns the cached entry for a URL — in-memory if present, otherwise loaded
/// from SQLite and cached. Returns nil only when there’s no row at all.
/// Returns the entry, loading from disk if not in memory.
private func cachedOrFetched(_ url: String) -> CachedRecord? {
if let cached = cache[url] {
return cached
@@ -103,8 +92,7 @@ final actor HTMLMetadataDatabase {
return cached
}
/// Returns a non-expired, successfully-fetched record. Returns nil if there’s
/// no record, it’s a failure entry, or it has expired.
/// Returns a non-expired success record, or nil.
func cachedRecord(for url: String) -> HTMLMetadataRecord? {
guard let cached = cachedOrFetched(url), !cached.isFailure, let record = cached.record else {
return nil
@@ -115,7 +103,7 @@ final actor HTMLMetadataDatabase {
return record
}
/// Returns a successfully-fetched record regardless of expiration. Used for stale fallback.
/// Returns a success record regardless of expiration.
func staleCachedRecord(for url: String) -> HTMLMetadataRecord? {
guard let cached = cachedOrFetched(url), !cached.isFailure else {
return nil
@@ -123,8 +111,7 @@ final actor HTMLMetadataDatabase {
return cached.record
}
/// 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.
/// True if the last contact failed recently (4xx or transient).
func recentlyFailed(for url: String) -> Bool {
guard let cached = cachedOrFetched(url) else {
return false
@@ -143,11 +130,7 @@ final actor HTMLMetadataDatabase {
HTMLMetadataTable.insertOrReplace(record: record, statusCode: statusCode, database: database)
}
/// 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.
/// Records a failure outcome. Accepts both 4xx and transient (statusCode 0).
func noteFailure(url: String, statusCode: Int) {
if cache[url]?.isFailure ?? true {
cache[url] = CachedRecord(record: nil, lastChecked: Date(), statusCode: statusCode)
@@ -155,8 +138,7 @@ 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.
/// Records a pre-response failure (DNS, TLS, network).
func noteTransientFailure(url: String) {
noteFailure(url: url, statusCode: 0)
}
@@ -23,11 +23,7 @@ nonisolated public final class HTMLMetadataDownloader: Sendable {
private let attemptDates = OSAllocatedUnfairLock(initialState: [String: Date]())
// Synchronous in-memory mirror of records that have been loaded from the
// database or freshly downloaded this session. The database (see
// HTMLMetadataDatabase) is the persistent store; this cache exists so
// `cachedMetadata(for:)` can answer synchronously after a record has been
// posted via .htmlMetadataAvailable. Cleared on background and low memory.
/// In-memory mirror so `cachedMetadata(for:)` can answer synchronously.
private let cache = OSAllocatedUnfairLock(initialState: [String: HTMLMetadataRecord]())
init() {
@@ -43,9 +39,8 @@ nonisolated public final class HTMLMetadataDownloader: Sendable {
cache.withLock { $0.removeAll() }
}
/// Returns cached metadata synchronously if available, otherwise kicks off a fetch.
/// When the fetch (database lookup or download) finishes, the record is cached
/// and a .htmlMetadataAvailable notification is posted — callers re-query then.
/// Returns cached metadata synchronously, or kicks off a fetch and returns nil.
/// Callers re-query after observing `.htmlMetadataAvailable`.
public func cachedMetadata(for url: String) -> HTMLMetadataRecord? {
if Self.shouldSkipDownloadingMetadata(url) {
return nil
@@ -148,8 +143,7 @@ nonisolated private extension HTMLMetadataDownloader {
activityLog.didFail(.htmlMetadataDownloader, kind: kind, error: statusError)
} catch {
// Pre-response failure (DNS, TLS, network). Record so we back off
// across launches, then try returning stale cached data.
// Pre-response failure (DNS, TLS, network).
await HTMLMetadataDatabase.shared.noteTransientFailure(url: url)
await returnStaleCacheIfAvailable(url)
@@ -55,11 +55,7 @@ struct HTMLMetadataTable {
return (record, lastChecked, statusCode)
}
/// 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).
/// The WHERE guard preserves rows with a 2xx success.
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 OR \(Column.statusCode) = 0;"
database.executeUpdate(sql, withArgumentsIn: [url, Date().timeIntervalSince1970, statusCode])