Skip to content
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

Closed
sagoez opened this issue Nov 18, 2024 · 2 comments
Closed

Explicit guidance over ClientSession lifetime #1247

sagoez opened this issue Nov 18, 2024 · 2 comments
Assignees

Comments

@sagoez
Copy link

sagoez commented Nov 18, 2024

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!

@isabelatkinson
Copy link
Contributor

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.

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.

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.

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?

The driver maintains an internal pool of sessions that get reused, so it's generally not expensive to create a session. Calling the start_session method does not actually communicate with the server, it just retrieves a session from the pool.

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?

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.

@sagoez
Copy link
Author

sagoez commented Nov 21, 2024

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!

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

No branches or pull requests

2 participants