Skip to content

controllers: Replace r2d2 with deadpool #8431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d0d6f8f
controllers/category: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
c5969f4
controllers/crate_owner_invitation: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
e4bd0a6
controllers/github/secret_scanning: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
53d11dc
controllers/keyword: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
8327540
controllers/krate/downloads: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
199c5a2
controllers/krate/follow: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
95f6e99
controllers/krate/metadata: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
1f2c60e
controllers/krate/owners: Move `spawn_blocking()` call into `modify_o…
Turbo87 Apr 9, 2024
42b5380
controllers/krate/owners: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
a7991ef
controllers/krate/publish: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
c88365e
controllers/krate/search: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
b7b9d70
controllers/krate/versions: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
4903ac9
controllers/metrics: Move `spawn_blocking()` calls into match arms
Turbo87 Apr 9, 2024
c5b8e3f
controllers/metrics: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
913f895
controllers/summary: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
740bf53
controllers/token: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
8f965c8
controllers/user/me: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
dbf4345
controllers/user/other: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
ec052df
controllers/version/downloads: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
c6556eb
controllers/version/metadata: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
3aa42e7
controllers/version/yank: Move `spawn_blocking()` call into `modify_y…
Turbo87 Apr 9, 2024
424775b
controllers/version/yank: Replace `r2d2` with `deadpool`
Turbo87 Apr 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/controllers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ use crate::views::{EncodableCategory, EncodableCategoryWithSubcategories};

/// Handles the `GET /categories` route.
pub async fn index(app: AppState, req: Parts) -> AppResult<Json<Value>> {
spawn_blocking(move || {
// FIXME: There are 69 categories, 47 top level. This isn't going to
// grow by an OoM. We need a limit for /summary, but we don't need
// to paginate this.
let options = PaginationOptions::builder().gather(&req)?;

let conn = app.db_read_async().await?;
conn.interact(move |conn| {
let query = req.query();
// FIXME: There are 69 categories, 47 top level. This isn't going to
// grow by an OoM. We need a limit for /summary, but we don't need
// to paginate this.
let options = PaginationOptions::builder().gather(&req)?;
let offset = options.offset().unwrap_or_default();
let sort = query.get("sort").map_or("alpha", String::as_str);

let conn = &mut app.db_read()?;
let offset = options.offset().unwrap_or_default();

let categories = Category::toplevel(conn, sort, options.per_page, offset)?;
let categories = categories
.into_iter()
Expand All @@ -31,13 +33,13 @@ pub async fn index(app: AppState, req: Parts) -> AppResult<Json<Value>> {
"meta": { "total": total },
})))
})
.await
.await?
}

/// Handles the `GET /categories/:category_id` route.
pub async fn show(state: AppState, Path(slug): Path<String>) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = &mut *state.db_read()?;
let conn = state.db_read_async().await?;
conn.interact(move |conn| {
let cat: Category = Category::by_slug(&slug).first(conn)?;
let subcats = cat
.subcategories(conn)?
Expand All @@ -64,13 +66,13 @@ pub async fn show(state: AppState, Path(slug): Path<String>) -> AppResult<Json<V

Ok(Json(json!({ "category": cat_with_subcats })))
})
.await
.await?
}

/// Handles the `GET /category_slugs` route.
pub async fn slugs(state: AppState) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = &mut *state.db_read()?;
let conn = state.db_read_async().await?;
conn.interact(move |conn| {
let slugs: Vec<Slug> = categories::table
.select((categories::slug, categories::slug, categories::description))
.order(categories::slug)
Expand All @@ -85,5 +87,5 @@ pub async fn slugs(state: AppState) -> AppResult<Json<Value>> {

Ok(Json(json!({ "category_slugs": slugs })))
})
.await
.await?
}
31 changes: 15 additions & 16 deletions src/controllers/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use tokio::runtime::Handle;

/// Handles the `GET /api/v1/me/crate_owner_invitations` route.
pub async fn list(app: AppState, req: Parts) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = &mut app.db_read()?;
let conn = app.db_read_async().await?;
conn.interact(move |conn| {
let auth = AuthCheck::only_cookie().check(&req, conn)?;
let user_id = auth.user_id();

Expand Down Expand Up @@ -53,13 +53,13 @@ pub async fn list(app: AppState, req: Parts) -> AppResult<Json<Value>> {
"users": users,
})))
})
.await
.await?
}

