Commit c475ac1
feat(power): real metered-network detection on macOS and Linux (#95)
## What
Metered-network detection was real on Windows only
(`INetworkCostManager::GetCost`); macOS and Linux returned a
conservative unmetered stub, so `skip_on_metered` was inert on those
platforms (issue #32). This wires both.
- **Linux** - reads NetworkManager's aggregate `Metered` property
(`org.freedesktop.NetworkManager`) over the system bus via zbus's
blocking API, dispatched through `tokio::task::spawn_blocking` (zbus's
blocking API must not be driven from inside an async runtime) and
bounded by a 5 s timeout so a wedged bus can never stall the AC/battery
poll. `NMMetered` YES/GUESS_YES -> metered, NO/GUESS_NO -> not metered,
UNKNOWN or a failed read (no NetworkManager / no system bus) ->
`Unknown` -> not metered.
- **macOS** - there is no literal "metered" bit. A long-lived
`NWPathMonitor` (Network.framework C API; its update handler is an
Objective-C block via `block2`, delivered on a libdispatch global queue)
caches `nw_path_is_expensive` (cellular / personal hotspot) `||
nw_path_is_constrained` (Low Data Mode) - the documented metered proxies
- into an `AtomicU8` that the 30 s power poll reads cheaply.
## Design
The per-OS raw read is separated from the decision logic. Three pure
`classify_*` functions (`classify_windows_cost`, `classify_nm_metered`,
`classify_nw_path`) plus `MeteredStatus`/`on_metered` turn each OS's raw
value into the `on_metered_network` bool. They are `#[cfg(any(test,
target_os = ...))]`, so **all three are compiled and unit-tested on
every OS** - the classification is CI-covered on Windows, macOS, and
Linux alike; only the thin OS-call adapters (COM / D-Bus /
`NWPathMonitor`) are compile-checked-only off their native OS.
Every read collapses ambiguity to `MeteredStatus::Unknown`, which maps
to `false` (not metered) - the safe direction, since a wrong "not
metered" only fails to skip a rare metered link whereas a wrong
"metered" would stall ALL sync.
No stored-format or public-API changes: `PowerState` shape and the
`PowerSource` trait are unchanged; this is purely additive.
## Tested vs compile-only
Development was on Windows, so the non-Windows adapters are
compile-verified via the 3-OS CI matrix, not locally:
- **Unit-tested on all three CI OSes** (run on Windows locally too): the
three `classify_*` functions, `MeteredStatus::on_metered`, `from_u8`,
`reachable_hint` - the full decision logic.
- **Runtime-tested on the Windows CI runner** (and locally):
`detect_metered()` real COM read is total
(`windows_detect_metered_is_total`); the existing `RealPowerSource`
construction tests exercise the read path.
- **Runtime-tested on the macOS CI runner**:
`macos_monitor_start_and_read_is_total` starts the real `NWPathMonitor`
and reads its cache - the only runtime exercise of the Network.framework
/ `block2` / libdispatch FFI (a wrong signature or bad link fails here).
- **Compile-only (CI matrix)**: the macOS FFI adapter body and the Linux
zbus D-Bus adapter body. There is no metered hardware in CI, and
NetworkManager may be absent on the Linux runner, so the Linux read's
happy path is not asserted in CI - the D-Bus/classification split keeps
the classification (the part with real logic) fully covered, and the
adapter is a thin, safe-defaulting wrapper.
APIs were taken from documented sources (zbus proxy/blocking docs; Apple
Network.framework; objc2/block2 encoding), not guessed.
## Nothing deferred
macOS `isExpensive`/`isConstrained` is the documented, feasible proxy
(macOS exposes no literal metered bit), so #32 is fully addressed rather
than scope-reduced.
Closes #32.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>1 parent fb5ea27 commit c475ac1
8 files changed
Lines changed: 536 additions & 192 deletions
File tree
- crates/driven-power
- src
- design
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
20 | | - | |
21 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
42 | 44 | | |
43 | 45 | | |
44 | 46 | | |
45 | 47 | | |
46 | | - | |
47 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
48 | 53 | | |
49 | 54 | | |
| 55 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
35 | 42 | | |
36 | 43 | | |
37 | 44 | | |
| |||
47 | 54 | | |
48 | 55 | | |
49 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
50 | 62 | | |
51 | | - | |
| 63 | + | |
52 | 64 | | |
53 | 65 | | |
54 | 66 | | |
| |||
69 | 81 | | |
70 | 82 | | |
71 | 83 | | |
72 | | - | |
| 84 | + | |
| 85 | + | |
73 | 86 | | |
74 | 87 | | |
75 | 88 | | |
| |||
81 | 94 | | |
82 | 95 | | |
83 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
84 | 118 | | |
85 | 119 | | |
86 | 120 | | |
| |||
92 | 126 | | |
93 | 127 | | |
94 | 128 | | |
95 | | - | |
96 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
97 | 134 | | |
98 | | - | |
99 | 135 | | |
100 | 136 | | |
101 | 137 | | |
102 | | - | |
103 | | - | |
| 138 | + | |
| 139 | + | |
104 | 140 | | |
105 | 141 | | |
106 | 142 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
| 31 | + | |
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
59 | 63 | | |
60 | 64 | | |
61 | 65 | | |
62 | 66 | | |
63 | 67 | | |
64 | 68 | | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
65 | 74 | | |
66 | | - | |
| 75 | + | |
| 76 | + | |
67 | 77 | | |
68 | 78 | | |
69 | 79 | | |
70 | 80 | | |
| 81 | + | |
71 | 82 | | |
72 | 83 | | |
73 | 84 | | |
| |||
79 | 90 | | |
80 | 91 | | |
81 | 92 | | |
| 93 | + | |
82 | 94 | | |
83 | 95 | | |
84 | 96 | | |
85 | 97 | | |
86 | 98 | | |
87 | | - | |
| 99 | + | |
88 | 100 | | |
89 | 101 | | |
90 | 102 | | |
| |||
107 | 119 | | |
108 | 120 | | |
109 | 121 | | |
110 | | - | |
111 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
112 | 125 | | |
113 | | - | |
114 | 126 | | |
115 | 127 | | |
116 | 128 | | |
117 | | - | |
118 | | - | |
| 129 | + | |
| 130 | + | |
119 | 131 | | |
120 | 132 | | |
121 | 133 | | |
| |||
0 commit comments