Skip to content

Commit 038a60f

Browse files
committed
drops
1 parent 45578b4 commit 038a60f

File tree

5 files changed

+88
-45
lines changed

5 files changed

+88
-45
lines changed

crates/bin/docs_rs_web/src/handlers/build_details.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ pub(crate) async fn build_details_handler(
105105
.await?
106106
.ok_or(AxumNope::BuildNotFound)?;
107107

108+
let metadata = MetaData::from_crate(
109+
&mut conn,
110+
params.name(),
111+
&version,
112+
Some(params.req_version().clone()),
113+
)
114+
.await?;
115+
let params = params.apply_metadata(&metadata);
116+
117+
// NOTE: we want to give back the db connection to the pool
118+
// before we do the long S3 requests.
119+
drop(conn);
120+
108121
let (output, all_log_filenames, current_filename) = if let Some(output) = row.output {
109122
// legacy case, for old builds the build log was stored in the database.
110123
(output, Vec::new(), None)
@@ -155,15 +168,6 @@ pub(crate) async fn build_details_handler(
155168
(file_content, all_log_filenames, current_filename)
156169
};
157170

158-
let metadata = MetaData::from_crate(
159-
&mut conn,
160-
params.name(),
161-
&version,
162-
Some(params.req_version().clone()),
163-
)
164-
.await?;
165-
let params = params.apply_metadata(&metadata);
166-
167171
Ok(BuildDetailsPage {
168172
metadata,
169173
build_details: BuildDetails {

crates/bin/docs_rs_web/src/handlers/crate_details.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,14 +469,18 @@ pub(crate) async fn crate_details_handler(
469469

470470
let mut details = CrateDetails::from_matched_release(&mut conn, matched_release).await?;
471471

472+
let build_statistics =
473+
BuildStatistics::fetch_for_release(&mut conn, details.crate_id, details.release_id).await?;
474+
475+
// NOTE: we want to give back the db connection to the pool
476+
// before we do the long S3 requests.
477+
drop(conn);
478+
472479
match details.fetch_readme(&storage).await {
473480
Ok(readme) => details.readme = readme.or(details.readme),
474481
Err(e) => warn!(?e, "error fetching readme"),
475482
}
476483

477-
let build_statistics =
478-
BuildStatistics::fetch_for_release(&mut conn, details.crate_id, details.release_id).await?;
479-
480484
let CrateDetails {
481485
version,
482486
name,

crates/bin/docs_rs_web/src/handlers/rustdoc.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use axum_extra::{
3434
typed_header::TypedHeader,
3535
};
3636
use docs_rs_cargo_metadata::Dependency;
37+
use docs_rs_database::Pool;
3738
use docs_rs_headers::{ETagComputer, IfNoneMatch, X_ROBOTS_TAG};
3839
use docs_rs_registry_api::OwnerKind;
3940
use docs_rs_rustdoc_json::RustdocJsonFormatVersion;
@@ -186,13 +187,13 @@ pub(crate) struct RustdocRedirectorParams {
186187
/// Handler called for `/:crate` and `/:crate/:version` URLs. Automatically redirects to the docs
187188
/// or crate details page based on whether the given crate version was successfully built.
188189
#[allow(clippy::too_many_arguments)]
189-
#[instrument(skip(storage, conn))]
190+
#[instrument(skip(storage, pool))]
190191
pub(crate) async fn rustdoc_redirector_handler(
191192
Path(params): Path<RustdocRedirectorParams>,
192193
original_uri: Uri,
193194
matched_path: MatchedPath,
194195
Extension(storage): Extension<Arc<AsyncStorage>>,
195-
mut conn: DbConnection,
196+
Extension(pool): Extension<Pool>,
196197
if_none_match: Option<TypedHeader<IfNoneMatch>>,
197198
RawQuery(original_query): RawQuery,
198199
) -> AxumResult<impl IntoResponse> {
@@ -291,6 +292,9 @@ pub(crate) async fn rustdoc_redirector_handler(
291292
.map_err(AxumNope::BadRequest)?
292293
.with_page_kind(PageKind::Rustdoc);
293294

295+
// NOTE: we try to shorten the time we hold the database connection from the pool.
296+
let mut conn = pool.get_async().await?;
297+
294298
// it doesn't matter if the version that was given was exact or not, since we're redirecting
295299
// anyway
296300
let matched_release = match_version(&mut conn, &crate_name, &params.req_version().clone())
@@ -316,6 +320,10 @@ pub(crate) async fn rustdoc_redirector_handler(
316320
return async {
317321
let krate = CrateDetails::from_matched_release(&mut conn, matched_release).await?;
318322

323+
// NOTE: we want to give back the db connection to the pool
324+
// before we do the long S3 requests.
325+
drop(conn);
326+
319327
match storage
320328
.stream_rustdoc_file(
321329
params.name(),
@@ -597,6 +605,10 @@ pub(crate) async fn rustdoc_html_server_handler(
597605

598606
let krate = CrateDetails::from_matched_release(&mut conn, matched_release).await?;
599607

608+
// NOTE: we want to give back the db connection to the pool
609+
// before we do the long S3 requests.
610+
drop(conn);
611+
600612
trace!(
601613
?params,
602614
doc_targets=?krate.metadata.doc_targets,
@@ -784,9 +796,13 @@ pub(crate) async fn target_redirect_handler(
784796
.await?
785797
.into_canonical_req_version_or_else(|_, _| AxumNope::VersionNotFound)?;
786798
let params = params.apply_matched_release(&matched_release);
799+
trace!(?params, "parsed params");
787800

788801
let crate_details = CrateDetails::from_matched_release(&mut conn, matched_release).await?;
789-
trace!(?params, "parsed params");
802+
803+
// NOTE: we want to give back the db connection to the pool
804+
// before we do the long S3 requests.
805+
drop(conn);
790806

791807
let storage_path = params.storage_path();
792808
trace!(storage_path, "checking if path exists in other version");
@@ -893,6 +909,10 @@ pub(crate) async fn json_download_handler(
893909

894910
let krate = CrateDetails::from_matched_release(&mut conn, matched_release).await?;
895911

912+
// NOTE: we want to give back the db connection to the pool
913+
// before we do the long S3 requests.
914+
drop(conn);
915+
896916
let wanted_format_version = if let Some(request_format_version) = json_params.format_version {
897917
// axum doesn't support extension suffixes in the route yet, not as parameter, and not
898918
// statically, when combined with a parameter (like `.../{format_version}.gz`).
@@ -1001,6 +1021,11 @@ pub(crate) async fn download_handler(
10011021
CachePolicy::ForeverInCdn(confirmed_name.into()),
10021022
)
10031023
})?;
1024+
1025+
// NOTE: we want to give back the db connection to the pool
1026+
// before we do the long S3 requests.
1027+
drop(conn);
1028+
10041029
params = params.apply_matched_release(&matched_release);
10051030

10061031
let version = &matched_release.release.version;

crates/bin/docs_rs_web/src/handlers/source.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,29 @@ pub(crate) async fn source_browser_handler(
246246

247247
let inner_path = params.inner_path();
248248

249+
let current_folder = if let Some(last_slash_pos) = inner_path.rfind('/') {
250+
&inner_path[..last_slash_pos + 1]
251+
} else {
252+
""
253+
};
254+
let show_parent_link = !current_folder.is_empty();
255+
256+
let file_list = FileList::from_path(&mut conn, params.name(), version, current_folder)
257+
.await?
258+
.unwrap_or_default();
259+
260+
let metadata = MetaData::from_crate(
261+
&mut conn,
262+
params.name(),
263+
version,
264+
Some(params.req_version().clone()),
265+
)
266+
.await?;
267+
268+
// NOTE: we want to give back the db connection to the pool
269+
// before we do the long S3 requests.
270+
drop(conn);
271+
249272
// try to get actual file first
250273
// skip if request is a directory
251274
let stream = if !params.path_is_folder() {
@@ -326,25 +349,6 @@ pub(crate) async fn source_browser_handler(
326349
(None, None)
327350
};
328351

329-
let current_folder = if let Some(last_slash_pos) = inner_path.rfind('/') {
330-
&inner_path[..last_slash_pos + 1]
331-
} else {
332-
""
333-
};
334-
let show_parent_link = !current_folder.is_empty();
335-
336-
let file_list = FileList::from_path(&mut conn, params.name(), version, current_folder)
337-
.await?
338-
.unwrap_or_default();
339-
340-
let metadata = MetaData::from_crate(
341-
&mut conn,
342-
params.name(),
343-
version,
344-
Some(params.req_version().clone()),
345-
)
346-
.await?;
347-
348352
Ok(SourcePage {
349353
file_list,
350354
metadata,

crates/lib/docs_rs_database/src/pool.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,38 @@ impl Pool {
4848
let max_lifetime = Duration::from_secs(30 * 60);
4949
let idle_timeout = Duration::from_secs(10 * 60);
5050

51-
let async_pool = PgPoolOptions::new()
51+
let mut options = PgPoolOptions::new()
5252
.max_connections(config.max_pool_size)
5353
.min_connections(config.min_pool_idle)
5454
.max_lifetime(max_lifetime)
5555
.acquire_timeout(acquire_timeout)
56-
.idle_timeout(idle_timeout)
57-
.after_connect({
56+
.idle_timeout(idle_timeout);
57+
58+
if cfg!(test) {
59+
options = options.test_before_acquire(false);
60+
}
61+
62+
if schema != DEFAULT_SCHEMA {
63+
options = options.after_connect({
5864
let schema = schema.to_owned();
5965
move |conn, _meta| {
6066
Box::pin({
6167
let schema = schema.clone();
6268

6369
async move {
64-
if schema != DEFAULT_SCHEMA {
65-
conn.execute(
66-
format!("SET search_path TO {schema}, {DEFAULT_SCHEMA};")
67-
.as_str(),
68-
)
69-
.await?;
70-
}
70+
conn.execute(
71+
format!("SET search_path TO {schema}, {DEFAULT_SCHEMA};").as_str(),
72+
)
73+
.await?;
7174

7275
Ok(())
7376
}
7477
})
7578
}
76-
})
79+
});
80+
}
81+
82+
let async_pool = options
7783
.connect_lazy(&config.database_url)
7884
.map_err(PoolError::AsyncPoolCreationFailed)?;
7985

0 commit comments

Comments
 (0)