|
@@ -29,7 +29,7 @@ impl Relayer {
|
|
pub fn new<F>(
|
|
pub fn new<F>(
|
|
broadcast_to_listeners: mpsc::Sender<(Response, String)>,
|
|
broadcast_to_listeners: mpsc::Sender<(Response, String)>,
|
|
sent_messages: Arc<RwLock<Vec<Request>>>,
|
|
sent_messages: Arc<RwLock<Vec<Request>>>,
|
|
- connection_retries: u16,
|
|
|
|
|
|
+ max_connections_attempts: u16,
|
|
url: &str,
|
|
url: &str,
|
|
on_connection: Option<F>,
|
|
on_connection: Option<F>,
|
|
) -> Result<Self, Error>
|
|
) -> Result<Self, Error>
|
|
@@ -46,7 +46,7 @@ impl Relayer {
|
|
send_to_socket.clone(),
|
|
send_to_socket.clone(),
|
|
receiver,
|
|
receiver,
|
|
url,
|
|
url,
|
|
- connection_retries,
|
|
|
|
|
|
+ max_connections_attempts,
|
|
on_connection,
|
|
on_connection,
|
|
)?;
|
|
)?;
|
|
|
|
|
|
@@ -63,7 +63,7 @@ impl Relayer {
|
|
send_to_socket: mpsc::Sender<Request>,
|
|
send_to_socket: mpsc::Sender<Request>,
|
|
mut receiver: mpsc::Receiver<Request>,
|
|
mut receiver: mpsc::Receiver<Request>,
|
|
url_str: &str,
|
|
url_str: &str,
|
|
- connection_retries: u16,
|
|
|
|
|
|
+ max_connections_attempts: u16,
|
|
on_connection: Option<F>,
|
|
on_connection: Option<F>,
|
|
) -> Result<oneshot::Sender<()>, Error>
|
|
) -> Result<oneshot::Sender<()>, Error>
|
|
where
|
|
where
|
|
@@ -79,11 +79,11 @@ impl Relayer {
|
|
|
|
|
|
tokio::spawn(async move {
|
|
tokio::spawn(async move {
|
|
let mut reconnect = true;
|
|
let mut reconnect = true;
|
|
- let mut retries = 0;
|
|
|
|
|
|
+ let mut connection_attempts = 0;
|
|
|
|
|
|
- while reconnect && retries <= connection_retries {
|
|
|
|
- log::warn!("{}: Connect attempt {}", url, retries);
|
|
|
|
- retries += 1;
|
|
|
|
|
|
+ while reconnect && connection_attempts <= max_connections_attempts {
|
|
|
|
+ log::warn!("{}: Connect attempt {}", url, connection_attempts);
|
|
|
|
+ connection_attempts += 1;
|
|
let mut socket = if let Ok(x) = connect_async(url_parsed.clone()).await {
|
|
let mut socket = if let Ok(x) = connect_async(url_parsed.clone()).await {
|
|
x.0
|
|
x.0
|
|
} else {
|
|
} else {
|
|
@@ -118,10 +118,16 @@ impl Relayer {
|
|
}
|
|
}
|
|
msg = timeout(Duration::from_secs(NO_ACTIVITY_TIMEOUT_SECS), socket.next()) => {
|
|
msg = timeout(Duration::from_secs(NO_ACTIVITY_TIMEOUT_SECS), socket.next()) => {
|
|
let msg = if let Ok(Some(Ok(msg))) = msg {
|
|
let msg = if let Ok(Some(Ok(msg))) = msg {
|
|
- if let Ok(msg) = msg.into_text() {
|
|
|
|
- msg
|
|
|
|
- } else {
|
|
|
|
- continue;
|
|
|
|
|
|
+ match msg {
|
|
|
|
+ Message::Text(text) => text,
|
|
|
|
+ Message::Ping(msg) => {
|
|
|
|
+ if let Err(x) = socket.send(Message::Pong(msg.clone())).await {
|
|
|
|
+ log::error!("{} : Reconnecting due error at sending Pong({:?}): {}", url, msg, x);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ continue;
|
|
|
|
+ },
|
|
|
|
+ msg => panic!("Unexpected {:?}", msg),
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
log::error!("{} Reconnecting client due of empty recv: {:?}", url, msg);
|
|
log::error!("{} Reconnecting client due of empty recv: {:?}", url, msg);
|
|
@@ -133,13 +139,13 @@ impl Relayer {
|
|
}
|
|
}
|
|
|
|
|
|
log::info!("New message: {}", msg);
|
|
log::info!("New message: {}", msg);
|
|
- retries = 0;
|
|
|
|
|
|
+ connection_attempts = 0;
|
|
|
|
|
|
|
|
|
|
let msg: Result<Response, _> = serde_json::from_str(&msg);
|
|
let msg: Result<Response, _> = serde_json::from_str(&msg);
|
|
|
|
|
|
if let Ok(msg) = msg {
|
|
if let Ok(msg) = msg {
|
|
- if let Err(error) = broadcast_to_listeners.send((msg, url.to_owned())).await {
|
|
|
|
|
|
+ if let Err(error) = broadcast_to_listeners.try_send((msg, url.to_owned())) {
|
|
log::error!("{}: Reconnecting client because of {}", url, error);
|
|
log::error!("{}: Reconnecting client because of {}", url, error);
|
|
break;
|
|
break;
|
|
}
|
|
}
|