feat(receiver): 持久化接收机发现端点并支持位置备注信息

This commit is contained in:
zyp
2026-11-30 09:29:33 +08:00
parent 1d6d7a277e
commit 5e761fc064
95 changed files with 15744 additions and 701 deletions
+72 -10
View File
@@ -20,20 +20,37 @@ pub struct Store {
metrics: Metrics,
}
fn refresh(track: &mut Track) {
const SOURCE_HOLD_MS: u64 = 4_000;
fn refresh(track: &mut Track, now_ms: u64) {
let mut sources: Vec<_> = track.sources.values().cloned().collect();
sources.sort_by(|a, b| {
a.received_at_ms
.cmp(&b.received_at_ms)
.then_with(|| a.observation.receiver_id.cmp(&b.observation.receiver_id))
});
let selected = sources
.iter()
.rev()
.find(|s| s.observation.coordinates().is_some())
let old = &track.target.observation;
let primary = track.sources.get(&old.receiver_id);
let fresh_primary = primary.filter(|s| {
s.observation.coordinates().is_some()
&& now_ms.saturating_sub(s.received_at_ms) < SOURCE_HOLD_MS
});
let selected = fresh_primary
.or_else(|| {
sources.iter().rev().find(|s| {
s.observation.coordinates().is_some()
&& now_ms.saturating_sub(s.received_at_ms) < SOURCE_HOLD_MS
})
})
.or_else(|| primary.filter(|s| s.observation.coordinates().is_some()))
.or_else(|| {
sources
.iter()
.rev()
.find(|s| s.observation.coordinates().is_some())
})
.or_else(|| sources.last())
.expect("nonempty source set");
let old = &track.target.observation;
if old.coordinates().is_some() != selected.observation.coordinates().is_some()
|| old.receiver_id != selected.observation.receiver_id
{
@@ -74,7 +91,7 @@ impl Store {
self.changes.push_back((self.metrics.sequence, id));
}
/// Select the latest still-valid position, retaining independent source observations.
/// Keep a healthy position source, retaining independent observations for failover.
pub fn apply(&mut self, observation: Observation, now_ms: u64) -> bool {
if !observation.validate() {
self.metrics.rejected += 1;
@@ -125,7 +142,7 @@ impl Store {
track
.sources
.retain(|_, s| now_ms.saturating_sub(s.received_at_ms) < self.ttl_ms);
refresh(track);
refresh(track, now_ms);
track.target.received_count = track.target.received_count.saturating_add(1);
track.suppressed = false;
if let Some(observer) = &self.observer {
@@ -141,6 +158,22 @@ impl Store {
self.tracks.get(id).map(|track| &track.target.observation)
}
/// Withdraw an alias from the receiver that supplied the confirmed identity.
pub fn remove_source(&mut self, id: &str, receiver_id: &str, now_ms: u64) {
let Some(track) = self.tracks.get_mut(id) else {
return;
};
if track.sources.remove(receiver_id).is_none() {
return;
}
if track.sources.is_empty() {
self.tracks.remove(id);
} else {
refresh(track, now_ms);
}
self.changed(id.to_owned());
}
pub fn expire(&mut self, now_ms: u64) {
let _ = self.expire_with_changes(now_ms);
}
@@ -191,7 +224,7 @@ impl Store {
if track.sources.is_empty() {
removed.push(id.clone());
} else if track.sources.len() != count || track.suppressed {
refresh(track);
refresh(track, now_ms);
track.suppressed = false;
if let Some(observer) = &self.observer {
observer(&track.target.observation, now_ms);
@@ -291,7 +324,7 @@ mod tests {
let frame = store.frame(None, 5);
assert_eq!(frame.targets.len(), 1);
assert_eq!(frame.targets[0].sources.len(), 2);
assert_eq!(frame.targets[0].observation.receiver_id, "b");
assert_eq!(frame.targets[0].observation.receiver_id, "a");
let generation = frame.targets[0].position_generation;
a.sequence = 2;
a.longitude = None;
@@ -321,6 +354,35 @@ mod tests {
store.expire(45);
assert!(store.frame(None, 45).targets.is_empty());
}
#[test]
fn lagging_receivers_do_not_replace_a_healthy_source_and_failover_is_sticky() {
let mut store = Store::new(10, 100, 30_000, 1);
let mut primary = sample("one-aircraft", 1);
primary.receiver_id = "primary".into();
let mut standby = primary.clone();
standby.receiver_id = "standby".into();
standby.longitude = Some(119.);
assert!(store.apply(primary.clone(), 100));
assert!(store.apply(standby.clone(), 200));
primary.sequence += 1;
primary.longitude = Some(120.01);
assert!(store.apply(primary.clone(), 1_000));
standby.sequence += 1;
assert!(store.apply(standby.clone(), 1_500));
let live = store.frame(None, 1_500);
assert_eq!(live.targets.len(), 1);
assert_eq!(live.targets[0].sources.len(), 2);
assert_eq!(live.targets[0].observation, primary);
let generation = live.targets[0].position_generation;
standby.sequence += 1;
assert!(store.apply(standby.clone(), 5_000));
primary.sequence += 1;
assert!(store.apply(primary, 5_100));
let live = store.frame(None, 5_100);
assert_eq!(live.targets[0].observation, standby);
assert_eq!(live.targets[0].position_generation, generation + 1);
}
fn sample(id: &str, sequence: u64) -> Observation {
Observation {
target_id: id.into(),