Refactor AppDatabaseUpdater for per-server updates #4224
Conversation
Reworks AppDatabaseUpdater to support bounded parallel per-server updates, global and per-server throttling with exponential backoff, and improved cancellation handling. Adds detailed documentation, separates update logic into smaller methods, and ensures batched database writes with differential deletes. This improves update reliability, resource usage, and responsiveness to cancellation.
AppDatabaseUpdater now supports concurrent updates per server instead of a single global update. The update method requires a server parameter, and task management is handled via an actor for thread safety. Call sites in SettingsView and WebViewController are updated to use the new API, and legacy global update logic is removed.
There was a problem hiding this comment.
Pull request overview
This PR refactors AppDatabaseUpdater to support per-server updates instead of updating all servers at once. The updater now ensures only one update per server runs at a time (while allowing different servers to update concurrently), implements per-server throttling with exponential backoff on failures, and improves cancellation handling throughout the update pipeline.
Changes:
- Introduced a
TaskCoordinatoractor for thread-safe management of per-server update tasks - Implemented per-server throttling (120s base) with exponential backoff (up to 5 minutes) on consecutive failures
- Enhanced cancellation checks at critical points in the update pipeline to enable early exit
- Updated API from
update()toupdate(server: Server)and modified all call sites accordingly
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| Sources/Shared/Environment/AppDatabaseUpdater.swift | Core refactor: added TaskCoordinator actor, per-server task tracking, throttling/backoff logic, and improved cancellation handling throughout |
| Sources/App/WebView/WebViewController.swift | Updated to call update(server:) for the specific server when connection info changes |
| Sources/App/Settings/Settings/SettingsView.swift | Changed to loop through all servers and call update(server:) for each one |
| Sources/App/Scenes/WebViewSceneDelegate.swift | Removed blanket database update call when scene becomes active (now happens per-server on connection change) |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Wrap all withCheckedContinuation and withCheckedThrowingContinuation usages with [weak self] to prevent retain cycles and ensure continuations are resumed or cancelled if self is deallocated. This improves memory safety and prevents potential leaks or crashes.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4224 +/- ##
=======================================
Coverage ? 44.32%
=======================================
Files ? 252
Lines ? 14580
Branches ? 0
=======================================
Hits ? 6462
Misses ? 8118
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary
Screenshots
Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#
Any other notes