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

Add shared mutable state documentation #1759

Merged
merged 10 commits into from Feb 23, 2023

Conversation

ThanePatrol
Copy link
Contributor

Motivation

I spent a lot of time trying to find documentation for creating a mutable state. I eventually found discussion #629

Solution

Added documentation for creating mutable state and wrote a small trivial example using a Vec<String>

Copy link
Member

@davidpdrsn davidpdrsn left a comment

Choose a reason for hiding this comment

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

I still don't feel this is appropriate to have in axum's docs 🤷

@jplatte
Copy link
Member

jplatte commented Feb 16, 2023

Maybe we should link to tokio's "Which kind of mutex should you use?" docs though?

@davidpdrsn
Copy link
Member

Yeah that'd be fine with me.

@Vagelis-Prokopiou
Copy link
Contributor

@ThanePatrol check the referenced pull request I crested #1777.
I am using a string, and I have added compiling example code.

@ThanePatrol
Copy link
Contributor Author

ThanePatrol commented Feb 22, 2023

@Vagelis-Prokopiou Thanks for letting me know. I've shared the repo with you and incorporated your changes.

The documentation for sharing a mutable state did not provide examples.
Wrote explanation of the steps required
to share mutable states between routes.

Provided an example in code.

This was inspired from discussion tokio-rs#629
tokio-rs#629
The cost of using an async mutex was not mentioned.
Rather than defaulting to async, the documentation
now gives the std Mutex in the example and links
to the discussion from tokio.
Example was opinionated and specific.
Also, changed written description to be more general.
Provides more consistent style across code base
Vagelis had a similar pull request. Changes were merged.
Added a note about deadlocks. A detailed discussion may not be appropriate.
However, a note that makes people aware of the potential consequences could help reduce pitfalls of share mutable state.
@Vagelis-Prokopiou
Copy link
Contributor

This PR looks ready. Kindly waiting on the owners @davidpdrsn @jplatte.

axum/src/extract/state.rs Outdated Show resolved Hide resolved
axum/src/extract/state.rs Outdated Show resolved Hide resolved
Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
@Vagelis-Prokopiou
Copy link
Contributor

@jplatte suggested change applied.

@davidpdrsn
Copy link
Member

I had lots of nitpicky suggestions so I've rewritten it myself. @jplatte wanna give it a final look?

@davidpdrsn davidpdrsn added T-docs Topic: documentation A-axum labels Feb 22, 2023
@davidpdrsn davidpdrsn changed the title Added mutable state documentation. Add shared mutable state documentation Feb 22, 2023
axum/src/extract/state.rs Outdated Show resolved Hide resolved
Comment on lines +317 to +320
/// #[derive(Clone)]
/// struct AppState {
/// data: Arc<Mutex<String>>,
/// }
Copy link
Member

Choose a reason for hiding this comment

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

I'm a little worried that people will do fine-grained locking where more coarse locking is the right solution, based on this example. I'm not sure just showing an Arc<Mutex<_>> with everything inside locked at once is better either though...

Copy link
Contributor

Choose a reason for hiding this comment

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

@jplatte Maybe something like the following can be added, in order to make your point super explicit?

#[derive(Clone)]
struct AppState {
      // This is just an example usage, not a best practice.
      // Choose a setup that matches your specific use case.
      data: Arc<Mutex<String>>,
}

Copy link
Member

Choose a reason for hiding this comment

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

I'm a little worried that people will do fine-grained locking where more coarse locking is the right solution, based on this example.

Yeah I'm a little worried about that as well. But like you say putting a mutex around the whole state probably isn't right either 😅 I dunno. Think I'll just keep it as is.

Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
@davidpdrsn davidpdrsn enabled auto-merge (squash) February 23, 2023 12:36
@davidpdrsn davidpdrsn merged commit 1f22439 into tokio-rs:main Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum T-docs Topic: documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants