Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

pepoviola
Copy link
Contributor

Hi @No9 / @yoshuawuyts, I was looking the pr #9 and looks really nice!! and triggered me the idea to check if we can skip the config step and keep the expiry feature.

I checked how this is handler in node.js and the connect-mongodb-session create an index with {expireAfterSeconds:0} and let the session to manage the expire as an absolute value.
https://github.com/mongodb-js/connect-mongodb-session/blob/master/index.js#L88-L94

I make some changes to mimic this behavior, but I don't know if creating the index in the connect function is the best way or we can add something like fn initialize to be called one time to create this index.
Also, mongodb runs the background process to delete expired items every 60 seconds, so any value of expire lower than that will not be respected.

I could be wrong on this idea and any feedback is welcome 😄

Thanks!

@No9
Copy link
Collaborator

No9 commented Oct 12, 2020

Hey @pepoviola
Thanks for getting involved - I like the shape of this PR as it has the foundation of catering for both the created and expire approaches.
89981b2#diff-b4aea3e418ccdb71239b96952d9cddb6R96

I think we should keep both options as the created approach allows for global control of the cache time and out of band management where as the expire approach in this PR is more aligned to prior art and allows for explicit management of the session length from code. I think both are valid use cases.

I'll put the rest of the comments inline.

db.coll_session.createIndex( { created": 1 } , { expireAfterSeconds: 300 } )
```
To enable this [expiry feature](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time) at `index` for `expireAt` is created when the client `connet`. Allowing then to use this property from the session to set the expiration of the document.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep both approaches in the README as well as state when you would use one over the other.

src/lib.rs Outdated
/// ```
pub async fn connect(uri: &str, db: &str, coll_name: &str) -> mongodb::error::Result<Self> {
let client = Client::with_uri_str(uri).await?;
let create_index = doc! {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be an external initialize as suggested in the PR commit message. It would fit with the builder approach in the tide example https://github.com/jbr/tide-example/blob/main/src/main.rs#L24 and allow us to support both global and local options

}

#[test]
fn test_check_expired() -> async_session::Result {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks for including the tests upfront :)

@pepoviola
Copy link
Contributor Author

Hi @No9, thanks for the feedback!! 😄 . I will review the comments and make those changes.

I think we should keep both options as the created approach allows for global control of the cache time and out of band management where as the expire approach in this PR is more aligned to prior art and allows for explicit management of the session length from code. I think both are valid use cases.

Yes, having support of both approaches is really nice.

Thx!

@pepoviola
Copy link
Contributor Author

Hi @No9, I just review the feedback and add this two ( more like initializers ) fn create_created_index_for_global_expiry / create_expire_at_index to create the indexes from the code. I also include the instructions for creating those through the mongodb client.

Thx!

@No9
Copy link
Collaborator

No9 commented Oct 12, 2020

Hey @pepoviola - Cool - This is great progress.

I think it's going to need something less wordy than create_created_index_for_global_expiry.
It's logically correct but the repetition of create_created doesn't flow.
Maybe use index as a verb and go for index_on_expiry_at and index_on_created but this might be too subtle for none native speakers? What are your thoughts?

I see you removed the reference to 12 Factor apps. I understand that it may not be directly relevant to the configuration section but we should have some overview of when you would use one option over another that captures the conclusions from this branch and the #8 issue.
Probably an Overview heading before the installation of configuration topics?
I am happy to add that on top of this PR if you would prefer.

I'll have a deeper look at the code later today but would good to get @jbr 's thoughts on this as well.

@pepoviola
Copy link
Contributor Author

pepoviola commented Oct 12, 2020

Hi @No9, thanks for the feedback 😄 . I just made those changes and add the overview section.
Thanks!

This library utilises the document [expiry feature](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds) in mongodb to expire the session.
## Overview
This library allow you to utilises the document expiration feature based on a [specified number of seconds](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds) or in a [specific clock time](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time) supported by mongodb to expire the session.

Copy link
Collaborator

@No9 No9 Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking something along the lines of
"The specified number of seconds approach is designed to enable the session time out to be managed at the mongodb layer. This approach provides a globally consistent session timeout across multiple processes but has the downside that all services using the same session collection must use the same timeout value.

The specific clock time clock time approach is where you require more flexibility on your session timeouts such as a different session timeout per running service or you would prefer to manage the session time out at the process level. This is more flexible but might lead to some perceived inconsistency in session timeout depending on your upgrade/rollout strategy."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @No9, Neat!! If you want I can add those lines. 👍

Copy link
Collaborator

@No9 No9 Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pepoviola If you're OK with it then lets add it inbetween the content that already there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @No9, added 👍 . Thanks for the help!

README.md Outdated
Comment on lines 49 to 50
## Overview
This library allow you to utilises the document expiration feature based on a [specified number of seconds](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds) or in a [specific clock time](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time) supported by mongodb to expire the session.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Overview
This library allow you to utilises the document expiration feature based on a [specified number of seconds](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds) or in a [specific clock time](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time) supported by mongodb to expire the session.
## Overview
This library uses the document expiration feature based on a [specified number of seconds](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds) or a [specific clock time](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-at-a-specific-clock-time) supported by mongodb to expire the session.

The specified number of seconds approach is designed to enable the session time out to be managed at the mongodb layer. This approach provides a globally consistent session timeout across multiple processes but has the downside that all services using the same session collection must use the same timeout value.

This library utilises the document [expiry feature](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds) in mongodb to expire the session.
The specific clock time clock time approach is where you require more flexibility on your session timeouts such as a different session timeout per running service or you would prefer to manage the session time out at the process level. This is more flexible but might lead to some perceived inconsistency in session timeout depending on your upgrade/rollout strategy.
Copy link
Member

@jbr jbr Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm curious about the word "perceived" — is it actually consistent?

db.coll_session.createIndex( { "created": 1 } , { expireAfterSeconds: 300 } );
```

Other way to set create the index is using `index_on_created` passing the amount of seconds to expiry after the session.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Other way to set create the index is using `index_on_created` passing the amount of seconds to expiry after the session.
Another way to set create the index is to use `index_on_created`, passing the amount of seconds to expiry after the session.

src/lib.rs Outdated
}
}

/// Create a new index for the `created` property and set the expiry ttl (in secods).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Create a new index for the `created` property and set the expiry ttl (in secods).
/// Create a new index for the `created` property and set the expiry ttl (in seconds).

Ok(())
}

/// Create a new index for the `expireAt` property, allowing to expire sessions at a specific clock time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Create a new index for the `expireAt` property, allowing to expire sessions at a specific clock time.
/// Create a new index for the `expireAt` property, in order to expire sessions at a specific clock time.

The specified number of seconds approach is designed to enable the session time out to be managed at the mongodb layer. This approach provides a globally consistent session timeout across multiple processes but has the downside that all services using the same session collection must use the same timeout value.

This library utilises the document [expiry feature](https://docs.mongodb.com/manual/tutorial/expire-data/#expire-documents-after-a-specified-number-of-seconds) in mongodb to expire the session.
The specific clock time clock time approach is where you require more flexibility on your session timeouts such as a different session timeout per running service or you would prefer to manage the session time out at the process level. This is more flexible but might lead to some perceived inconsistency in session timeout depending on your upgrade/rollout strategy.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The specific clock time clock time approach is where you require more flexibility on your session timeouts such as a different session timeout per running service or you would prefer to manage the session time out at the process level. This is more flexible but might lead to some perceived inconsistency in session timeout depending on your upgrade/rollout strategy.
The specific clock time approach is for use cases when you require more flexibility on your session timeouts such as a different session timeout per running service or you would prefer to manage the session time out at the process level. This is more flexible but might lead to some perceived inconsistency in session timeout depending on your upgrade/rollout strategy.

@pepoviola
Copy link
Contributor Author

Hi @No9 / @jbr, Thanks for the feedback! I updated the code according to but I have some questions related

  1. I check the integration with tide and the sessionMiddleware have a default session expiry of 1 day, so when this library is used with tide the session will always have some expiry. So, the default session expire mechanism of the library will be only use where the library is used as stand alone. Also, related to this topic I wonder if it's more ergonomic to call initialize from new by default.

  2. I couldn't use the ? in the example integration with tide because the From trait isn't implemented. Is this something we want to add?

Thanks a lot for the help!

@pepoviola pepoviola requested a review from No9 October 27, 2020 11:19
@pepoviola
Copy link
Contributor Author

Hi @jbr, can you review when you have time. Thanks!

@jbr
Copy link
Member

jbr commented Nov 9, 2020

@pepoviola thanks for mentioning me here but I think this is ultimately a @yoshuawuyts / @io9 PR to review. I don't know anything about mongo, and my only feedback was "we should provide sensible defaults so it's easy to get started," which it sounds like we're doing

@pepoviola
Copy link
Contributor Author

Hi @jbr, thanks! I will wait for @yoshuawuyts or @No9.

@No9 No9 merged commit 48bc322 into http-rs:session-as-object Dec 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants