Skip to content

Commit

Permalink
core: retry session connection
Browse files Browse the repository at this point in the history
  • Loading branch information
kingosticks committed Sep 20, 2024
1 parent 67d3195 commit 3cbef8f
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl Session {

async fn connect_inner(
&self,
access_point: SocketAddress,
access_point: &SocketAddress,
credentials: Credentials,
) -> Result<(Credentials, Transport), Error> {
let mut transport = connection::connect(
Expand Down Expand Up @@ -187,19 +187,30 @@ impl Session {
credentials: Credentials,
store_credentials: bool,
) -> Result<(), Error> {
let (reusable_credentials, transport) = loop {
const MAX_RETRIES: u8 = 2;
let (reusable_credentials, transport) = 'ap: loop {
let ap = self.apresolver().resolve("accesspoint").await?;
info!("Connecting to AP \"{}:{}\"", ap.0, ap.1);
match self.connect_inner(ap, credentials.clone()).await {
Ok(ct) => break ct,
Err(e) => {
if let Some(AuthenticationError::LoginFailed(ErrorCode::TryAnotherAP)) =
e.error.downcast_ref::<AuthenticationError>()
{
warn!("Instructed to try another access point...");
continue;
} else {
return Err(e);
let mut num_retries = 0;
loop {
match self.connect_inner(&ap, credentials.clone()).await {
Ok(ct) => break 'ap ct,
Err(e) => {
if let Some(AuthenticationError::LoginFailed(ErrorCode::TryAnotherAP)) =
e.error.downcast_ref::<AuthenticationError>()
{
warn!("Instructed to try another access point...");
continue 'ap;
} else {
debug!("Connection failed: {e}");
if num_retries == MAX_RETRIES {
warn!("Retries exceeded, try another access point...");
continue 'ap;
} else {
num_retries += 1;
trace!("Retry access point...");
}
}
}
}
}
Expand Down Expand Up @@ -567,7 +578,7 @@ impl Session {
}

pub fn shutdown(&self) {
debug!("Invalidating session");
debug!("Shutdown: Invalidating session");
self.0.data.write().invalid = true;
self.mercury().shutdown();
self.channel().shutdown();
Expand Down Expand Up @@ -624,6 +635,7 @@ where
return Poll::Ready(Ok(()));
}
Some(Err(e)) => {
error!("Connection to server closed.");
session.shutdown();
return Poll::Ready(Err(e));
}
Expand Down

0 comments on commit 3cbef8f

Please sign in to comment.