Skip to content

Commit 81b4b3c

Browse files
committedJan 21, 2025··
refactor: [torrust#1195] extract and move method
1 parent cd542dc commit 81b4b3c

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed
 

‎src/core/authentication/key/repository/in_memory.rs

+11
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,15 @@ impl InMemoryKeyRepository {
2727
let mut keys = self.keys.write().await;
2828
keys.clear();
2929
}
30+
31+
/// It resets the authentication keys with a new list of keys.
32+
pub async fn reset_with(&self, peer_keys: Vec<PeerKey>) {
33+
let mut keys_lock = self.keys.write().await;
34+
35+
keys_lock.clear();
36+
37+
for key in peer_keys {
38+
keys_lock.insert(key.key.clone(), key.clone());
39+
}
40+
}
3041
}

‎src/core/authentication/mod.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,18 @@ impl Facade {
239239
self.in_memory_key_repository.remove(key).await;
240240
}
241241

242-
/// The `Tracker` stores the authentication keys in memory and in the database.
243-
/// In case you need to restart the `Tracker` you can load the keys from the database
244-
/// into memory with this function. Keys are automatically stored in the database when they
245-
/// are generated.
242+
/// The `Tracker` stores the authentication keys in memory and in the
243+
/// database. In case you need to restart the `Tracker` you can load the
244+
/// keys from the database into memory with this function. Keys are
245+
/// automatically stored in the database when they are generated.
246246
///
247247
/// # Errors
248248
///
249249
/// Will return a `database::Error` if unable to `load_keys` from the database.
250250
pub async fn load_keys_from_database(&self) -> Result<(), databases::error::Error> {
251251
let keys_from_database = self.db_key_repository.load_keys()?;
252252

253-
self.in_memory_key_repository.clear().await;
254-
255-
for key in keys_from_database {
256-
self.in_memory_key_repository.insert(&key).await;
257-
}
253+
self.in_memory_key_repository.reset_with(keys_from_database).await;
258254

259255
Ok(())
260256
}

0 commit comments

Comments
 (0)
Please sign in to comment.