-
Notifications
You must be signed in to change notification settings - Fork 164
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
Explicit guidance over ClientSession
lifetime
#1247
Comments
Hey @sagoez, thanks for opening this issue! The driver specification for sessions may be of interest to you for more information on sessions, but I'll outline some details that should answer your questions.
I'd recommend creating a new session per usage (usage meaning a transaction, causally consistent session, etc.). Sessions on the server expire after a certain amount of time, so getting a fresh session per use will reduce the likelihood of a session timing out, especially for a long-running application. Additionally, passing a session to an operation requires a mutable reference to the session, so it may become difficult to share the session across your application.
The driver maintains an internal pool of sessions that get reused, so it's generally not expensive to create a session. Calling the
This will depend upon your use case. For example, are the operations in the loop all meant to be part of a single transaction? If so, they'll all need to share the same session. However, if you're not using the session for any particular behavior, it may be preferable to rely on the driver to create an implicit session. This way you don't need to manage the session yourself. |
Thank you so much for the detailed reply! It answers all my questions about session management, gives me a clearer understanding of how to approach it. Appreciate it! |
I have a question regarding the usage of the ClientSession API. While reviewing the documentation, I noticed that it's not explicitly stated whether it’s intended to be a long-lived or short-lived session. I’m wondering if it’s recommended to recreate the session per usage, or if it’s better to wrap it in an Arc and share it across the application.
Additionally, I’m curious about the cost of creating a ClientSession—is it inexpensive to create (similar to Tokio tasks), or is it something that should be done more cautiously?
For example, if I were using a ClientSession inside a loop, what would be the ideal approach? Should the session be created once and reused, or would it be better to recreate it on each iteration?
I took a look at the source code and it seems like creating the session might be relatively cheap, but I would appreciate some official guidance on this for more confidence in how to handle it.
Thanks so much for any insights you can provide!
The text was updated successfully, but these errors were encountered: