Skip to content

deadpool: Drop obsolete async suffixes #8447

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 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 18 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ type DeadpoolResult = Result<deadpool_diesel::postgres::Connection, deadpool_die
/// The `App` struct holds the main components of the application like
/// the database connection pool and configurations
pub struct App {
/// Async database connection pool based on `deadpool` connected
/// to the primary database
pub deadpool_primary: DeadpoolPool,
/// Database connection pool connected to the primary database
pub primary_database: DeadpoolPool,

/// Async database connection pool based on `deadpool` connected
/// to the read-only replica database
pub deadpool_replica: Option<DeadpoolPool>,
/// Database connection pool connected to the read-only replica database
pub replica_database: Option<DeadpoolPool>,

/// GitHub API client
pub github: Box<dyn GitHubClient>,
Expand Down Expand Up @@ -80,7 +78,7 @@ impl App {
),
);

let primary_database_async = {
let primary_database = {
use secrecy::ExposeSecret;

let primary_db_connection_config = ConnectionConfig {
Expand All @@ -100,7 +98,7 @@ impl App {
.unwrap()
};

let replica_database_async = if let Some(pool_config) = config.db.replica.as_ref() {
let replica_database = if let Some(pool_config) = config.db.replica.as_ref() {
use secrecy::ExposeSecret;

let replica_db_connection_config = ConnectionConfig {
Expand All @@ -125,8 +123,8 @@ impl App {
};

App {
deadpool_primary: primary_database_async,
deadpool_replica: replica_database_async,
primary_database,
replica_database,
github,
github_oauth,
emails,
Expand All @@ -146,18 +144,18 @@ impl App {

/// Obtain a read/write database connection from the async primary pool
#[instrument(skip_all)]
pub async fn db_write_async(&self) -> DeadpoolResult {
self.deadpool_primary.get().await
pub async fn db_write(&self) -> DeadpoolResult {
self.primary_database.get().await
}

/// Obtain a readonly database connection from the replica pool
///
/// If the replica pool is disabled or unavailable, the primary pool is used instead.
#[instrument(skip_all)]
pub async fn db_read_async(&self) -> DeadpoolResult {
let Some(read_only_pool) = self.deadpool_replica.as_ref() else {
pub async fn db_read(&self) -> DeadpoolResult {
let Some(read_only_pool) = self.replica_database.as_ref() else {
// Replica is disabled, but primary might be available
return self.deadpool_primary.get().await;
return self.primary_database.get().await;
};

match read_only_pool.get().await {
Expand All @@ -173,7 +171,7 @@ impl App {
.map(|metric| metric.inc());

warn!("Replica is unavailable, falling back to primary ({error})");
self.deadpool_primary.get().await
self.primary_database.get().await
}

// Replica failed
Expand All @@ -185,12 +183,12 @@ impl App {
///
/// If the primary pool is unavailable, the replica pool is used instead, if not disabled.
#[instrument(skip_all)]
pub async fn db_read_prefer_primary_async(&self) -> DeadpoolResult {
let Some(read_only_pool) = self.deadpool_replica.as_ref() else {
return self.deadpool_primary.get().await;
pub async fn db_read_prefer_primary(&self) -> DeadpoolResult {
let Some(read_only_pool) = self.replica_database.as_ref() else {
return self.primary_database.get().await;
};

match self.deadpool_primary.get().await {
match self.primary_database.get().await {
// Primary is available
Ok(connection) => Ok(connection),

Expand Down
6 changes: 3 additions & 3 deletions src/controllers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub async fn index(app: AppState, req: Parts) -> AppResult<Json<Value>> {
// to paginate this.
let options = PaginationOptions::builder().gather(&req)?;

let conn = app.db_read_async().await?;
let conn = app.db_read().await?;
conn.interact(move |conn| {
let query = req.query();
let sort = query.get("sort").map_or("alpha", String::as_str);
Expand All @@ -38,7 +38,7 @@ pub async fn index(app: AppState, req: Parts) -> AppResult<Json<Value>> {

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

/// Handles the `GET /category_slugs` route.
pub async fn slugs(state: AppState) -> AppResult<Json<Value>> {
let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
conn.interact(move |conn| {
let slugs: Vec<Slug> = categories::table
.select((categories::slug, categories::slug, categories::description))
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/crate_owner_invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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>> {
let conn = app.db_read_async().await?;
let conn = app.db_read().await?;
conn.interact(move |conn| {
let auth = AuthCheck::only_cookie().check(&req, conn)?;
let user_id = auth.user_id();
Expand Down Expand Up @@ -58,7 +58,7 @@ pub async fn list(app: AppState, req: Parts) -> AppResult<Json<Value>> {

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

Expand Down Expand Up @@ -265,7 +265,7 @@ pub async fn handle_invite(state: AppState, req: BytesRequest) -> AppResult<Json

let crate_invite = crate_invite.crate_owner_invite;

let conn = state.db_write_async().await?;
let conn = state.db_write().await?;
conn.interact(move |conn| {
let auth = AuthCheck::default().check(&req, conn)?;
let user_id = auth.user_id();
Expand All @@ -289,7 +289,7 @@ pub async fn handle_invite_with_token(
state: AppState,
Path(token): Path<String>,
) -> AppResult<Json<Value>> {
let conn = state.db_write_async().await?;
let conn = state.db_write().await?;
conn.interact(move |conn| {
let config = &state.config;

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/github/secret_scanning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ pub async fn verify(
let alerts: Vec<GitHubSecretAlert> = json::from_slice(&body)
.map_err(|e| bad_request(format!("invalid secret alert request: {e:?}")))?;

let conn = state.db_write_async().await?;
let conn = state.db_write().await?;
conn.interact(move |conn| {
let feedback = alerts
.into_iter()
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub async fn index(state: AppState, qp: Query<IndexQuery>, req: Parts) -> AppRes

let query = query.pages_pagination(PaginationOptions::builder().gather(&req)?);

let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
conn.interact(move |conn| {
let data: Paginated<Keyword> = query.load(conn)?;
let total = data.total();
Expand All @@ -45,7 +45,7 @@ pub async fn index(state: AppState, qp: Query<IndexQuery>, req: Parts) -> AppRes

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

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/krate/downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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>> {
let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
conn.interact(move |conn| {
use diesel::dsl::*;
use diesel::sql_types::BigInt;
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/krate/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn follow(
Path(crate_name): Path<String>,
req: Parts,
) -> AppResult<Response> {
let conn = app.db_write_async().await?;
let conn = app.db_write().await?;
conn.interact(move |conn| {
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
let follow = follow_target(&crate_name, conn, user_id)?;
Expand All @@ -44,7 +44,7 @@ pub async fn unfollow(
Path(crate_name): Path<String>,
req: Parts,
) -> AppResult<Response> {
let conn = app.db_write_async().await?;
let conn = app.db_write().await?;
conn.interact(move |conn| {
let user_id = AuthCheck::default().check(&req, conn)?.user_id();
let follow = follow_target(&crate_name, conn, user_id)?;
Expand All @@ -61,7 +61,7 @@ pub async fn following(
Path(crate_name): Path<String>,
req: Parts,
) -> AppResult<Json<Value>> {
let conn = app.db_read_prefer_primary_async().await?;
let conn = app.db_read_prefer_primary().await?;
conn.interact(move |conn| {
use diesel::dsl::exists;

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/krate/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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>> {
let conn = app.db_read_async().await?;
let conn = app.db_read().await?;
conn.interact(move |conn| {
let include = req
.query()
Expand Down Expand Up @@ -227,7 +227,7 @@ pub async fn reverse_dependencies(
Path(name): Path<String>,
req: Parts,
) -> AppResult<Json<Value>> {
let conn = app.db_read_async().await?;
let conn = app.db_read().await?;
conn.interact(move |conn| {
let pagination_options = PaginationOptions::builder().gather(&req)?;

Expand Down
8 changes: 4 additions & 4 deletions src/controllers/krate/owners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::runtime::Handle;

/// Handles the `GET /crates/:crate_id/owners` route.
pub async fn owners(state: AppState, Path(crate_name): Path<String>) -> AppResult<Json<Value>> {
let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
conn.interact(move |conn| {
let krate: Crate = Crate::by_name(&crate_name)
.first(conn)
Expand All @@ -30,7 +30,7 @@ pub async fn owners(state: AppState, Path(crate_name): Path<String>) -> AppResul

/// Handles the `GET /crates/:crate_id/owner_team` route.
pub async fn owner_team(state: AppState, Path(crate_name): Path<String>) -> AppResult<Json<Value>> {
let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
conn.interact(move |conn| {
let krate: Crate = Crate::by_name(&crate_name)
.first(conn)
Expand All @@ -49,7 +49,7 @@ pub async fn owner_team(state: AppState, Path(crate_name): Path<String>) -> AppR

/// Handles the `GET /crates/:crate_id/owner_user` route.
pub async fn owner_user(state: AppState, Path(crate_name): Path<String>) -> AppResult<Json<Value>> {
let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
conn.interact(move |conn| {
let krate: Crate = Crate::by_name(&crate_name)
.first(conn)
Expand Down Expand Up @@ -101,7 +101,7 @@ async fn modify_owners(
) -> AppResult<Json<Value>> {
let logins = body.owners;

let conn = app.db_write_async().await?;
let conn = app.db_write().await?;
conn.interact(move |conn| {
let auth = AuthCheck::default()
.with_endpoint_scope(EndpointScope::ChangeOwners)
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
request_log.add("crate_name", &*metadata.name);
request_log.add("crate_version", &version_string);

let conn = app.db_write_async().await?;
let conn = app.db_write().await?;
conn.interact(move |conn| {

// this query should only be used for the endpoint scope calculation
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/krate/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::sql::{array_agg, canon_crate_name, lower};
/// function out to cover the different use cases, and create unit tests
/// for them.
pub async fn search(app: AppState, req: Parts) -> AppResult<Json<Value>> {
let conn = app.db_read_async().await?;
let conn = app.db_read().await?;
conn.interact(move |conn| {
use diesel::sql_types::Float;
use seek::*;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/krate/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn versions(
Path(crate_name): Path<String>,
req: Parts,
) -> AppResult<Json<Value>> {
let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
conn.interact(move |conn| {
let crate_id: i32 = Crate::by_name(&crate_name)
.select(crates::id)
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn prometheus(app: AppState, Path(kind): Path<String>, req: Parts) ->

let metrics = match kind.as_str() {
"service" => {
let conn = app.db_read_async().await?;
let conn = app.db_read().await?;
conn.interact(move |conn| app.service_metrics.gather(conn))
.await??
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde_json::Value;

/// Handles the `GET /summary` route.
pub async fn summary(state: AppState) -> AppResult<Json<Value>> {
let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
conn.interact(move |conn| {
let config = &state.config;

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::views::EncodableTeam;
pub async fn show_team(state: AppState, Path(name): Path<String>) -> AppResult<Json<Value>> {
use self::teams::dsl::{login, teams};

let conn = state.db_read_async().await?;
let conn = state.db_read().await?;
let team: Team = conn
.interact(move |conn| teams.filter(login.eq(&name)).first(conn))
.await??;
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub async fn list(
Query(params): Query<GetParams>,
req: Parts,
) -> AppResult<Json<Value>> {
let conn = &mut *app.db_read_prefer_primary_async().await?;
let conn = &mut *app.db_read_prefer_primary().await?;
conn.interact(move |conn| {
let auth = AuthCheck::only_cookie().check(&req, conn)?;
let user = auth.user();
Expand All @@ -58,7 +58,7 @@ pub async fn list(

/// Handles the `PUT /me/tokens` route.
pub async fn new(app: AppState, req: BytesRequest) -> AppResult<Json<Value>> {
let conn = &mut *app.db_write_async().await?;
let conn = &mut *app.db_write().await?;
conn.interact(move |conn| {
/// The incoming serialization format for the `ApiToken` model.
#[derive(Deserialize)]
Expand Down Expand Up @@ -142,7 +142,7 @@ pub async fn new(app: AppState, req: BytesRequest) -> AppResult<Json<Value>> {

/// Handles the `DELETE /me/tokens/:id` route.
pub async fn revoke(app: AppState, Path(id): Path<i32>, req: Parts) -> AppResult<Json<Value>> {
let conn = &mut *app.db_write_async().await?;
let conn = &mut *app.db_write().await?;
conn.interact(move |conn| {
let auth = AuthCheck::default().check(&req, conn)?;
let user = auth.user();
Expand All @@ -157,7 +157,7 @@ pub async fn revoke(app: AppState, Path(id): Path<i32>, req: Parts) -> AppResult

/// Handles the `DELETE /tokens/current` route.
pub async fn revoke_current(app: AppState, req: Parts) -> AppResult<Response> {
let conn = &mut *app.db_write_async().await?;
let conn = &mut *app.db_write().await?;
conn.interact(move |conn| {
let auth = AuthCheck::default().check(&req, conn)?;
let api_token_id = auth
Expand Down
Loading