Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flitering of versions. #18

Merged
merged 13 commits into from Nov 1, 2022
19 changes: 16 additions & 3 deletions negy-gateway/src/main.rs
Expand Up @@ -10,6 +10,7 @@ use negy_node_pool::req::ListNodeResponse;
use openssl::rsa::Rsa;
use std::sync::{Arc, RwLock};
use tokio::net::{TcpListener, TcpStream};
use semver::Version;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand All @@ -24,6 +25,8 @@ struct Args {
hops: usize,
#[clap(short, long, value_parser)]
auth_token: Option<String>,
min_version: Option<String>,

tbrand marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line added

}

async fn spawn_inner(
Expand All @@ -41,8 +44,7 @@ async fn spawn_inner(

Ok(())
}

async fn fetch_nodes_unselected(node_pool_endpoint: &str) -> Result<Vec<NodeUnselected>> {
async fn fetch_nodes_unselected(node_pool_endpoint: &str, min_version: &Option<String>) -> Result<Vec<NodeUnselected>> {
let res = reqwest::Client::new()
.get(format!("{}/list", node_pool_endpoint))
.send()
Expand All @@ -58,6 +60,13 @@ async fn fetch_nodes_unselected(node_pool_endpoint: &str) -> Result<Vec<NodeUnse
name: n.name,
version: n.version,
})
.filter(|n| {
if let Some(min_version) = &min_version {
Version::parse(&n.version).unwrap() >= Version::parse(min_version).unwrap()
} else {
true
}
})
.collect();

Ok(nodes_unselected)
Expand All @@ -68,14 +77,16 @@ async fn spawn(
node_pool_endpoint: String,
hops: usize,
auth_token: Option<String>,
min_version: Option<String>,
) -> Result<()> {

tbrand marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line added

let listed_nodes: Arc<RwLock<Vec<NodeUnselected>>> = Arc::new(RwLock::new(Vec::new()));
let listed_nodes_fetch = listed_nodes.clone();
let listed_nodes_accept = listed_nodes.clone();

tokio::spawn(async move {
loop {
match fetch_nodes_unselected(&node_pool_endpoint).await {
match fetch_nodes_unselected(&node_pool_endpoint, &min_version).await {
Ok(nodes_unselected) => {
info!("fetched {} nodes", nodes_unselected.len());
*listed_nodes_fetch.write().unwrap() = nodes_unselected;
Expand Down Expand Up @@ -124,11 +135,13 @@ async fn main() -> Result<()> {

let listener = TcpListener::bind(bind_addr).await?;


tbrand marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

empty line added

spawn(
listener,
args.node_pool_endpoint,
args.hops,
args.auth_token,
args.min_version
)
.await?;

Expand Down