Skip to content

Commit 3d19e1c

Browse files
authored
Merge pull request #8431 from Turbo87/deadpool-controllers
controllers: Replace `r2d2` with `deadpool`
2 parents 1031565 + 424775b commit 3d19e1c

19 files changed

+283
-279
lines changed

src/controllers/category.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@ use crate::views::{EncodableCategory, EncodableCategoryWithSubcategories};
77

88
/// Handles the `GET /categories` route.
99
pub async fn index(app: AppState, req: Parts) -> AppResult<Json<Value>> {
10-
spawn_blocking(move || {
10+
// FIXME: There are 69 categories, 47 top level. This isn't going to
11+
// grow by an OoM. We need a limit for /summary, but we don't need
12+
// to paginate this.
13+
let options = PaginationOptions::builder().gather(&req)?;
14+
15+
let conn = app.db_read_async().await?;
16+
conn.interact(move |conn| {
1117
let query = req.query();
12-
// FIXME: There are 69 categories, 47 top level. This isn't going to
13-
// grow by an OoM. We need a limit for /summary, but we don't need
14-
// to paginate this.
15-
let options = PaginationOptions::builder().gather(&req)?;
16-
let offset = options.offset().unwrap_or_default();
1718
let sort = query.get("sort").map_or("alpha", String::as_str);
1819

19-
let conn = &mut app.db_read()?;
20+
let offset = options.offset().unwrap_or_default();
21+
2022
let categories = Category::toplevel(conn, sort, options.per_page, offset)?;
2123
let categories = categories
2224
.into_iter()
@@ -31,13 +33,13 @@ pub async fn index(app: AppState, req: Parts) -> AppResult<Json<Value>> {
3133
"meta": { "total": total },
3234
})))
3335
})
34-
.await
36+
.await?
3537
}
3638

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

6567
Ok(Json(json!({ "category": cat_with_subcats })))
6668
})
67-
.await
69+
.await?
6870
}
6971

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

8688
Ok(Json(json!({ "category_slugs": slugs })))
8789
})
88-
.await
90+
.await?
8991
}

src/controllers/crate_owner_invitation.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use tokio::runtime::Handle;
1818

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

@@ -53,13 +53,13 @@ pub async fn list(app: AppState, req: Parts) -> AppResult<Json<Value>> {
5353
"users": users,
5454
})))
5555
})
56-
.await
56+
.await?
5757
}
5858

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

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

7979
enum ListFilter {
@@ -260,14 +260,13 @@ struct OwnerInvitation {
260260

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

267-
let crate_invite = crate_invite.crate_owner_invite;
268-
269-
let conn = &mut state.db_write()?;
266+
let crate_invite = crate_invite.crate_owner_invite;
270267

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

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

283282
Ok(Json(json!({ "crate_owner_invitation": crate_invite })))
284283
})
285-
.await
284+
.await?
286285
}
287286

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

297296
let invitation = CrateOwnerInvitation::find_by_token(&token, conn)?;
298297
let crate_id = invitation.crate_id;
@@ -305,5 +304,5 @@ pub async fn handle_invite_with_token(
305304
},
306305
})))
307306
})
308-
.await
307+
.await?
309308
}

