Reduce DB load upon initial connection due to channel loading (#12500)

#### Lazily loading channels

I've added a new RPC message called `SubscribeToChannels` that the
client now sends when it first renders the channels panel. This causes
the server to load the channels for that client and send updates to that
client as channels are updated. Previously, the server did this upon
connection.

For backwards compatibility, the server will inspect clients' version,
and continue to do this work immediately for old clients.

#### Optimizations

Running collab locally, I realized that upon connecting, we were running
two concurrent transactions that *both* queried the `channel_members`
table: one for loading your channels, and one for loading your channel
invites. I've combined these into one query. In addition, we now use a
join to load channels + members, as opposed to two separate queries.
Even though `where id in` is efficient, it adds an extra round trip to
the database, keeping the transaction open for slightly longer.

Release Notes:

- N/A
This commit is contained in:
Max Brunsfeld 2024-05-30 13:02:55 -07:00 committed by GitHub
parent 99901801f4
commit 279c5ab81f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 101 additions and 78 deletions

View file

@ -557,6 +557,7 @@ impl Server {
.add_request_handler(user_handler(request_contact))
.add_request_handler(user_handler(remove_contact))
.add_request_handler(user_handler(respond_to_contact_request))
.add_message_handler(subscribe_to_channels)
.add_request_handler(user_handler(create_channel))
.add_request_handler(user_handler(delete_channel))
.add_request_handler(user_handler(invite_channel_member))
@ -1105,34 +1106,25 @@ impl Server {
.await?;
}
let (contacts, channels_for_user, channel_invites, dev_server_projects) =
future::try_join4(
self.app_state.db.get_contacts(user.id),
self.app_state.db.get_channels_for_user(user.id),
self.app_state.db.get_channel_invites_for_user(user.id),
self.app_state.db.dev_server_projects_update(user.id),
)
.await?;
let (contacts, dev_server_projects) = future::try_join(
self.app_state.db.get_contacts(user.id),
self.app_state.db.dev_server_projects_update(user.id),
)
.await?;
{
let mut pool = self.connection_pool.lock();
pool.add_connection(connection_id, user.id, user.admin, zed_version);
for membership in &channels_for_user.channel_memberships {
pool.subscribe_to_channel(user.id, membership.channel_id, membership.role)
}
self.peer.send(
connection_id,
build_initial_contacts_update(contacts, &pool),
)?;
self.peer.send(
connection_id,
build_update_user_channels(&channels_for_user),
)?;
self.peer.send(
connection_id,
build_channels_update(channels_for_user, channel_invites),
)?;
}
if should_auto_subscribe_to_channels(zed_version) {
subscribe_user_to_channels(user.id, session).await?;
}
send_dev_server_projects_update(user.id, dev_server_projects, session).await;
if let Some(incoming_call) =
@ -3399,6 +3391,36 @@ async fn remove_contact(
Ok(())
}
fn should_auto_subscribe_to_channels(version: ZedVersion) -> bool {
version.0.minor() < 139
}
async fn subscribe_to_channels(_: proto::SubscribeToChannels, session: Session) -> Result<()> {
subscribe_user_to_channels(
session.user_id().ok_or_else(|| anyhow!("must be a user"))?,
&session,
)
.await?;
Ok(())
}
async fn subscribe_user_to_channels(user_id: UserId, session: &Session) -> Result<(), Error> {
let channels_for_user = session.db().await.get_channels_for_user(user_id).await?;
let mut pool = session.connection_pool().await;
for membership in &channels_for_user.channel_memberships {
pool.subscribe_to_channel(user_id, membership.channel_id, membership.role)
}
session.peer.send(
session.connection_id,
build_update_user_channels(&channels_for_user),
)?;
session.peer.send(
session.connection_id,
build_channels_update(channels_for_user),
)?;
Ok(())
}
/// Creates a new channel.
async fn create_channel(
request: proto::CreateChannel,
@ -5034,7 +5056,7 @@ fn notify_membership_updated(
..Default::default()
};
let mut update = build_channels_update(result.new_channels, vec![]);
let mut update = build_channels_update(result.new_channels);
update.delete_channels = result
.removed_channels
.into_iter()
@ -5064,10 +5086,7 @@ fn build_update_user_channels(channels: &ChannelsForUser) -> proto::UpdateUserCh
}
}
fn build_channels_update(
channels: ChannelsForUser,
channel_invites: Vec<db::Channel>,
) -> proto::UpdateChannels {
fn build_channels_update(channels: ChannelsForUser) -> proto::UpdateChannels {
let mut update = proto::UpdateChannels::default();
for channel in channels.channels {
@ -5086,7 +5105,7 @@ fn build_channels_update(
});
}
for channel in channel_invites {
for channel in channels.invited_channels {
update.channel_invitations.push(channel.to_proto());
}