use std::sync::Arc;
use tracing::trace;
use crate::selector::server_stat_man::ServerStatMan;
use crate::selector::uri_selector::UriSelector;
const NUM_URI: usize = 10;
const SPEED_THRESHOLD: u64 = 20 * 1024;
pub fn extract_host_and_protocol(uri: &str) -> Option<(String, String)> {
let uri = uri.trim();
let scheme_end = uri.find("://")?;
let protocol = &uri[..scheme_end];
let after_scheme = &uri[scheme_end + 3..];
let host_part = if let Some(slash_idx) = after_scheme.find('/') {
&after_scheme[..slash_idx]
} else {
after_scheme
};
if host_part.is_empty() || protocol.is_empty() {
return None;
}
Some((host_part.to_string(), protocol.to_string()))
}
pub struct FeedbackUriSelector {
stat_man: Arc<ServerStatMan>,
}
impl FeedbackUriSelector {
pub fn new(stat_man: Arc<ServerStatMan>) -> Self {
Self { stat_man }
}
fn select_one(&self, uris: &[String], used_hosts: &[(usize, String)]) -> Option<usize> {
if uris.is_empty() {
return None;
}
for (cuid, host) in used_hosts {
trace!(cuid, host = %host, "FeedbackURISelector: UsedHost");
}
if let Some(idx) = self.select_faster(uris, used_hosts) {
trace!(uri = %uris[idx], "FeedbackURISelector selected (faster)");
return Some(idx);
}
trace!("No URI returned from selectFaster()");
if let Some(idx) = self.select_rarer(uris, used_hosts) {
trace!(uri = %uris[idx], "FeedbackURISelector selected (rarer)");
return Some(idx);
}
trace!("FeedbackURISelector: no URI selected");
None
}
fn select_faster(&self, uris: &[String], used_hosts: &[(usize, String)]) -> Option<usize> {
let used_set: std::collections::HashSet<&str> =
used_hosts.iter().map(|(_, h)| h.as_str()).collect();
let mut fast_cands: Vec<(u64, usize)> = Vec::new(); let mut norm_cands: Vec<usize> = Vec::new();
for (idx, uri) in uris.iter().enumerate() {
if fast_cands.len() >= NUM_URI {
break;
}
let (host, protocol) = match extract_host_and_protocol(uri) {
Some(hp) => hp,
None => continue, };
if used_set.contains(host.as_str()) {
trace!(uri = %uri, "is in usedHosts, not considered");
continue;
}
let stat = self.stat_man.find_stat_by_protocol(&host, &protocol);
match stat {
None => {
norm_cands.push(idx);
}
Some(s) if s.is_ok() => {
if s.get_download_speed() > SPEED_THRESHOLD {
fast_cands.push((s.get_download_speed(), idx));
} else {
norm_cands.push(idx);
}
}
Some(_) => {
trace!(uri = %uri, "Error server not considered");
}
}
}
if fast_cands.is_empty() {
if norm_cands.is_empty() {
None
} else {
trace!("Selected from normCands");
Some(norm_cands[0])
}
} else {
trace!("Selected from fastCands");
fast_cands.sort_by_key(|b| std::cmp::Reverse(b.0));
Some(fast_cands[0].1)
}
}
fn select_rarer(&self, uris: &[String], used_hosts: &[(usize, String)]) -> Option<usize> {
let mut cands: Vec<(String, usize)> = Vec::new();
for (idx, uri) in uris.iter().enumerate() {
let (host, protocol) = match extract_host_and_protocol(uri) {
Some(hp) => hp,
None => continue,
};
if let Some(stat) = self.stat_man.find_stat_by_protocol(&host, &protocol)
&& !stat.is_ok()
{
trace!(uri = %uri, "Error not considered (rarer)");
continue;
}
cands.push((host, idx));
}
for (_, used_host) in used_hosts {
for (cand_host, cand_idx) in &cands {
if cand_host == used_host {
return Some(*cand_idx);
}
}
}
cands.first().map(|(_, idx)| *idx).or({
if !uris.is_empty() { Some(0) } else { None }
})
}
}
impl UriSelector for FeedbackUriSelector {
fn select(&self, uris: &[String], used_hosts: &[(usize, String)]) -> Option<usize> {
self.select_one(uris, used_hosts)
}
fn tune_command(&self, _uris: &[String], _speed: u64) {
}
fn reset(&self) {
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_host_and_protocol_http() {
let (host, proto) = extract_host_and_protocol("http://example.com/path").unwrap();
assert_eq!(host, "example.com");
assert_eq!(proto, "http");
}
#[test]
fn test_extract_host_and_protocol_invalid() {
assert!(extract_host_and_protocol("not-a-uri").is_none());
assert!(extract_host_and_protocol("").is_none());
}
}
#[cfg(test)]
mod extended_tests {
use super::*;
use crate::selector::server_stat_man::ServerStatMan;
include!("feedback_uri_selector_tests.rs");
}