src/controllers/github/secret_scanning.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,8 @@ pub async fn verify(
255255
let alerts: Vec<GitHubSecretAlert> = json::from_slice(&body)
256256
.map_err(|e| bad_request(format!("invalid secret alert request: {e:?}")))?;
257257

258-
spawn_blocking(move || {
259-
let conn = &mut *state.db_write()?;
260-
258+
let conn = state.db_write_async().await?;
259+
conn.interact(move |conn| {
261260
let feedback = alerts
262261
.into_iter()
263262
.map(|alert| {
@@ -272,7 +271,7 @@ pub async fn verify(
272271

273272
Ok(Json(feedback))
274273
})
275-
.await
274+
.await?
276275
}
277276

278277
#[cfg(test)]

src/controllers/keyword.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ pub struct IndexQuery {
1515

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

21-
let mut query = keywords::table.into_boxed();
20+
let mut query = keywords::table.into_boxed();
2221

23-
query = match &qp.sort {
24-
Some(sort) if sort == "crates" => query.order(keywords::crates_cnt.desc()),
25-
_ => query.order(keywords::keyword.asc()),
26-
};
22+
query = match &qp.sort {
23+
Some(sort) if sort == "crates" => query.order(keywords::crates_cnt.desc()),
24+
_ => query.order(keywords::keyword.asc()),
25+
};
2726

28-
let query = query.pages_pagination(PaginationOptions::builder().gather(&req)?);
29-
let conn = &mut state.db_read()?;
27+
let query = query.pages_pagination(PaginationOptions::builder().gather(&req)?);
28+
29+
let conn = state.db_read_async().await?;
30+
conn.interact(move |conn| {
3031
let data: Paginated<Keyword> = query.load(conn)?;
3132
let total = data.total();
3233
let kws = data
@@ -39,17 +40,16 @@ pub async fn index(state: AppState, qp: Query<IndexQuery>, req: Parts) -> AppRes
3940
"meta": { "total": total },
4041
})))
4142
})
42-
.await
43+
.await?
4344
}
4445

4546
/// Handles the `GET /keywords/:keyword_id` route.
4647
pub async fn show(Path(name): Path<String>, state: AppState) -> AppResult<Json<Value>> {
47-
spawn_blocking(move || {
48-
let conn = &mut state.db_read()?;
49-
48+
let conn = &mut state.db_read_async().await?;
49+
conn.interact(move |conn| {
5050
let kw = Keyword::find_by_keyword(conn, &name)?;
5151

5252
Ok(Json(json!({ "keyword": EncodableKeyword::from(kw) })))
5353
})
54-
.await
54+
.await?
5555
}

src/controllers/krate/downloads.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use crate::views::EncodableVersionDownload;
1515

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

22-
let conn = &mut *state.db_read()?;
2323
let crate_id: i32 = Crate::by_name(&crate_name)
2424
.select(crates::id)
2525
.first(conn)
@@ -68,5 +68,5 @@ pub async fn downloads(state: AppState, Path(crate_name): Path<String>) -> AppRe
6868
},
6969
})))
7070
})
71-
.await
71+
.await?
7272
}

src/controllers/krate/follow.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ pub async fn follow(
2424
Path(crate_name): Path<String>,
2525
req: Parts,
2626
) -> AppResult<Response> {
27-
spawn_blocking(move || {
28-
let conn = &mut *app.db_write()?;
27+
let conn = app.db_write_async().await?;
28+
conn.interact(move |conn| {
2929
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
3030
let follow = follow_target(&crate_name, conn, user_id)?;
3131
diesel::insert_into(follows::table)
@@ -35,7 +35,7 @@ pub async fn follow(
3535

3636
ok_true()
3737
})
38-
.await
38+
.await?
3939
}
4040

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

5353
ok_true()
5454
})
55-
.await
55+
.await?
5656
}
5757

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

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

7373
Ok(Json(json!({ "following": following })))
7474
})
75-
.await
75+
.await?
7676
}

src/controllers/krate/metadata.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ pub async fn show_new(app: AppState, req: Parts) -> AppResult<Json<Value>> {
2727

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

38-
let conn = &mut *app.db_read()?;
3939
let (krate, downloads): (Crate, i64) = Crate::by_name(&name)
4040
.inner_join(crate_downloads::table)
4141
.select((Crate::as_select(), crate_downloads::downloads))
@@ -142,7 +142,7 @@ pub async fn show(app: AppState, Path(name): Path<String>, req: Parts) -> AppRes
142142
"categories": encodable_cats,
143143
})))
144144
})
145-
.await
145+
.await?
146146
}
147147

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

234234
let krate: Crate = Crate::by_name(&name)
235235
.first(conn)
@@ -273,5 +273,5 @@ pub async fn reverse_dependencies(
273273
"meta": { "total": total },
274274
})))
275275
})
276-
.await
276+
.await?
277277
}

0 commit comments

Comments
 (0)