Skip to content

Commit

Permalink
ensure clear works before record loading (#134)
Browse files Browse the repository at this point in the history
Co-authored-by: Max Countryman <[email protected]>
  • Loading branch information
martinetd and maxcountryman authored Jan 4, 2024
1 parent 0cfcec8 commit 7eb5205
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions tower-sessions-core/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,26 @@ impl Session {
/// // Data is cleared...
/// assert!(session.get::<usize>("foo").await.unwrap().is_none());
///
/// // ...data is cleared before loading from the backend...
/// let session = Session::new(session.id(), store.clone(), None);
/// session.clear().await;
/// assert!(session.get::<usize>("foo").await.unwrap().is_none());
///
/// let session = Session::new(session.id(), store, None);
/// // ...but not deleted from the store.
/// // ...but data is not deleted from the store.
/// assert_eq!(session.get::<usize>("foo").await.unwrap(), Some(42));
/// # });
/// ```
pub async fn clear(&self) {
let mut record = self.record.lock().await;
if let Some(record) = record.as_mut() {
let mut record_guard = self.record.lock().await;
if let Some(record) = record_guard.as_mut() {
record.data.clear();
} else if let Some(session_id) = *self.session_id.lock() {
let mut new_record = self.create_record();
new_record.id = session_id;
*record_guard = Some(new_record);
}

self.is_modified.store(true, atomic::Ordering::Release);
}

Expand Down

0 comments on commit 7eb5205

Please sign in to comment.