Hello!
I'm working on a program where I have the following running:
- User requests a container to be provisioned.
- Host machine ensures that that container doesn't already exist.
- Host machine starts the container.
- Host machine adds the container to its list of containers.
Of course, if a user decides to spam requests, I could end up with a race condition where multiple containers are created. I want to fix that by putting a lock over the entire initialization method, but I want it to be able to run concurrently with other initialization requests, so long as they aren't the exact same one. So, I need some sort of ID lock where I can lock an ID for the duration of the function, then unlock it at the end. Here's what I've come up with: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b46b9e2883bc5f08709c519b021afc7e.
This is the sort of territory where I'm liable to create deadlocks (or locks that simply don't do anything), so I'm wondering if somebody could:
A. Take a look over it and tell me what I'm doing wrong.
B. Point me towards a library that implements this exact thing which has somehow eluded me.
Thank you!
A browser interface to the Rust compiler to experiment with the language