From a0dc917dfe6649e0428821606c06ef6bcb28a26d Mon Sep 17 00:00:00 2001 From: asonix Date: Fri, 18 Nov 2022 19:00:45 -0600 Subject: [PATCH] Prioritize open-reg servers on index page --- src/routes/index.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/routes/index.rs b/src/routes/index.rs index ea2634e..4e141d7 100644 --- a/src/routes/index.rs +++ b/src/routes/index.rs @@ -1,19 +1,40 @@ use crate::{ config::Config, - data::State, + data::{Node, State}, error::{Error, ErrorKind}, }; use actix_web::{web, HttpResponse}; use rand::{seq::SliceRandom, thread_rng}; use std::io::BufWriter; +fn open_reg(node: &Node) -> bool { + node.instance + .as_ref() + .map(|i| i.reg) + .or_else(|| node.info.as_ref().map(|i| i.reg)) + .unwrap_or(false) +} + #[tracing::instrument(name = "Index", skip(config, state))] pub(crate) async fn route( state: web::Data, config: web::Data, ) -> Result { let mut nodes = state.node_cache().nodes().await?; - nodes.shuffle(&mut thread_rng()); + + nodes.sort_by(|lhs, rhs| match (open_reg(lhs), open_reg(rhs)) { + (true, true) | (false, false) => std::cmp::Ordering::Equal, + (true, false) => std::cmp::Ordering::Less, + (false, true) => std::cmp::Ordering::Greater, + }); + + if let Some((i, _)) = nodes.iter().enumerate().find(|(_, node)| !open_reg(node)) { + nodes[..i].shuffle(&mut thread_rng()); + nodes[i..].shuffle(&mut thread_rng()); + } else { + nodes.shuffle(&mut thread_rng()); + } + let mut buf = BufWriter::new(Vec::new()); crate::templates::index(&mut buf, &nodes, &config)?;