Allow server to see client disconnection before giving up on reconnecting

This commit is contained in:
Antonio Scandurra 2022-12-13 12:17:21 +01:00
parent 417db95693
commit f1884d608b

View file

@ -264,28 +264,18 @@ impl Room {
});
// Wait for client to re-establish a connection to the server.
{
let mut reconnection_timeout = cx.background().timer(RECONNECT_TIMEOUT).fuse();
let client_reconnection = async {
loop {
let mut remaining_attempts = 3;
while remaining_attempts > 0 {
if let Some(status) = client_status.next().await {
if status.is_connected() {
return true;
}
} else {
return false;
}
}
}
.fuse();
futures::pin_mut!(client_reconnection);
futures::select_biased! {
reconnected = client_reconnection => {
if reconnected {
// Client managed to reconnect to the server. Now attempt to join the room.
let rejoin_room = async {
let response = client.request(proto::JoinRoom { id: room_id }).await?;
let room_proto = response.room.ok_or_else(|| anyhow!("invalid room"))?;
let response =
client.request(proto::JoinRoom { id: room_id }).await?;
let room_proto =
response.room.ok_or_else(|| anyhow!("invalid room"))?;
this.upgrade(&cx)
.ok_or_else(|| anyhow!("room was dropped"))?
.update(&mut cx, |this, cx| {
@ -295,15 +285,32 @@ impl Room {
anyhow::Ok(())
};
if rejoin_room.await.is_ok() {
return true;
} else {
remaining_attempts -= 1;
}
}
} else {
return false;
}
}
false
}
.fuse();
futures::pin_mut!(client_reconnection);
futures::select_biased! {
reconnected = client_reconnection => {
if reconnected {
// If we successfully joined the room, go back around the loop
// waiting for future connection status changes.
if rejoin_room.await.log_err().is_some() {
continue;
}
}
}
_ = reconnection_timeout => {}
}
}
// The client failed to re-establish a connection to the server
// or an error occurred while trying to re-join the room. Either way