Skip to content

Commit 7c9ff9b

Browse files
authored
Merge pull request #22 from jbr/make-async-std-optional
upgrade sqlx, make async_std optional
2 parents 5eec414 + f10e245 commit 7c9ff9b

File tree

6 files changed

+86
-36
lines changed

6 files changed

+86
-36
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ jobs:
5959
uses: actions-rs/cargo@v1
6060
with:
6161
command: test
62-
args: --all --features pg -- --test-threads=1
62+
args: --all --features pg,async_std -- --test-threads=1
6363

6464
- name: sqlite tests
6565
uses: actions-rs/cargo@v1
6666
with:
6767
command: test
68-
args: --all --features sqlite
68+
args: --all --features sqlite,async_std
6969

7070
check_fmt_and_docs:
7171
name: Checking fmt, clippy, and docs

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@ keywords = ["sessions", "tide", "async-session", "sqlx", "sqlite"]
1212
categories = ["web-programming::http-server", "web-programming", "database"]
1313

1414
[package.metadata.docs.rs]
15-
features = ["pg", "sqlite"]
15+
features = ["pg", "sqlite", "async_std"]
1616

1717
[features]
1818
default = ["native-tls"]
1919
sqlite = ["sqlx/sqlite"]
2020
pg = ["sqlx/postgres", "sqlx/json"]
2121
native-tls = ["sqlx/runtime-async-std-native-tls"]
2222
rustls = ["sqlx/runtime-async-std-rustls"]
23+
async_std = ["async-std"]
2324

2425
[dependencies]
25-
async-session = "2.0.1"
26-
sqlx = { version = "0.5.1", features = ["chrono"] }
27-
async-std = "1.9.0"
26+
async-session = "3.0.0"
27+
sqlx = { version = "0.5.5", features = ["chrono"] }
28+
async-std = { version = "1.9.0", optional = true }
2829

2930
[dev-dependencies]
3031
async-std = { version = "1.9.0", features = ["attributes"] }

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@
1919
### sqlite:
2020

2121
```toml
22-
async-sqlx-session = { version = "0.2.0", features = ["sqlite"] }
22+
async-sqlx-session = { version = "0.3.0", features = ["sqlite"] }
2323
```
2424

2525
### postgres:
2626

2727
```toml
28-
async-sqlx-session = { version = "0.2.0", features = ["pg"] }
28+
async-sqlx-session = { version = "0.3.0", features = ["pg"] }
2929
```
3030

31+
### Optional `async_std` feature
32+
33+
To use the `spawn_cleanup_task` function on async-std, enable the
34+
`async_std` feature.
35+
36+
```toml
37+
async-sqlx-session = { version = "0.3.0", features = ["pg", "async_std"] }
38+
```
3139

3240

3341
## Cargo Features:

src/lib.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
1-
//! # async-sqlx-session
2-
//!
3-
//! This crate currently provides two session stores: [`PostgresSessionStore`] and [`SqliteSessionStore`], each of which is enabled by a feature flag.
4-
//! To use `SqliteSessionStore`, enable the `sqlite` feature on this crate.
5-
//! To use `PostgresSessionStore`, enable the `pg` feature on this crate.
1+
/*!
2+
# async-sqlx-session
3+
4+
This crate currently provides two session stores:
5+
[`PostgresSessionStore`] and [`SqliteSessionStore`], each of which
6+
is enabled by a feature flag.
7+
8+
To use [`SqliteSessionStore`], enable the `sqlite` feature on this
9+
crate.
10+
11+
To use [`PostgresSessionStore`], enable the `pg` feature on this
12+
crate.
13+
14+
To use the `spawn_cleanup_task` function for either store on
15+
async-std, enable the `async_std` feature. To perform session cleanup
16+
intermittently with a different runtime, use a function like:
17+
18+
```rust,ignore
19+
fn clean_up_intermittently(store: &SqliteSessionStore, period: Duration) {
20+
let store = store.clone();
21+
other_runtime::spawn(async move {
22+
loop {
23+
other_runtime::sleep(period).await;
24+
if let Err(error) = store.cleanup().await {
25+
log::error!("cleanup error: {}", error);
26+
}
27+
}
28+
});
29+
}
30+
```
31+
*/
32+
633
#![forbid(unsafe_code, future_incompatible)]
734
#![deny(
835
missing_debug_implementations,

src/pg.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use async_session::{async_trait, chrono::Utc, log, serde_json, Result, Session, SessionStore};
2-
use async_std::task;
32
use sqlx::{pool::PoolConnection, Executor, PgPool, Postgres};
4-
use std::time::Duration;
53

