get_best_relays() now takes our rank and the historical success rate into account.

This commit is contained in:
Mike Dilger 2023-08-10 16:50:18 +12:00
parent e88cfe5788
commit 1e2d627526
2 changed files with 19 additions and 2 deletions

View File

@ -49,7 +49,8 @@ impl PersonRelay {
} }
} }
// This ranks the relays that a person writes to // This ranks the relays that a person writes to, but does not consider local
// factors such as our relay rank or the success rate of the relay.
pub fn write_rank(mut dbprs: Vec<PersonRelay>) -> Vec<(RelayUrl, u64)> { pub fn write_rank(mut dbprs: Vec<PersonRelay>) -> Vec<(RelayUrl, u64)> {
let now = Unixtime::now().unwrap().0 as u64; let now = Unixtime::now().unwrap().0 as u64;
let mut output: Vec<(RelayUrl, u64)> = Vec::new(); let mut output: Vec<(RelayUrl, u64)> = Vec::new();
@ -106,7 +107,8 @@ impl PersonRelay {
output output
} }
// This ranks the relays that a person reads from // This ranks the relays that a person reads from, but does not consider local
// factors such as our relay rank or the success rate of the relay.
pub fn read_rank(mut dbprs: Vec<PersonRelay>) -> Vec<(RelayUrl, u64)> { pub fn read_rank(mut dbprs: Vec<PersonRelay>) -> Vec<(RelayUrl, u64)> {
let now = Unixtime::now().unwrap().0 as u64; let now = Unixtime::now().unwrap().0 as u64;
let mut output: Vec<(RelayUrl, u64)> = Vec::new(); let mut output: Vec<(RelayUrl, u64)> = Vec::new();

View File

@ -1794,11 +1794,26 @@ impl Storage {
dir: Direction, dir: Direction,
) -> Result<Vec<(RelayUrl, u64)>, Error> { ) -> Result<Vec<(RelayUrl, u64)>, Error> {
let person_relays = self.get_person_relays(pubkey)?; let person_relays = self.get_person_relays(pubkey)?;
// Note: the following read_rank and write_rank do not consider our own
// rank or the success rate.
let mut ranked_relays = match dir { let mut ranked_relays = match dir {
Direction::Write => PersonRelay::write_rank(person_relays), Direction::Write => PersonRelay::write_rank(person_relays),
Direction::Read => PersonRelay::read_rank(person_relays), Direction::Read => PersonRelay::read_rank(person_relays),
}; };
// Modulate these scores with our local rankings
for ranked_relay in ranked_relays.iter_mut() {
match self.read_relay(&ranked_relay.0)? {
None => ranked_relay.1 = 0,
Some(relay) => {
let success_rate = relay.success_rate();
let rank = (relay.rank as f32 * success_rate * 0.66666) as u64;
ranked_relay.1 *= rank;
}
}
}
let num_relays_per_person = GLOBALS.settings.read().num_relays_per_person as usize; let num_relays_per_person = GLOBALS.settings.read().num_relays_per_person as usize;
// If we can't get enough of them, extend with some of our relays // If we can't get enough of them, extend with some of our relays