/// Handles the `GET /api/private/crate_owner_invitations` route.
pub async fn private_list(app: AppState, req: Parts) -> AppResult<Json<PrivateListResponse>> {
spawn_blocking(move || {
let conn = &mut app.db_read()?;
let conn = app.db_read_async().await?;
conn.interact(move |conn| {
let auth = AuthCheck::only_cookie().check(&req, conn)?;

let filter = if let Some(crate_name) = req.query().get("crate_name") {
Expand All @@ -73,7 +73,7 @@ pub async fn private_list(app: AppState, req: Parts) -> AppResult<Json<PrivateLi
let list = prepare_list(&app, &req, auth, filter, conn)?;
Ok(Json(list))
})
.await
.await?
}

enum ListFilter {
Expand Down Expand Up @@ -260,14 +260,13 @@ struct OwnerInvitation {

/// Handles the `PUT /api/v1/me/crate_owner_invitations/:crate_id` route.
pub async fn handle_invite(state: AppState, req: BytesRequest) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let crate_invite: OwnerInvitation =
serde_json::from_slice(req.body()).map_err(|_| bad_request("invalid json request"))?;
let crate_invite: OwnerInvitation =
serde_json::from_slice(req.body()).map_err(|_| bad_request("invalid json request"))?;

let crate_invite = crate_invite.crate_owner_invite;

let conn = &mut state.db_write()?;
let crate_invite = crate_invite.crate_owner_invite;

let conn = state.db_write_async().await?;
conn.interact(move |conn| {
let auth = AuthCheck::default().check(&req, conn)?;
let user_id = auth.user_id();

Expand All @@ -282,17 +281,17 @@ pub async fn handle_invite(state: AppState, req: BytesRequest) -> AppResult<Json

Ok(Json(json!({ "crate_owner_invitation": crate_invite })))
})
.await
.await?
}

/// Handles the `PUT /api/v1/me/crate_owner_invitations/accept/:token` route.
pub async fn handle_invite_with_token(
state: AppState,
Path(token): Path<String>,
) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = state.db_write_async().await?;
conn.interact(move |conn| {
let config = &state.config;
let conn = &mut state.db_write()?;

let invitation = CrateOwnerInvitation::find_by_token(&token, conn)?;
let crate_id = invitation.crate_id;
Expand All @@ -305,5 +304,5 @@ pub async fn handle_invite_with_token(
},
})))
})
.await
.await?
}
7 changes: 3 additions & 4 deletions src/controllers/github/secret_scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,8 @@ pub async fn verify(
let alerts: Vec<GitHubSecretAlert> = json::from_slice(&body)
.map_err(|e| bad_request(format!("invalid secret alert request: {e:?}")))?;

spawn_blocking(move || {
let conn = &mut *state.db_write()?;

let conn = state.db_write_async().await?;
conn.interact(move |conn| {
let feedback = alerts
.into_iter()
.map(|alert| {
Expand All @@ -272,7 +271,7 @@ pub async fn verify(

Ok(Json(feedback))
})
.await
.await?
}

#[cfg(test)]
Expand Down
28 changes: 14 additions & 14 deletions src/controllers/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ pub struct IndexQuery {

/// Handles the `GET /keywords` route.
pub async fn index(state: AppState, qp: Query<IndexQuery>, req: Parts) -> AppResult<Json<Value>> {
spawn_blocking(move || {
use crate::schema::keywords;
use crate::schema::keywords;

let mut query = keywords::table.into_boxed();
let mut query = keywords::table.into_boxed();

query = match &qp.sort {
Some(sort) if sort == "crates" => query.order(keywords::crates_cnt.desc()),
_ => query.order(keywords::keyword.asc()),
};
query = match &qp.sort {
Some(sort) if sort == "crates" => query.order(keywords::crates_cnt.desc()),
_ => query.order(keywords::keyword.asc()),
};

let query = query.pages_pagination(PaginationOptions::builder().gather(&req)?);
let conn = &mut state.db_read()?;
let query = query.pages_pagination(PaginationOptions::builder().gather(&req)?);

let conn = state.db_read_async().await?;
conn.interact(move |conn| {
let data: Paginated<Keyword> = query.load(conn)?;
let total = data.total();
let kws = data
Expand All @@ -39,17 +40,16 @@ pub async fn index(state: AppState, qp: Query<IndexQuery>, req: Parts) -> AppRes
"meta": { "total": total },
})))
})
.await
.await?
}

/// Handles the `GET /keywords/:keyword_id` route.
pub async fn show(Path(name): Path<String>, state: AppState) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = &mut state.db_read()?;

let conn = &mut state.db_read_async().await?;
conn.interact(move |conn| {
let kw = Keyword::find_by_keyword(conn, &name)?;

Ok(Json(json!({ "keyword": EncodableKeyword::from(kw) })))
})
.await
.await?
}
6 changes: 3 additions & 3 deletions src/controllers/krate/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use crate::views::EncodableVersionDownload;

/// Handles the `GET /crates/:crate_id/downloads` route.
pub async fn downloads(state: AppState, Path(crate_name): Path<String>) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = state.db_read_async().await?;
conn.interact(move |conn| {
use diesel::dsl::*;
use diesel::sql_types::BigInt;

let conn = &mut *state.db_read()?;
let crate_id: i32 = Crate::by_name(&crate_name)
.select(crates::id)
.first(conn)
Expand Down Expand Up @@ -68,5 +68,5 @@ pub async fn downloads(state: AppState, Path(crate_name): Path<String>) -> AppRe
},
})))
})
.await
.await?
}
18 changes: 9 additions & 9 deletions src/controllers/krate/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub async fn follow(
Path(crate_name): Path<String>,
req: Parts,
) -> AppResult<Response> {
spawn_blocking(move || {
let conn = &mut *app.db_write()?;
let conn = app.db_write_async().await?;
conn.interact(move |conn| {
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
let follow = follow_target(&crate_name, conn, user_id)?;
diesel::insert_into(follows::table)
Expand All @@ -35,7 +35,7 @@ pub async fn follow(

ok_true()
})
.await
.await?
}

/// Handles the `DELETE /crates/:crate_id/follow` route.
Expand All @@ -44,15 +44,15 @@ pub async fn unfollow(
Path(crate_name): Path<String>,
req: Parts,
) -> AppResult<Response> {
spawn_blocking(move || {
let conn = &mut *app.db_write()?;
let conn = app.db_write_async().await?;
conn.interact(move |conn| {
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
let follow = follow_target(&crate_name, conn, user_id)?;
diesel::delete(&follow).execute(conn)?;

ok_true()
})
.await
.await?
}

/// Handles the `GET /crates/:crate_id/following` route.
Expand All @@ -61,16 +61,16 @@ pub async fn following(
Path(crate_name): Path<String>,
req: Parts,
) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = app.db_read_prefer_primary_async().await?;
conn.interact(move |conn| {
use diesel::dsl::exists;

let conn = &mut *app.db_read_prefer_primary()?;
let user_id = AuthCheck::only_cookie().check(&req, conn)?.user_id();
let follow = follow_target(&crate_name, conn, user_id)?;
let following =
diesel::select(exists(follows::table.find(follow.id()))).get_result::<bool>(conn)?;

Ok(Json(json!({ "following": following })))
})
.await
.await?
}
12 changes: 6 additions & 6 deletions src/controllers/krate/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ pub async fn show_new(app: AppState, req: Parts) -> AppResult<Json<Value>> {

/// Handles the `GET /crates/:crate_id` route.
pub async fn show(app: AppState, Path(name): Path<String>, req: Parts) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = app.db_read_async().await?;
conn.interact(move |conn| {
let include = req
.query()
.get("include")
.map(|mode| ShowIncludeMode::from_str(mode))
.transpose()?
.unwrap_or_default();

let conn = &mut *app.db_read()?;
let (krate, downloads): (Crate, i64) = Crate::by_name(&name)
.inner_join(crate_downloads::table)
.select((Crate::as_select(), crate_downloads::downloads))
Expand Down Expand Up @@ -142,7 +142,7 @@ pub async fn show(app: AppState, Path(name): Path<String>, req: Parts) -> AppRes
"categories": encodable_cats,
})))
})
.await
.await?
}

#[derive(Debug)]
Expand Down Expand Up @@ -227,9 +227,9 @@ pub async fn reverse_dependencies(
Path(name): Path<String>,
req: Parts,
) -> AppResult<Json<Value>> {
spawn_blocking(move || {
let conn = app.db_read_async().await?;
conn.interact(move |conn| {
let pagination_options = PaginationOptions::builder().gather(&req)?;
let conn = &mut *app.db_read()?;

let krate: Crate = Crate::by_name(&name)
.first(conn)
Expand Down Expand Up @@ -273,5 +273,5 @@ pub async fn reverse_dependencies(
"meta": { "total": total },
})))
})
.await
.await?
}
Loading