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

Update the credentials cache to use an async mutex #3105

Closed
wants to merge 1 commit into from
Closed

Conversation

zanieb
Copy link
Member

@zanieb zanieb commented Apr 17, 2024

It seems wrong to use a sync mutex in an async context without strong justification. We're probably unnecessarily blocking the event loop.

@zanieb zanieb added performance Potential performance improvement internal A refactor or improvement that is not user-facing labels Apr 17, 2024
@charliermarsh
Copy link
Member

I think this actually isn't recommended unless you need to hold the mutex across await points: https://docs.rs/tokio/latest/tokio/sync/struct.Mutex.html#which-kind-of-mutex-should-you-use

@charliermarsh
Copy link
Member

(I was surprised to learn this.)

@zanieb
Copy link
Member Author

zanieb commented Apr 17, 2024

Oh interesting... that... kind of makes sense but idk? It's hard to grok when you have a bunch of async requests going through the middleware at the same time. It's fine for one of the requests to block the entire async thread waiting for a mutex? Weird.

@zanieb zanieb added the do-not-merge Pull request is not ready to merge label Apr 17, 2024
@zanieb
Copy link
Member Author

zanieb commented Apr 18, 2024

@BurntSushi do you have more knowledge here?

@BurntSushi
Copy link
Member

Not particularly. But the advice from tokio's docs makes sense. Maybe another way to conceptualize this is that I believe it's always correct to use an async mutex, but it's not always correct to use a std mutex. Maybe think of a std mutex as an optimization. You just really want to avoid holding it over an await boundary. And indeed, it could wind up blocking the whole thread if it's highly contended. But at that point, I'd classify it as a perf bug, async or not.

I also think the last bit of the tokio docs is crucially important:

Additionally, when you do want shared access to an IO resource, it is often better to spawn a task to manage the IO resource, and to use message passing to communicate with that task.

In other words, I'd treat the use of an async mutex as somewhat niche. I'd try to take a more CSP (Communicating Sequential Processes) approach to the problem.

@BurntSushi
Copy link
Member

For this PR, it looks like the use of a std mutex is okay? The tip-off is that it looks like your routines aren't doing any I/O and weren't previously async (because you needed to add async fn in order to use an async mutex).

@zanieb zanieb closed this Apr 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge Pull request is not ready to merge internal A refactor or improvement that is not user-facing performance Potential performance improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants