Avoid trying to reconnect to a room if client is signed out

This commit is contained in:
Antonio Scandurra 2023-02-23 14:53:10 +01:00
parent 9334267bd0
commit 59a66190e5
2 changed files with 28 additions and 21 deletions

View file

@ -277,14 +277,12 @@ impl Room {
) -> Result<()> { ) -> Result<()> {
let mut client_status = client.status(); let mut client_status = client.status();
loop { loop {
let is_connected = client_status let _ = client_status.try_recv();
.next() let is_connected = client_status.borrow().is_connected();
.await
.map_or(false, |s| s.is_connected());
// Even if we're initially connected, any future change of the status means we momentarily disconnected. // Even if we're initially connected, any future change of the status means we momentarily disconnected.
if !is_connected || client_status.next().await.is_some() { if !is_connected || client_status.next().await.is_some() {
log::info!("detected client disconnection"); log::info!("detected client disconnection");
this.upgrade(&cx) this.upgrade(&cx)
.ok_or_else(|| anyhow!("room was dropped"))? .ok_or_else(|| anyhow!("room was dropped"))?
.update(&mut cx, |this, cx| { .update(&mut cx, |this, cx| {
@ -298,12 +296,7 @@ impl Room {
let client_reconnection = async { let client_reconnection = async {
let mut remaining_attempts = 3; let mut remaining_attempts = 3;
while remaining_attempts > 0 { while remaining_attempts > 0 {
log::info!( if client_status.borrow().is_connected() {
"waiting for client status change, remaining attempts {}",
remaining_attempts
);
let Some(status) = client_status.next().await else { break };
if status.is_connected() {
log::info!("client reconnected, attempting to rejoin room"); log::info!("client reconnected, attempting to rejoin room");
let Some(this) = this.upgrade(&cx) else { break }; let Some(this) = this.upgrade(&cx) else { break };
@ -317,7 +310,15 @@ impl Room {
} else { } else {
remaining_attempts -= 1; remaining_attempts -= 1;
} }
} else if client_status.borrow().is_signed_out() {
return false;
} }
log::info!(
"waiting for client status change, remaining attempts {}",
remaining_attempts
);
client_status.next().await;
} }
false false
} }
@ -339,18 +340,20 @@ impl Room {
} }
} }
// The client failed to re-establish a connection to the server break;
// or an error occurred while trying to re-join the room. Either way
// we leave the room and return an error.
if let Some(this) = this.upgrade(&cx) {
log::info!("reconnection failed, leaving room");
let _ = this.update(&mut cx, |this, cx| this.leave(cx));
}
return Err(anyhow!(
"can't reconnect to room: client failed to re-establish connection"
));
} }
} }
// The client failed to re-establish a connection to the server
// or an error occurred while trying to re-join the room. Either way
// we leave the room and return an error.
if let Some(this) = this.upgrade(&cx) {
log::info!("reconnection failed, leaving room");
let _ = this.update(&mut cx, |this, cx| this.leave(cx));
}
Err(anyhow!(
"can't reconnect to room: client failed to re-establish connection"
))
} }
fn rejoin(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> { fn rejoin(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {

View file

@ -179,6 +179,10 @@ impl Status {
pub fn is_connected(&self) -> bool { pub fn is_connected(&self) -> bool {
matches!(self, Self::Connected { .. }) matches!(self, Self::Connected { .. })
} }
pub fn is_signed_out(&self) -> bool {
matches!(self, Self::SignedOut | Self::UpgradeRequired)
}
} }
struct ClientState { struct ClientState {