From 6baa9fe37b60c46bffe0a6d89e6a0dd632273d32 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 8 Sep 2021 18:20:32 -0700 Subject: [PATCH] WIP - Start work on reconnect logic --- zed/src/channel.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++++ zed/src/rpc.rs | 21 ++++++++++++--- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/zed/src/channel.rs b/zed/src/channel.rs index e0d65e89690a5d679b61e86b1792fe80b2f6e228..601e47355e7ff683ed164e62a98429104ff5c247 100644 --- a/zed/src/channel.rs +++ b/zed/src/channel.rs @@ -653,4 +653,69 @@ mod tests { ); }); } + + #[gpui::test] + async fn test_channel_reconnect(mut cx: TestAppContext) { + let user_id = 5; + let mut client = Client::new(); + let server = FakeServer::for_client(user_id, &mut client, &cx).await; + + let user_store = Arc::new(UserStore::new(client.clone())); + + let channel = cx.add_model(|cx| { + Channel::new( + ChannelDetails { + id: 1, + name: "general".into(), + }, + user_store, + client.clone(), + cx, + ) + }); + + let join_channel = server.receive::().await.unwrap(); + server + .respond( + join_channel.receipt(), + proto::JoinChannelResponse { + messages: vec![ + proto::ChannelMessage { + id: 10, + body: "a".into(), + timestamp: 1000, + sender_id: 5, + }, + proto::ChannelMessage { + id: 11, + body: "b".into(), + timestamp: 1001, + sender_id: 5, + }, + ], + done: false, + }, + ) + .await; + + let get_users = server.receive::().await.unwrap(); + assert_eq!(get_users.payload.user_ids, vec![5]); + server + .respond( + get_users.receipt(), + proto::GetUsersResponse { + users: vec![proto::User { + id: 5, + github_login: "nathansobo".into(), + avatar_url: "http://avatar.com/nathansobo".into(), + }], + }, + ) + .await; + + // Disconnect, wait for the client to reconnect. + server.disconnect().await; + cx.foreground().advance_clock(Duration::from_secs(10)); + let get_messages = server.receive::().await.unwrap(); + } } diff --git a/zed/src/rpc.rs b/zed/src/rpc.rs index 81cc10395f487fa1b1a804fe3873538302f7f361..518e29170466eff91375b5e4d42737e90f33537e 100644 --- a/zed/src/rpc.rs +++ b/zed/src/rpc.rs @@ -146,14 +146,27 @@ impl Client { })); } Status::ConnectionLost => { - state._maintain_connection = Some(cx.foreground().spawn(async move { - // TODO: try to reconnect + let this = self.clone(); + let foreground = cx.foreground(); + state._maintain_connection = Some(cx.spawn(|cx| async move { + let mut delay_seconds = 5; + while let Err(error) = this.authenticate_and_connect(&cx).await { + log::error!("failed to connect {}", error); + let delay = Duration::from_secs(delay_seconds); + this.set_status( + Status::ReconnectionError { + next_reconnection: Instant::now() + delay, + }, + &cx, + ); + foreground.sleep(delay).await; + delay_seconds = (delay_seconds * 2).min(300); + } })); } - Status::Disconnected => { + _ => { state._maintain_connection.take(); } - _ => {} } }