Tweak logs and attempt to avoid races around removing nodes (#504)

* Tweak logs and attempt to avoid races around removing nodes

* wrapping_add in assign_id
This commit is contained in:
James Wilson
2022-10-10 13:12:07 +01:00
committed by GitHub
parent 41c93a8a19
commit e7d15d03b3
5 changed files with 53 additions and 52 deletions
+7 -2
View File
@@ -47,7 +47,9 @@ where
pub fn assign_id(&mut self, details: Details) -> Id {
let this_id = self.current_id;
self.current_id += 1;
// It's very unlikely we'll ever overflow the ID limit, but in case we do,
// a wrapping_add will almost certainly be fine:
self.current_id = self.current_id.wrapping_add(1);
self.mapping.insert(this_id, details);
this_id.into()
}
@@ -73,7 +75,10 @@ where
}
pub fn clear(&mut self) {
*self = AssignId::new();
// Leave the `current_id` as-is. Why? To avoid reusing IDs and risking
// race conditions where old messages can accidentally screw with new nodes
// that have been assigned the same ID.
self.mapping = BiMap::new();
}
pub fn iter(&self) -> impl Iterator<Item = (Id, &Details)> {