misc changes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2023-04-22 19:28:38 +02:00
parent 699d12324b
commit f444137684
3 changed files with 39 additions and 17 deletions

View File

@ -17,7 +17,7 @@ use std::path::Path;
pub const GIT_VERSION: &str = git_version!(); pub const GIT_VERSION: &str = git_version!();
const MASTERSERVER: &str = "ipbl.paulbsd.com"; const MASTERSERVER: &str = "ipbl.paulbsd.com";
const WSSUBSCRIPTION: &str = "ipbl"; const WSSUBSCRIPTION: &str = "ipbl";
const CONFIG_RETRY: u64 = 10; const CONFIG_RETRY: u64 = 5;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Context { pub struct Context {
@ -131,18 +131,29 @@ impl Context {
if cfg!(test) { if cfg!(test) {
return Ok(()); return Ok(());
} }
let mut last_in_err = false;
loop { loop {
match self.cfg.load(self.to_owned()).await { match self.cfg.load(self.to_owned()).await {
Ok(()) => { Ok(()) => {
if last_in_err {
println!("loaded config");
}
break; break;
} }
Err(err) => { Err(err) => {
println!("error loading config: {err}, retrying in {CONFIG_RETRY} secs"); println!("error loading config: {err}, retrying in {CONFIG_RETRY} secs");
last_in_err = true;
sleep_s(CONFIG_RETRY).await; sleep_s(CONFIG_RETRY).await;
} }
}; };
} }
if last_in_err {
println!("creating sas");
}
self.create_sas().await?; self.create_sas().await?;
if last_in_err {
println!("created sas");
}
Ok(()) Ok(())
} }

View File

@ -74,11 +74,11 @@ pub async fn run() {
ipdata: ip_to_send, ipdata: ip_to_send,
}; };
if !send_to_ipbl_websocket(&mut wssocketrr, &ipe).await { if !send_to_ipbl_websocket(&mut wssocketrr, &ipe).await {
drop(ctx);
wssocketrr = websocketreqrep(&ctxwsrr).await; wssocketrr = websocketreqrep(&ctxwsrr).await;
break; break;
} }
} }
drop(ctx);
continue continue
} }
@ -98,8 +98,8 @@ pub async fn run() {
}; };
send_to_ipbl_api(&ctx.client, &ctx.flags.server, &ipe).await; send_to_ipbl_api(&ctx.client, &ctx.flags.server, &ipe).await;
let status = send_to_ipbl_websocket(&mut wssocketrr, &ipe).await; let status = send_to_ipbl_websocket(&mut wssocketrr, &ipe).await;
if !status {
drop(ctx); drop(ctx);
if !status {
wssocketrr = websocketreqrep(&ctxwsrr).await; wssocketrr = websocketreqrep(&ctxwsrr).await;
continue; continue;
} }

View File

@ -102,22 +102,33 @@ pub async fn send_to_ipbl_websocket(
) -> bool { ) -> bool {
let msg = format!("{val}", val = serde_json::to_string(&ip).unwrap()); let msg = format!("{val}", val = serde_json::to_string(&ip).unwrap());
if ws.can_write() {
match ws.write_message(Message::Text(msg)) { match ws.write_message(Message::Text(msg)) {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
println!("err send write: {e:?}"); println!("err send read: {e:?}");
ws.close(None).unwrap(); return handle_websocket_error(ws);
return false;
} }
}; };
} else {
return handle_websocket_error(ws);
};
if ws.can_read() {
match ws.read_message() { match ws.read_message() {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
println!("err send read: {e:?}"); println!("err send read: {e:?}");
ws.close(None).unwrap(); return handle_websocket_error(ws);
return false;
} }
}; };
} else {
return handle_websocket_error(ws);
};
true true
} }
fn handle_websocket_error(ws: &mut WebSocket<MaybeTlsStream<TcpStream>>) -> bool {
ws.close(None).unwrap();
return false;
}