-
Notifications
You must be signed in to change notification settings - Fork 125
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
Send the state to the Session backend. #468
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f92481f
Send the state to the Session backend.
StephenWakely 0c7e55a
Added state to drop_session for Session backend.
StephenWakely 769e6a0
Updated tests and formatting for Session Backend.
StephenWakely 15cb4a7
Session backend functions to return Futures
StephenWakely 6811ee9
Merge branch 'master' into session-backend-state
msrd0 04ee0f0
Merge remote-tracking branch 'upstream/master' into session-backend-s…
msrd0 204cc25
fix clippy lint
msrd0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ use std::pin::Pin; | |
use futures::prelude::*; | ||
|
||
use crate::middleware::session::{SessionError, SessionIdentifier}; | ||
use crate::state::State; | ||
|
||
/// A type which is used to spawn new `Backend` values. | ||
pub trait NewBackend: Sync + Clone + RefUnwindSafe { | ||
|
@@ -16,8 +17,11 @@ pub trait NewBackend: Sync + Clone + RefUnwindSafe { | |
fn new_backend(&self) -> anyhow::Result<Self::Instance>; | ||
} | ||
|
||
/// Type alias for the trait objects returned by `Backend`. | ||
pub type SessionFuture = dyn Future<Output = Result<Option<Vec<u8>>, SessionError>> + Send; | ||
/// Type alias for the trait objects that returned by `Backend`. | ||
pub type GetSessionFuture = dyn Future<Output = Result<Option<Vec<u8>>, SessionError>> + Send; | ||
|
||
/// Type alias for the trait objects that set the session in the `Backend`. | ||
pub type SetSessionFuture = dyn Future<Output = Result<(), SessionError>> + Send; | ||
|
||
/// A `Backend` receives session data and stores it, and recalls the session data subsequently. | ||
/// | ||
|
@@ -27,17 +31,26 @@ pub trait Backend: Send { | |
/// Persists a session, either creating a new session or updating an existing session. | ||
fn persist_session( | ||
&self, | ||
state: &State, | ||
identifier: SessionIdentifier, | ||
content: &[u8], | ||
) -> Result<(), SessionError>; | ||
) -> Pin<Box<SetSessionFuture>>; | ||
|
||
/// Retrieves a session from the underlying storage. | ||
/// | ||
/// The returned future will resolve to an `Option<Vec<u8>>` on success, where a value of | ||
/// `None` indicates that the session is not available for use and a new session should be | ||
/// established. | ||
fn read_session(&self, identifier: SessionIdentifier) -> Pin<Box<SessionFuture>>; | ||
fn read_session( | ||
&self, | ||
state: &State, | ||
identifier: SessionIdentifier, | ||
) -> Pin<Box<GetSessionFuture>>; | ||
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. Same as above |
||
|
||
/// Drops a session from the underlying storage. | ||
fn drop_session(&self, identifier: SessionIdentifier) -> Result<(), SessionError>; | ||
fn drop_session( | ||
&self, | ||
state: &State, | ||
identifier: SessionIdentifier, | ||
) -> Pin<Box<SetSessionFuture>>; | ||
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. Same as above |
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Sorry for taking so long to review this - I believe this type signature is incorrect? Without any lifetime information, the
GetSessionFuture
type needs to live for'static
, which it will not do if it accessed the state in an async context. I think this needs to be changed to something likeAlternatively, it might make sense to let
#[async_trait]
deal with the lifetimes.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.
No worries, thanks for reviewing. I'll try to get to these shortly.
The last time I used
#[async_trait]
it resulting in the rust compiler not being able to produce any decent error messages for any errors in the code that implemented those traits. We ended up changing back to returning Futures instead. Do you know if this problem has been resolved? I would be hesitant to use it unless it has..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.
The only time I used
#[async_trait]
was to runcargo expand
on it to see how it worked because I didn't want my proc macro to invoke another proc macro. So I don't know if this has been changed or has ever been a problem. However, I feel like the code would probably be more readable if we don't introduce this many lifetimes, but if you run into any incorrect/unhelpful error messages, just write the code the explicit way. I'm happy with both solutions.