-
Notifications
You must be signed in to change notification settings - Fork 282
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
Question about ConcurrencyLimit
Service and Service
trait in general
#689
Comments
Can you elaborate on this? Why might it cause race conditions? |
Here are the relevant functions.
Suppose we have
suppose the scheduling decides to let both poll_ready() calls finish, so two tasks both are allowed to invoke |
This is not possible in safe Rust. In order to call Instead, the common pattern is for each spawned task to move a clone of the service by value. Typically, if only a single instance of an inner Note that the tower/tower/src/limit/concurrency/service.rs Lines 96 to 107 in b12f148
The shared state of such middlewares (in this case, the Incidentally, I think this also answers your other question about why the I hope this is a helpful answer, and I'm happy to expand on anything I said if it doesn't make sense yet! Footnotes
|
That was an insightful answer with great details. Thank you @hawkw! Indeed the custom I also started a discussion in discord and I'm quoting a comment from @seanmonstar
Could you also please share some insight about the design tradeoffs? |
@hawkw / @davidpdrsn . Is further action required here? E.g. more answers, doc changes, etc? |
As I understood the
Service
trait's responsibility is to produce aFuture
that encapsulates all the computations needed to serve a request. And it is common to expect aService
being reused by many requests concurrently.I noticed that
ConcurrencyLimit<T>
allows mutations to itself in bothpoll_ready()
andcall()
to update thepermit
which means concurrent accesses would cause race conditions. Why is this okay?And in general why does
Service
trait definepoll_ready()
andcall()
to work with&mut self
instead of&self
?The text was updated successfully, but these errors were encountered: