Closed X-OrBit closed 1 month ago
It is because the Ref
is not Send
. Therefore if you use it across .await
points, the returned future will not be Send
. It is a requirement for the returned future to be Send
so it can be later boxed.
One possible way to solve this might be to change the Dashmap
that is backing the Extensions
by a tokio::RwLock<HashMap>
so that LockGuard
are Send
.
Currently the only solution to solve your issue is either to constraint your ref to a limited scope before your .await
point:
async fn middleware(socket: SocketRef) -> Result<(), socketioxide::SocketError<()>> {
{
let ext: &Extensions = &socket.extensions;
let number: Option<Ref<i32>> = ext.get::<i32>();
}
let _ = test_async().await;
Ok(())
}
or to clone the content and discard the Ref
.
Thanks for the answer, I just tried to use cloning for now, as the objects where not too complicated. But the solution with the limitation of the scope is good one, did not figure out it myself
Are there any plans to add rwlock/mutex to extensions?
Actually, it is global to every handlers (not only middlewares).
The only thing I could do is to provide a Extension
extractor that directly get and clone the object like with the Extension
extractor from axum. I might also add an HttpExtension
extractor that would correspond to the http request extension.
Describe the bug Cannot use socket extensions inside async namespace middleware
To Reproduce Code that reproduce error:
main.rs
Cargo.toml
Error:
Expected behavior The code is being compiled, extensions are working properly
Versions (please complete the following information):
Additional context -