Skip to content

Commit

Permalink
controllers/version: Pipelining database queries (#10306)
Browse files Browse the repository at this point in the history
  • Loading branch information
eth3lbert authored Jan 2, 2025
1 parent 9607d28 commit dd90af8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/controllers/version/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ use super::CrateVersionPath;
pub async fn find_version(state: AppState, path: CrateVersionPath) -> AppResult<ErasedJson> {
let mut conn = state.db_read().await?;
let (version, krate) = path.load_version_and_crate(&mut conn).await?;
let published_by = version.published_by(&mut conn).await?;
let actions = VersionOwnerAction::by_version(&mut conn, &version).await?;
let (actions, published_by) = tokio::try_join!(
VersionOwnerAction::by_version(&mut conn, &version),
version.published_by(&mut conn),
)?;

let version = EncodableVersion::from(version, &krate.name, published_by, actions);
Ok(json!({ "version": version }))
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/version/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ pub async fn update_version(
)
.await?;

let published_by = version.published_by(&mut conn).await?;
let actions = VersionOwnerAction::by_version(&mut conn, &version).await?;
let (actions, published_by) = tokio::try_join!(
VersionOwnerAction::by_version(&mut conn, &version),
version.published_by(&mut conn),
)?;
let updated_version = EncodableVersion::from(version, &krate.name, published_by, actions);
Ok(json!({ "version": updated_version }))
}
Expand Down
10 changes: 6 additions & 4 deletions src/models/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use chrono::NaiveDateTime;
use crates_io_diesel_helpers::pg_enum;
use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};
use futures_util::future::BoxFuture;
use futures_util::future::FutureExt;

pg_enum! {
pub enum VersionAction {
Expand Down Expand Up @@ -54,18 +56,18 @@ impl VersionOwnerAction {
version_owner_actions::table.load(conn).await
}

pub async fn by_version(
pub fn by_version<'a>(
conn: &mut AsyncPgConnection,
version: &Version,
) -> QueryResult<Vec<(Self, User)>> {
version: &'a Version,
) -> BoxFuture<'a, QueryResult<Vec<(Self, User)>>> {
use version_owner_actions::dsl::version_id;

version_owner_actions::table
.filter(version_id.eq(version.id))
.inner_join(users::table)
.order(version_owner_actions::dsl::id)
.load(conn)
.await
.boxed()
}

pub async fn for_versions(
Expand Down

0 comments on commit dd90af8

Please sign in to comment.