Getting rid of a giant mutex in Axum
I have been stuck for the past weeks on a problem concerning the web server Axum in Rust.
I have a scientific experiment running that regularly performs measurements. For each measurement, I need access to the application state. In Axum, shared application state between different handlers, is inside an Arc<Mutex<Shared>>. The problem with this is that every web handler has to wait until the experiment loop is done and unlocks the shared state. This means web handlers have a long response time, since the web handlers of Axum are waiting for the measurement loop to finish and unlock the shared mutex.
It is as if the measurements are competing with the web handlers. There are two parts making use of the shared mutex:
- web handlers
- experiment measurements
I do not know how to solve this problem. I have tried splitting the shared mutex for Axum up in smaller mutexes, but then the code for the experiment becomes much more complicated and everywhere asynchronous mutex locks are introduced. Asynchronous mutexes make every call-back in the experiment asynchronous.
What would be a good simple solution?