Skip to content

Commit cb1dbe4

Browse files
jonasmalacofilhojayy-lmao
authored andcommitted
docs(pool): recommend actix-web ThinData over Data to avoid two Arcs (launchbadge#3762)
Both actix_web::web::Data and sqlx::PgPool internally wrap an Arc. Thus, using Data<PgPool> as an extractor in an actix-web route handler results in two Arcs wrapping the data of interest, which isn't ideal. Actix-web 4.9.0 introduced a new web::ThinData extractor for cases like this, where the data is already wrapped in an `Arc` (or is otherwise similarly cheap and sensible to simply clone), which doesn't wrap the inner value in a (second) Arc. Since the new extractor is better suited to the task, suggest it in place of web::Data when giving an example on how to share a pool.
1 parent 72e6826 commit cb1dbe4

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

sqlx-core/src/pool/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ mod options;
109109
/// application/daemon/web server/etc. and then shared with all tasks throughout the process'
110110
/// lifetime. How best to accomplish this depends on your program architecture.
111111
///
112-
/// In Actix-Web, for example, you can share a single pool with all request handlers using [web::Data].
112+
/// In Actix-Web, for example, you can efficiently share a single pool with all request handlers
113+
/// using [web::ThinData].
113114
///
114115
/// Cloning `Pool` is cheap as it is simply a reference-counted handle to the inner pool state.
115116
/// When the last remaining handle to the pool is dropped, the connections owned by the pool are
@@ -131,7 +132,7 @@ mod options;
131132
/// * [PgPool][crate::postgres::PgPool] (PostgreSQL)
132133
/// * [SqlitePool][crate::sqlite::SqlitePool] (SQLite)
133134
///
134-
/// [web::Data]: https://docs.rs/actix-web/3/actix_web/web/struct.Data.html
135+
/// [web::ThinData]: https://docs.rs/actix-web/4.9.0/actix_web/web/struct.ThinData.html
135136
///
136137
/// ### Note: Drop Behavior
137138
/// Due to a lack of async `Drop`, dropping the last `Pool` handle may not immediately clean

0 commit comments

Comments
 (0)