-
Notifications
You must be signed in to change notification settings - Fork 95
Enforce single-owner contract on InMemoryPersister #1534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -809,25 +809,21 @@ pub trait AsyncSessionPersister: Send + Sync { | |||||
| } | ||||||
|
|
||||||
| /// In-memory session persister for replaying sessions and introspecting events. | ||||||
| #[derive(Clone)] | ||||||
| pub struct InMemoryPersister<V> { | ||||||
| pub(crate) inner: std::sync::Arc<std::sync::RwLock<InnerStorage<V>>>, | ||||||
| pub(crate) inner: std::sync::Mutex<InnerStorage<V>>, | ||||||
| } | ||||||
|
|
||||||
| impl<V> Default for InMemoryPersister<V> { | ||||||
| fn default() -> Self { | ||||||
| Self { inner: std::sync::Arc::new(std::sync::RwLock::new(InnerStorage::default())) } | ||||||
| } | ||||||
| fn default() -> Self { Self { inner: std::sync::Mutex::new(InnerStorage::default()) } } | ||||||
| } | ||||||
|
|
||||||
| #[derive(Clone)] | ||||||
| pub(crate) struct InnerStorage<V> { | ||||||
| pub(crate) events: std::sync::Arc<Vec<V>>, | ||||||
| pub(crate) events: Vec<V>, | ||||||
| pub(crate) is_closed: bool, | ||||||
| } | ||||||
|
|
||||||
| impl<V> Default for InnerStorage<V> { | ||||||
| fn default() -> Self { Self { events: std::sync::Arc::new(vec![]), is_closed: false } } | ||||||
| fn default() -> Self { Self { events: vec![], is_closed: false } } | ||||||
| } | ||||||
|
|
||||||
| impl<V> SessionPersister for InMemoryPersister<V> | ||||||
|
|
@@ -838,40 +834,32 @@ where | |||||
| type SessionEvent = V; | ||||||
|
|
||||||
| fn save_event(&self, event: Self::SessionEvent) -> Result<(), Self::InternalStorageError> { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
maybe it makes sense to change the trait definition... @arminsabouri ? thoughts?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested Workarounds (split native vs FFI traits , or
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC we decided to go with &self bc it simplified stuff at the FFI level. Otherwise you would have to wrap the callbacks with a mutex. Regardless I agree with Dan and we should ticket this up and revisit this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we even need to ticket it? As both of our comments suggest, this was a deliberate decision already compared to known alternatives, not tech-debt. Please ticket if I'm missing something. |
||||||
| let mut inner = self.inner.write().expect("Lock should not be poisoned"); | ||||||
| std::sync::Arc::make_mut(&mut inner.events).push(event); | ||||||
| self.inner.lock().expect("Lock should not be poisoned").events.push(event); | ||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| fn load( | ||||||
| &self, | ||||||
| ) -> Result<Box<dyn Iterator<Item = Self::SessionEvent>>, Self::InternalStorageError> { | ||||||
| let inner = self.inner.read().expect("Lock should not be poisoned"); | ||||||
| let events = std::sync::Arc::clone(&inner.events); | ||||||
| Ok(Box::new( | ||||||
| std::sync::Arc::try_unwrap(events).unwrap_or_else(|arc| (*arc).clone()).into_iter(), | ||||||
| )) | ||||||
| let events = self.inner.lock().expect("Lock should not be poisoned").events.clone(); | ||||||
| Ok(Box::new(events.into_iter())) | ||||||
| } | ||||||
|
|
||||||
| fn close(&self) -> Result<(), Self::InternalStorageError> { | ||||||
| let mut inner = self.inner.write().expect("Lock should not be poisoned"); | ||||||
| inner.is_closed = true; | ||||||
| self.inner.lock().expect("Lock should not be poisoned").is_closed = true; | ||||||
| Ok(()) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| #[cfg(test)] | ||||||
| #[derive(Clone)] | ||||||
| /// Async in-memory session persister for replaying async sessions and introspecting events. | ||||||
| pub struct InMemoryAsyncPersister<V> { | ||||||
| pub(crate) inner: std::sync::Arc<tokio::sync::RwLock<InnerStorage<V>>>, | ||||||
| pub(crate) inner: tokio::sync::Mutex<InnerStorage<V>>, | ||||||
| } | ||||||
|
|
||||||
| #[cfg(test)] | ||||||
| impl<V> Default for InMemoryAsyncPersister<V> { | ||||||
| fn default() -> Self { | ||||||
| Self { inner: std::sync::Arc::new(tokio::sync::RwLock::new(InnerStorage::default())) } | ||||||
| } | ||||||
| fn default() -> Self { Self { inner: tokio::sync::Mutex::new(InnerStorage::default()) } } | ||||||
| } | ||||||
|
|
||||||
| #[cfg(test)] | ||||||
|
|
@@ -886,25 +874,20 @@ where | |||||
| &self, | ||||||
| event: Self::SessionEvent, | ||||||
| ) -> Result<(), Self::InternalStorageError> { | ||||||
| let mut inner = self.inner.write().await; | ||||||
| std::sync::Arc::make_mut(&mut inner.events).push(event); | ||||||
| self.inner.lock().await.events.push(event); | ||||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| async fn load( | ||||||
| &self, | ||||||
| ) -> Result<Box<dyn Iterator<Item = Self::SessionEvent> + Send>, Self::InternalStorageError> | ||||||
| { | ||||||
| let inner = self.inner.read().await; | ||||||
| let events = std::sync::Arc::clone(&inner.events); | ||||||
| Ok(Box::new( | ||||||
| std::sync::Arc::try_unwrap(events).unwrap_or_else(|arc| (*arc).clone()).into_iter(), | ||||||
| )) | ||||||
| let events = self.inner.lock().await.events.clone(); | ||||||
| Ok(Box::new(events.into_iter())) | ||||||
| } | ||||||
|
|
||||||
| async fn close(&self) -> Result<(), Self::InternalStorageError> { | ||||||
| let mut inner = self.inner.write().await; | ||||||
| inner.is_closed = true; | ||||||
| self.inner.lock().await.is_closed = true; | ||||||
| Ok(()) | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -960,7 +943,7 @@ mod tests { | |||||
| } | ||||||
|
|
||||||
| assert_eq!( | ||||||
| persister.inner.read().expect("Lock should not be poisoned").is_closed, | ||||||
| persister.inner.lock().expect("Lock should not be poisoned").is_closed, | ||||||
| expected_result.is_closed | ||||||
| ); | ||||||
|
|
||||||
|
|
@@ -991,7 +974,7 @@ mod tests { | |||||
| assert_eq!(event.0, expected_event.0); | ||||||
| } | ||||||
|
|
||||||
| assert_eq!(persister.inner.read().await.is_closed, expected_result.is_closed); | ||||||
| assert_eq!(persister.inner.lock().await.is_closed, expected_result.is_closed); | ||||||
|
|
||||||
| match (&result, &expected_result.error) { | ||||||
| (Ok(actual), None) => { | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cACk. I haven't found any functional issues; I was gonna recommend clearer documentation on intent, but I've seen the previous thread now already touched on this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you feel it's still not clear for follow up? I addressed prior thread to remove the single-owner part. What would you like to see?