Closed tatsuya6502 closed 2 years ago
Hi! Big thanks for this issue. It means a lot to me that you care that much about your project! This is truly unique. I'm currently working on a refactor in the refactor
branch because the code in main currently kinda sucks (as you've probably seen while writing the fix). I'm implementing a fix on that branch and will eventually merge it into main.
Again, thank you!
Hi. I am the author of Moka cache crate. I found minkan-chat server is using Moka. Thank you for choosing it!
I noticed that minkan-chat server no longer compiles if Moka v0.5.2 or newer is used. Let me explain why and how to solve it.
Why?
When compiling minkan-chat server with a recent version of Moka, the following error will occur:
This is because the signature of
get_or_insert_with
method was changed in v0.5.2.v0.5.1 (doc)
v0.5.2 (doc)
As you can see,
Send
and'static
bounds were added to the generic parameterF
of theinit
future. I had to do this to avoid undefined behaviors in some cases. I wrote a couple of examples here: https://github.com/moka-rs/moka/issues/31This is the relevant code from minkan-chat server:
The compile error occurs because
ctx: &Context<'_>
, which has not-static lifetime, is captured in the async block forget_or_insert_with
. Moka v0.5.2 or newer no longer accept it as the async block will be executed later by different executor threads in the asynchronous runtime, and by the time, the reference (Context<'_>
) may outlive the referent (the actual value).How to solve the error
To solve the error, we need to change the async block not to capture
Context<'_>
, but to have the ownership of adb (Pool<Postgres>)
. Here is a solution. I added+
and-
signs to the beginning of lines to indicate the added/removed ones.The downside of this solution is that
get_token_expiry
will always clone aPool<Postgres>
no matter if the cache has the token expiry or not. I think cloning aPool<T>
will be an inexpensive operation as it will only increment/decrement the reference count ofArc
. So I hope this is negligible.Please let me know if you have some questions.