Skip to content

Remove blocking IO #1803

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

Merged
merged 2 commits into from
Apr 11, 2023
Merged

Remove blocking IO #1803

merged 2 commits into from
Apr 11, 2023

Conversation

senekor
Copy link
Contributor

@senekor senekor commented Apr 11, 2023

I had another go at removing the remaining blocking IO in cache.rs. The main problem I had previously encountered was that passing function pointers to async functions is very messy. I used async_trait to not have to do that in the first place. In the process, I switched over to Rocket's managed state. This reduces complexity in cache.rs and theoretically even improves performance a bit, since there are now three separate caches, with separate RwLocks, which should reduce lock contention.

@senekor senekor requested a review from a team as a code owner April 11, 2023 07:55

impl Default for RustReleasePost {
fn default() -> Self {
Self(Default::default(), Instant::now())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is consistent with the previous behavior: If no version (or release post) can be fetched, an empty string is used instead. Not sure how much sense that makes though.

teams: RustTeams::get(teams_cache)
.await
.0
.ok_or_else(|| Box::<dyn Error + Send + Sync>::from("failed to load teams"))?,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The teams data does currently cause an error if it cannot be fetched (unlike rust version and release post) so I used an option in the cache type to indicate there might not be any teams data.

Comment on lines +497 to +499
let rust_version = RustVersion::fetch().await.unwrap_or_default();
let rust_release_post = RustReleasePost::fetch().await.unwrap_or_default();
let teams = RustTeams::fetch().await.unwrap_or_default();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a change in behavior, the caches will now already be prefilled with data at startup. Previously, I think the first request using a cache would actually block until some data could be retrieved.

Copy link
Member

Choose a reason for hiding this comment

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

I think that's a good call, yeah.

src/main.rs Outdated
async fn index() -> Template {
render_index(ENGLISH.into()).await
async fn index(
version_cache: &State<Arc<RwLock<RustVersion>>>,
Copy link
Member

Choose a reason for hiding this comment

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

nit: can we use some kind of typedef for this?

Copy link
Member

Choose a reason for hiding this comment

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

trait Cached + type Cache<T> = ... would probably be fine. Open to other opinions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's cleaner 👍

Comment on lines +497 to +499
let rust_version = RustVersion::fetch().await.unwrap_or_default();
let rust_release_post = RustReleasePost::fetch().await.unwrap_or_default();
let teams = RustTeams::fetch().await.unwrap_or_default();
Copy link
Member

Choose a reason for hiding this comment

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

I think that's a good call, yeah.

@Manishearth Manishearth merged commit 0e2ce97 into rust-lang:master Apr 11, 2023
@senekor senekor deleted the rocket-managed-state branch April 12, 2023 18:41
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.

2 participants