64
/// sqlx postgres session store for async-sessions
75
///
@@ -14,7 +12,9 @@ use std::time::Duration;
1412
/// let store = PostgresSessionStore::new(&std::env::var("PG_TEST_DB_URL").unwrap()).await?;
1513
/// store.migrate().await?;
1614
/// # store.clear_store().await?;
15+
/// # #[cfg(feature = "async_std")] {
1716
/// store.spawn_cleanup_task(Duration::from_secs(60 * 60));
17+
/// # }
1818
///
1919
/// let mut session = Session::new();
2020
/// session.insert("key", vec![1,2,3]);
@@ -174,7 +174,9 @@ impl PostgresSessionStore {
174174
}
175175

176176
/// Spawns an async_std::task that clears out stale (expired)
177-
/// sessions on a periodic basis.
177+
/// sessions on a periodic basis. Only available with the
178+
/// async_std feature enabled.
179+
///
178180
/// ```rust,no_run
179181
/// # use async_sqlx_session::PostgresSessionStore;
180182
/// # use async_session::{Result, SessionStore, Session};
@@ -193,7 +195,12 @@ impl PostgresSessionStore {
193195
/// # join_handle.cancel().await;
194196
/// # Ok(()) }) }
195197
/// ```
196-
pub fn spawn_cleanup_task(&self, period: Duration) -> task::JoinHandle<()> {
198+
#[cfg(feature = "async_std")]
199+
pub fn spawn_cleanup_task(
200+
&self,
201+
period: std::time::Duration,
202+
) -> async_std::task::JoinHandle<()> {
203+
use async_std::task;
197204
let store = self.clone();
198205
task::spawn(async move {
199206
loop {
@@ -326,6 +333,7 @@ impl SessionStore for PostgresSessionStore {
326333
mod tests {
327334
use super::*;
328335
use async_session::chrono::DateTime;
336+
use std::time::Duration;
329337

330338
async fn test_store() -> PostgresSessionStore {
331339
let store = PostgresSessionStore::new(&std::env::var("PG_TEST_DB_URL").unwrap())
@@ -458,7 +466,7 @@ mod tests {
458466

459467
assert!(!loaded_session.is_expired());
460468

461-
task::sleep(Duration::from_secs(1)).await;
469+
async_std::task::sleep(Duration::from_secs(1)).await;
462470
assert_eq!(None, store.load_session(cookie_value).await?);
463471

464472
Ok(())

src/sqlite.rs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use async_session::{async_trait, chrono::Utc, log, serde_json, Result, Session, SessionStore};
2-
use async_std::task;
32
use sqlx::{pool::PoolConnection, sqlite::SqlitePool, Sqlite};
4-
use std::time::Duration;
53

64
/// sqlx sqlite session store for async-sessions
75
///
@@ -11,8 +9,9 @@ use std::time::Duration;
119
/// use std::time::Duration;
1210
///
1311
/// # fn main() -> async_session::Result { async_std::task::block_on(async {
14-
/// let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?;
12+
/// let store = SqliteSessionStore::new("sqlite::memory:").await?;
1513
/// store.migrate().await?;
14+
/// # #[cfg(feature = "async_std")]
1615
/// store.spawn_cleanup_task(Duration::from_secs(60 * 60));
1716
///
1817
/// let mut session = Session::new();
@@ -39,7 +38,7 @@ impl SqliteSessionStore {
3938
/// # use async_sqlx_session::SqliteSessionStore;
4039
/// # use async_session::Result;
4140
/// # fn main() -> Result { async_std::task::block_on(async {
42-
/// let pool = sqlx::SqlitePool::connect("sqlite:%3Amemory:").await.unwrap();
41+
/// let pool = sqlx::SqlitePool::connect("sqlite::memory:").await.unwrap();
4342
/// let store = SqliteSessionStore::from_client(pool)
4443
/// .with_table_name("custom_table_name");
4544
/// store.migrate().await;
@@ -66,7 +65,7 @@ impl SqliteSessionStore {
6665
/// # use async_sqlx_session::SqliteSessionStore;
6766
/// # use async_session::Result;
6867
/// # fn main() -> Result { async_std::task::block_on(async {
69-
/// let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?;
68+
/// let store = SqliteSessionStore::new("sqlite::memory:").await?;
7069
/// store.migrate().await;
7170
/// # Ok(()) }) }
7271
/// ```
@@ -85,7 +84,7 @@ impl SqliteSessionStore {
8584
/// # use async_sqlx_session::SqliteSessionStore;
8685
/// # use async_session::Result;
8786
/// # fn main() -> Result { async_std::task::block_on(async {
88-
/// let store = SqliteSessionStore::new_with_table_name("sqlite:%3Amemory:", "custom_table_name").await?;
87+
/// let store = SqliteSessionStore::new_with_table_name("sqlite::memory:", "custom_table_name").await?;
8988
/// store.migrate().await;
9089
/// # Ok(()) }) }
9190
/// ```
@@ -99,7 +98,7 @@ impl SqliteSessionStore {
9998
/// # use async_sqlx_session::SqliteSessionStore;
10099
/// # use async_session::Result;
101100
/// # fn main() -> Result { async_std::task::block_on(async {
102-
/// let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?
101+
/// let store = SqliteSessionStore::new("sqlite::memory:").await?
103102
/// .with_table_name("custom_name");
104103
/// store.migrate().await;
105104
/// # Ok(()) }) }
@@ -109,7 +108,7 @@ impl SqliteSessionStore {
109108
/// # use async_sqlx_session::SqliteSessionStore;
110109
/// # use async_session::Result;
111110
/// # fn main() -> Result { async_std::task::block_on(async {
112-
/// let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?
111+
/// let store = SqliteSessionStore::new("sqlite::memory:").await?
113112
/// .with_table_name("johnny (); drop users;");
114113
/// # Ok(()) }) }
115114
/// ```
@@ -139,7 +138,7 @@ impl SqliteSessionStore {
139138
/// # use async_sqlx_session::SqliteSessionStore;
140139
/// # use async_session::{Result, SessionStore, Session};
141140
/// # fn main() -> Result { async_std::task::block_on(async {
142-
/// let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?;
141+
/// let store = SqliteSessionStore::new("sqlite::memory:").await?;
143142
/// assert!(store.count().await.is_err());
144143
/// store.migrate().await?;
145144
/// store.store_session(Session::new()).await?;
@@ -177,13 +176,15 @@ impl SqliteSessionStore {
177176
}
178177

179178
/// Spawns an async_std::task that clears out stale (expired)
180-
/// sessions on a periodic basis.
179+
/// sessions on a periodic basis. Only available with the
180+
/// async_std feature enabled.
181+
///
181182
/// ```rust,no_run
182183
/// # use async_sqlx_session::SqliteSessionStore;
183184
/// # use async_session::{Result, SessionStore, Session};
184185
/// # use std::time::Duration;
185186
/// # fn main() -> Result { async_std::task::block_on(async {
186-
/// let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?;
187+
/// let store = SqliteSessionStore::new("sqlite::memory:").await?;
187188
/// store.migrate().await?;
188189
/// # let join_handle =
189190
/// store.spawn_cleanup_task(Duration::from_secs(1));
@@ -196,11 +197,15 @@ impl SqliteSessionStore {
196197
/// # join_handle.cancel().await;
197198
/// # Ok(()) }) }
198199
/// ```
199-
pub fn spawn_cleanup_task(&self, period: Duration) -> task::JoinHandle<()> {
200+
#[cfg(feature = "async_std")]
201+
pub fn spawn_cleanup_task(
202+
&self,
203+
period: std::time::Duration,
204+
) -> async_std::task::JoinHandle<()> {
200205
let store = self.clone();
201-
task::spawn(async move {
206+
async_std::task::spawn(async move {
202207
loop {
203-
task::sleep(period).await;
208+
async_std::task::sleep(period).await;
204209
if let Err(error) = store.cleanup().await {
205210
log::error!("cleanup error: {}", error);
206211
}
@@ -214,7 +219,7 @@ impl SqliteSessionStore {
214219
/// # use async_sqlx_session::SqliteSessionStore;
215220
/// # use async_session::{chrono::{Utc,Duration}, Result, SessionStore, Session};
216221
/// # fn main() -> Result { async_std::task::block_on(async {
217-
/// let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?;
222+
/// let store = SqliteSessionStore::new("sqlite::memory:").await?;
218223
/// store.migrate().await?;
219224
/// let mut session = Session::new();
220225
/// session.set_expiry(Utc::now() - Duration::seconds(5));
@@ -247,7 +252,7 @@ impl SqliteSessionStore {
247252
/// # use async_session::{Result, SessionStore, Session};
248253
/// # use std::time::Duration;
249254
/// # fn main() -> Result { async_std::task::block_on(async {
250-
/// let store = SqliteSessionStore::new("sqlite:%3Amemory:").await?;
255+
/// let store = SqliteSessionStore::new("sqlite::memory:").await?;
251256
/// store.migrate().await?;
252257
/// assert_eq!(store.count().await?, 0);
253258
/// store.store_session(Session::new()).await?;
@@ -342,9 +347,10 @@ impl SessionStore for SqliteSessionStore {
342347
#[cfg(test)]
343348
mod tests {
344349
use super::*;
350+
use std::time::Duration;
345351

346352
async fn test_store() -> SqliteSessionStore {
347-
let store = SqliteSessionStore::new("sqlite:%3Amemory:")
353+
let store = SqliteSessionStore::new("sqlite::memory:")
348354
.await
349355
.expect("building a sqlite :memory: SqliteSessionStore");
350356
store
@@ -468,7 +474,7 @@ mod tests {
468474

469475
assert!(!loaded_session.is_expired());
470476

471-
task::sleep(Duration::from_secs(1)).await;
477+
async_std::task::sleep(Duration::from_secs(1)).await;
472478
assert_eq!(None, store.load_session(cookie_value).await?);
473479

474480
Ok(())

0 commit comments

Comments
 (0)