Repository navigation
Add time scheduling with debouncing for gratuitous link statuses - #145
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Activity decay can miscount traffic or panic on accepted period values, and the promised tunable for disabling early link-status broadcasts is missing.
4 open issues
What changed in this PR
This PR adds early Link Status broadcasts to help routers establish neighbor links sooner, addressing part of #143. It also moves activity counters toward elapsed-time accounting so broadcasts need not follow a fixed cadence.
Changes:
- Introduces fractional activity counters and time-based decay for neighbors and routes.
- Adds a reusable debouncer for Link Status and many-to-one route-request scheduling.
- Adds bootstrap jitter settings and tests for scheduling, decay, and neighbor discovery.
| File | Description |
|---|---|
crates/ziggurat-zigbee/src/time.rs |
Adds a default value for core timestamps. |
crates/ziggurat-zigbee/src/nwk/routing/tests.rs |
Updates routing calls and tests inactive-route decay. |
crates/ziggurat-zigbee/src/nwk/routing.rs |
Migrates route activity to fractional counters and elapsed-time decay. |
crates/ziggurat-zigbee/src/nwk/neighbors.rs |
Tracks early-response needs and migrates neighbor activity counters. |
crates/ziggurat-zigbee/src/nwk/activity.rs |
Adds fractional activity counters, decay calculations, and tests. |
crates/ziggurat-zigbee/src/nwk.rs |
Exports the activity module. |
crates/ziggurat-zigbee/src/lib.rs |
Exports the debouncer module. |
crates/ziggurat-zigbee/src/debouncer.rs |
Adds reusable deadline scheduling with tests. |
crates/ziggurat-zigbee/src/constants.rs |
Defines bootstrap jitter settings. |
crates/ziggurat-protocol/src/bridge.rs |
Supplies activity initialization values when restoring state. |
crates/ziggurat-driver/src/zigbee_stack/sends.rs |
Passes the router age limit during discovery retries. |
crates/ziggurat-driver/src/zigbee_stack/route.rs |
Uses debounced route advertisements and updated routing APIs. |
crates/ziggurat-driver/src/zigbee_stack/nwk.rs |
Updates route cleanup and discovery calls. |
crates/ziggurat-driver/src/zigbee_stack/neighbor.rs |
Schedules early Link Status broadcasts and elapsed-time decay. |
crates/ziggurat-driver/src/zigbee_stack/joining.rs |
Supplies activity initialization values when attaching children. |
crates/ziggurat-driver/src/zigbee_stack.rs |
Stores debouncers and adds shared deadline-waiting logic. |
Review effort: Balanced
… generic `Debouncer`
a289197 to
52e1bf9
Compare
|
Tested #144 + this PR on my bench, without #143:
Holding early link statuses until the first periodic one fixed it in both runs. |
zigpy-review-bot
left a comment
There was a problem hiding this comment.
Post-merge review. The debouncer, the wake-up handling in wait_for_debouncer and the MTORR migration hold up, and two of the four Copilot findings from the earlier head are fully fixed: decay now runs before every update, and the decay timestamp moves past all elapsed steps. Three problems remain on dev. For the first, a fix is already in an open PR; fix PRs for the other two are linked below.
1. A restart makes routers not heard yet drop their link to us (regression)
t3hk0d3's bench report above is right, and the mechanism is in the code:
- After a restart, restored router children have no LQI samples and sibling routers are not in the table yet, and
is_in_link_statuslists only routers with a sample. - The first router heard makes an early link status due, and 0.5–1 s later we send a complete list holding only the routers heard so far.
- A router left out of a complete list sets its outgoing cost to us to 0 (spec 3.6.4.4.2). On a Ziggurat router that reports
lost_link, invalidates its routes through us and drops our route requests (outgoing_cost == 0inroute.rs) until we have heard it and sent another link status.
Before this PR the first non-empty link status went out a full period after the start, when nearly every router had been heard. A probe test against Neighbors confirms the sequence: two restored routers, the first one heard makes the link status due, the list holds it alone, and the other router ends at outgoing cost 0 with lost_link set.
The fix exists: commit 315d7b6 in #147 holds every early link status until one period after the start (Debouncer::hold, unit-tested, with its own tunable), and the bench numbers there show cost 3 from the first discovery with it. That commit doesn't depend on the route refresh in the rest of #147, so it could land on its own while the route refresh vs source routing question is settled. Two trade-offs to keep in mind. Routers that zero their cost on our empty startup link status (spec behaviour, and Ziggurat routers before #144) get it back today about a second after we first hear them (a sibling router: after its first link status); with the hold they wait for the first periodic link status again, as before this PR. And because the hold sits in Debouncer::request, it also delays the spec's answer to an unknown router's empty list (3.6.4.4.2) during that first period.
2. hack_early_link_status_for_unknown_routers only gates one of the two paths
handle_link_status checks the tunable, but maybe_recompute_lqa asks for the same early link status through record_lqa without checking it. A restored or newly joined router child (or a router whose samples age() cleared, when it is heard again) takes the second path, because its first frame of any kind reaches record_lqa before on_link_status. With the tunable cleared, a restart still sends early link statuses as restored router children are heard (requests close together share one), contrary to its doc. Fix: #151.
3. link_status_period = 0 aborts the stack
The 1–255 µs case is fixed, but zero still reaches elapsed * steps_per_count / period in take_decay_steps. The setter accepts it, and decay now runs on the receive path, so the next received link status or relayed unicast panics, and in a release build (panic = "abort") the process exits. Fix: #152.
Notes, no change asked
- Nothing reads
router_inbound_activity,router_outbound_activityorrecent_activityyet, so the time-based decay and its new start value have no observable effect today. The description's "converting them to u8 when emitting values" has no emitter on dev yet. - "This is still rate limited" comes down to the jitter. The early link status uses a zero minimum interval, so each newly listed router can get its own broadcast. A minimum interval of a second or two would bound both the new-router path and the answers to repeated empty lists.
Debouncer::requestbefore the firstfired()is dropped (nextis 0). For MTORR that is harmless (the first request goes out at the end of the startup wait anyway). For link status it only matters if a frame is handled before the link status task is first polled: an answer to an empty list requested then waits for the periodic link status.
Verified (9 checks)
- Read the full diff and the surrounding driver code at 34f6e5f, against spec R23.2 3.6.4.4.1/3.6.4.4.2.
cargo test --workspace --all-featuresat 34f6e5f: 199 passed.- Probe unit tests (not committed) for the restart sequence,
take_decay_stepswith a zero period, andTunables::set("link_status_period", 0)returningOk. - Checked
wait_for_debounceragainst the runtime'sNotify: one stored permit, a single waiter, no await between the wait returning andfired(). A request made duringdelivered().awaitis picked up on the next loop turn. - MTORR timing matches the parent after the first advertisement: a kick fires at
max(now, last_fired + mtorr_min_interval), otherwise everymtorr_max_interval. A kick during the startup wait no longer adds a second advertisement one min interval after the first. - Jitter arithmetic cannot panic for any accepted tunable value (
min > maxgivesmin). - Copilot findings 2 and 4 from the earlier head are fixed; finding 1 is half fixed (item 2 above) and finding 3 is fixed except for zero (item 3).
- Each fix PR runs
cargo fmt,cargo clippywith the CI flags andcargo test --workspace. - Commit 315d7b6 from #147 cherry-picks cleanly onto dev on its own; the workspace builds and the
ziggurat-zigbeetests pass with it.

Neighbor and route activity counters were implemented in a spec-literal manner: every time they are used, a number goes up. When a link status broadcast goes out, a number goes down. This couples an activity counter's time-based component to a fixed cadence and prevents the activity counter from being refreshed early due to double or non-counting. I've migrated the activity counters to still be mostly spec-compatible by storing them as u16 (in steps of 1/256) and converting them to u8 when emitting values, which allows them to be driven by time instead of by activity.
The purpose of the above migration is to enable us to send gratuitous Link Status responses to new routers coming online. This is done according to the spec. I've also added a slight deviation:
hack_early_link_status_for_unknown_routers(true by default) to also send a gratuitous Link Status response when an unknown router comes online. This is still rate limited.A shared
Debounceris now used by both MTORR and gratuitous Link Status broadcasts. In the future, more of this logic will be made sans-io to be better unit testable and to shrink the async runtime's state.Addresses some of #143