Skip to content

Commit 5da70d2

Browse files
committedJul 3, 2023
fix: Make connection Arced
1 parent 23fbc4f commit 5da70d2

File tree

2 files changed

+54
-57
lines changed

2 files changed

+54
-57
lines changed
 

‎src/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl Default for CacheConfiguration {
8888

8989
#[derive(Clone)]
9090
pub struct SqlxPostgresqlSessionStore {
91-
client_pool: Pool<Postgres>,
91+
client_pool: Arc<Pool<Postgres>>,
9292
configuration: CacheConfiguration,
9393
}
9494

@@ -117,7 +117,7 @@ impl SqlxPostgresqlSessionStore {
117117
Self::builder(connection_string).build().await
118118
}
119119

120-
pub async fn from_pool(pool: Pool<Postgres>) -> SqlxPostgresqlSessionStoreBuilder {
120+
pub async fn from_pool(pool: Arc<Pool<Postgres>>) -> SqlxPostgresqlSessionStoreBuilder {
121121
SqlxPostgresqlSessionStoreBuilder {
122122
connection_data: ConnectionPool(pool),
123123
configuration: CacheConfiguration::default(),
@@ -127,7 +127,7 @@ impl SqlxPostgresqlSessionStore {
127127

128128
pub enum ConnectionData {
129129
ConnectionString(String),
130-
ConnectionPool(Pool<Postgres>),
130+
ConnectionPool(Arc<Pool<Postgres>>),
131131
}
132132

133133
#[must_use]
@@ -145,7 +145,7 @@ impl SqlxPostgresqlSessionStoreBuilder {
145145
.await
146146
.map_err(Into::into)
147147
.map(|pool| SqlxPostgresqlSessionStore {
148-
client_pool: pool,
148+
client_pool: Arc::new(pool),
149149
configuration: self.configuration,
150150
}),
151151
ConnectionPool(pool) => Ok(SqlxPostgresqlSessionStore {
@@ -164,7 +164,7 @@ impl SessionStore for SqlxPostgresqlSessionStore {
164164
let row =
165165
sqlx::query("SELECT session_state FROM sessions WHERE key = $1 AND expires > NOW()")
166166
.bind(key)
167-
.fetch_optional(&self.client_pool)
167+
.fetch_optional(self.client_pool.as_ref())
168168
.await
169169
.map_err(Into::into)
170170
.map_err(LoadError::Other)?;
@@ -195,7 +195,7 @@ impl SessionStore for SqlxPostgresqlSessionStore {
195195
.bind(cache_key)
196196
.bind(body)
197197
.bind(expires)
198-
.execute(&self.client_pool)
198+
.execute(self.client_pool.as_ref())
199199
.await
200200
.map_err(Into::into)
201201
.map_err(SaveError::Other)?;
@@ -217,7 +217,7 @@ impl SessionStore for SqlxPostgresqlSessionStore {
217217
.bind(body)
218218
.bind(new_expires)
219219
.bind(cache_key)
220-
.execute(&self.client_pool)
220+
.execute(self.client_pool.as_ref())
221221
.await
222222
.map_err(Into::into)
223223
.map_err(UpdateError::Other)?;
@@ -234,7 +234,7 @@ impl SessionStore for SqlxPostgresqlSessionStore {
234234
sqlx::query("UPDATE sessions SET expires = $1 WHERE key = $2")
235235
.bind(new_expires)
236236
.bind(key)
237-
.execute(&self.client_pool)
237+
.execute(self.client_pool.as_ref())
238238
.await
239239
.map_err(Into::into)
240240
.map_err(UpdateError::Other)?;
@@ -245,7 +245,7 @@ impl SessionStore for SqlxPostgresqlSessionStore {
245245
let key = (self.configuration.cache_keygen)(session_key.as_ref());
246246
sqlx::query("DELETE FROM sessions WHERE key = $1")
247247
.bind(key)
248-
.execute(&self.client_pool)
248+
.execute(self.client_pool.as_ref())
249249
.await
250250
.map_err(Into::into)
251251
.map_err(UpdateError::Other)?;

‎tests/session_store.rs

+45-48
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,54 @@
11
mod test_helpers;
2-
#[cfg(test)]
3-
pub mod tests {
4-
use actix_session::storage::SessionStore;
5-
use actix_session_sqlx_postgres::SqlxPostgresqlSessionStore;
6-
use sqlx::postgres::PgPoolOptions;
7-
use std::collections::HashMap;
8-
use testcontainers::clients;
9-
use testcontainers::core::WaitFor;
10-
use testcontainers::images::generic;
2+
use actix_session::storage::SessionStore;
3+
use actix_session_sqlx_postgres::SqlxPostgresqlSessionStore;
4+
use sqlx::postgres::PgPoolOptions;
5+
use std::collections::HashMap;
6+
use testcontainers::clients;
7+
use testcontainers::core::WaitFor;
8+
use testcontainers::images::generic;
119

12-
async fn postgres_store(url: String) -> SqlxPostgresqlSessionStore {
13-
SqlxPostgresqlSessionStore::new(url).await.expect("")
14-
}
10+
async fn postgres_store(url: String) -> SqlxPostgresqlSessionStore {
11+
SqlxPostgresqlSessionStore::new(url).await.expect("")
12+
}
1513

16-
#[actix_web::test]
17-
async fn test_session_workflow() {
18-
let docker = clients::Cli::default();
19-
let postgres = docker.run(
20-
generic::GenericImage::new("postgres", "14-alpine")
21-
.with_wait_for(WaitFor::message_on_stderr(
22-
"database system is ready to accept connections",
23-
))
24-
.with_env_var("POSTGRES_DB", "sessions")
25-
.with_env_var("POSTGRES_PASSWORD", "example")
26-
.with_env_var("POSTGRES_HOST_AUTH_METHOD", "trust")
27-
.with_env_var("POSTGRES_USER", "tests"),
28-
);
29-
let url = format!(
30-
"postgres://tests:example@localhost:{}/sessions",
31-
postgres.get_host_port_ipv4(5432)
32-
);
14+
#[actix_web::test]
15+
async fn test_session_workflow() {
16+
let docker = clients::Cli::default();
17+
let postgres = docker.run(
18+
generic::GenericImage::new("postgres", "15-alpine")
19+
.with_wait_for(WaitFor::message_on_stderr(
20+
"database system is ready to accept connections",
21+
))
22+
.with_env_var("POSTGRES_DB", "sessions")
23+
.with_env_var("POSTGRES_PASSWORD", "example")
24+
.with_env_var("POSTGRES_HOST_AUTH_METHOD", "trust")
25+
.with_env_var("POSTGRES_USER", "tests"),
26+
);
27+
let url = format!(
28+
"postgres://tests:example@localhost:{}/sessions",
29+
postgres.get_host_port_ipv4(5432)
30+
);
3331

34-
let postgres_store = postgres_store(url.clone()).await;
35-
let pool = PgPoolOptions::new()
36-
.max_connections(1)
37-
.connect(url.as_str())
38-
.await
39-
.expect("Could not connect to database");
40-
sqlx::query(
41-
r#"CREATE TABLE sessions(
32+
let postgres_store = postgres_store(url.clone()).await;
33+
let pool = PgPoolOptions::new()
34+
.max_connections(1)
35+
.connect(url.as_str())
36+
.await
37+
.expect("Could not connect to database");
38+
sqlx::query(
39+
r#"CREATE TABLE sessions(
4240
key TEXT PRIMARY KEY NOT NULL,
4341
session_state JSONB,
4442
expires TIMESTAMP WITH TIME ZONE NOT NULL
4543
);"#,
46-
)
47-
.execute(&pool)
48-
.await
49-
.expect("Could not create table");
50-
let mut session = HashMap::new();
51-
session.insert("key".to_string(), "value".to_string());
52-
let data = postgres_store.save(session, &time::Duration::days(1)).await;
53-
println!("{:#?}", data);
54-
assert!(data.is_ok());
55-
super::test_helpers::acceptance_test_suite(move || postgres_store.clone(), true).await;
56-
}
44+
)
45+
.execute(&pool)
46+
.await
47+
.expect("Could not create table");
48+
let mut session = HashMap::new();
49+
session.insert("key".to_string(), "value".to_string());
50+
let data = postgres_store.save(session, &time::Duration::days(1)).await;
51+
println!("{:#?}", data);
52+
assert!(data.is_ok());
53+
test_helpers::acceptance_test_suite(move || postgres_store.clone(), true).await;
5754
}

0 commit comments

Comments
 (0)