Skip to content

Commit

Permalink
feat: retry tcpapps bind after failure
Browse files Browse the repository at this point in the history
  • Loading branch information
de-sh committed Jun 26, 2024
1 parent 7d2996b commit 768ddff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
16 changes: 14 additions & 2 deletions uplink/src/collector/tcpjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use thiserror::Error;
use tokio::net::{TcpListener, TcpStream};
use tokio::select;
use tokio::task::{spawn, JoinHandle};
use tokio::time::{sleep, Duration};
use tokio_stream::StreamExt;
use tokio_util::codec::{Framed, LinesCodec, LinesCodecError};

Expand Down Expand Up @@ -49,9 +50,20 @@ impl TcpJson {
TcpJson { name, config, bridge, actions_rx }
}

pub async fn start(self) -> Result<(), Error> {
pub async fn start(self) {
let addr = format!("0.0.0.0:{}", self.config.port);
let listener = TcpListener::bind(&addr).await?;
let listener = loop {
match TcpListener::bind(&addr).await {
Ok(s) => break s,
Err(e) => {
error!(
"Couldn't bind to port: {}; Error = {e}; retrying in 5s",
self.config.port
);
sleep(Duration::from_secs(5)).await;
}
}
};
let mut handle: Option<JoinHandle<()>> = None;

info!("Waiting for app = {} to connect on {addr}", self.name);
Expand Down
6 changes: 2 additions & 4 deletions uplink/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub type ReloadHandle =
use uplink::config::{AppConfig, Config, StreamConfig, MAX_BATCH_SIZE};
use uplink::{simulator, spawn_named_thread, TcpJson, Uplink};

const DEFAULT_CONFIG: &str = r#"
const DEFAULT_CONFIG: &str = r#"
[mqtt]
max_packet_size = 256000
max_inflight = 100
Expand Down Expand Up @@ -355,9 +355,7 @@ fn main() -> Result<(), Error> {
rt.block_on(async {
for app in tcpapps {
tokio::task::spawn(async move {
if let Err(e) = app.start().await {
error!("App failed. Error = {e}");
}
app.start().await;
});
}

Expand Down

0 comments on commit 768ddff

Please sign in to comment.