code review results
Signed-off-by: Sienna Meridian Satterwhite <sienna@r3t.io>
This commit is contained in:
@@ -18,6 +18,8 @@ use std::collections::{
|
||||
use bevy::prelude::*;
|
||||
|
||||
use crate::networking::{
|
||||
GossipBridge,
|
||||
NodeVectorClock,
|
||||
messages::{
|
||||
EntityDelta,
|
||||
SyncMessage,
|
||||
@@ -27,8 +29,6 @@ use crate::networking::{
|
||||
NodeId,
|
||||
VectorClock,
|
||||
},
|
||||
GossipBridge,
|
||||
NodeVectorClock,
|
||||
};
|
||||
|
||||
/// Maximum operations to keep per entity (prevents unbounded growth)
|
||||
@@ -62,7 +62,8 @@ struct LogEntry {
|
||||
/// - Max operation age: `MAX_OP_AGE_SECS` (300 seconds / 5 minutes)
|
||||
/// - Max entities: `MAX_ENTITIES` (10,000)
|
||||
///
|
||||
/// When limits are exceeded, oldest operations/entities are pruned automatically.
|
||||
/// When limits are exceeded, oldest operations/entities are pruned
|
||||
/// automatically.
|
||||
#[derive(Resource)]
|
||||
pub struct OperationLog {
|
||||
/// Map from entity ID to list of recent operations
|
||||
@@ -88,7 +89,11 @@ impl OperationLog {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use lib::networking::{OperationLog, EntityDelta, VectorClock};
|
||||
/// use lib::networking::{
|
||||
/// EntityDelta,
|
||||
/// OperationLog,
|
||||
/// VectorClock,
|
||||
/// };
|
||||
/// use uuid::Uuid;
|
||||
///
|
||||
/// let mut log = OperationLog::new();
|
||||
@@ -119,7 +124,10 @@ impl OperationLog {
|
||||
timestamp: std::time::Instant::now(),
|
||||
};
|
||||
|
||||
let log = self.logs.entry(delta.entity_id).or_insert_with(VecDeque::new);
|
||||
let log = self
|
||||
.logs
|
||||
.entry(delta.entity_id)
|
||||
.or_insert_with(VecDeque::new);
|
||||
log.push_back(entry);
|
||||
self.total_ops += 1;
|
||||
|
||||
@@ -134,9 +142,7 @@ impl OperationLog {
|
||||
fn find_oldest_entity(&self) -> Option<uuid::Uuid> {
|
||||
self.logs
|
||||
.iter()
|
||||
.filter_map(|(entity_id, log)| {
|
||||
log.front().map(|entry| (*entity_id, entry.timestamp))
|
||||
})
|
||||
.filter_map(|(entity_id, log)| log.front().map(|entry| (*entity_id, entry.timestamp)))
|
||||
.min_by_key(|(_, timestamp)| *timestamp)
|
||||
.map(|(entity_id, _)| entity_id)
|
||||
}
|
||||
@@ -189,9 +195,7 @@ impl OperationLog {
|
||||
|
||||
for log in self.logs.values_mut() {
|
||||
let before_len = log.len();
|
||||
log.retain(|entry| {
|
||||
now.duration_since(entry.timestamp) < max_age
|
||||
});
|
||||
log.retain(|entry| now.duration_since(entry.timestamp) < max_age);
|
||||
pruned_count += before_len - log.len();
|
||||
}
|
||||
|
||||
@@ -226,7 +230,10 @@ impl Default for OperationLog {
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use lib::networking::{build_sync_request, VectorClock};
|
||||
/// use lib::networking::{
|
||||
/// VectorClock,
|
||||
/// build_sync_request,
|
||||
/// };
|
||||
/// use uuid::Uuid;
|
||||
///
|
||||
/// let node_id = Uuid::new_v4();
|
||||
@@ -258,8 +265,7 @@ pub fn build_missing_deltas(deltas: Vec<EntityDelta>) -> VersionedMessage {
|
||||
/// use bevy::prelude::*;
|
||||
/// use lib::networking::handle_sync_requests_system;
|
||||
///
|
||||
/// App::new()
|
||||
/// .add_systems(Update, handle_sync_requests_system);
|
||||
/// App::new().add_systems(Update, handle_sync_requests_system);
|
||||
/// ```
|
||||
pub fn handle_sync_requests_system(
|
||||
bridge: Option<Res<GossipBridge>>,
|
||||
@@ -296,10 +302,10 @@ pub fn handle_sync_requests_system(
|
||||
} else {
|
||||
debug!("No missing deltas for node {}", requesting_node);
|
||||
}
|
||||
}
|
||||
},
|
||||
| _ => {
|
||||
// Not a SyncRequest, ignore
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -324,17 +330,14 @@ pub fn handle_missing_deltas_system(world: &mut World) {
|
||||
|
||||
// Apply each delta
|
||||
for delta in deltas {
|
||||
debug!(
|
||||
"Applying missing delta for entity {:?}",
|
||||
delta.entity_id
|
||||
);
|
||||
debug!("Applying missing delta for entity {:?}", delta.entity_id);
|
||||
|
||||
crate::networking::apply_entity_delta(&delta, world);
|
||||
}
|
||||
}
|
||||
},
|
||||
| _ => {
|
||||
// Not MissingDeltas, ignore
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -394,10 +397,7 @@ pub fn prune_operation_log_system(
|
||||
let after = operation_log.total_operations();
|
||||
|
||||
if before != after {
|
||||
debug!(
|
||||
"Pruned operation log: {} ops -> {} ops",
|
||||
before, after
|
||||
);
|
||||
debug!("Pruned operation log: {} ops -> {} ops", before, after);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -489,7 +489,7 @@ mod tests {
|
||||
} => {
|
||||
assert_eq!(req_node_id, node_id);
|
||||
assert_eq!(vector_clock, clock);
|
||||
}
|
||||
},
|
||||
| _ => panic!("Expected SyncRequest"),
|
||||
}
|
||||
}
|
||||
@@ -507,7 +507,7 @@ mod tests {
|
||||
| SyncMessage::MissingDeltas { deltas } => {
|
||||
assert_eq!(deltas.len(), 1);
|
||||
assert_eq!(deltas[0].entity_id, entity_id);
|
||||
}
|
||||
},
|
||||
| _ => panic!("Expected MissingDeltas"),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user