Totodore / socketioxide

A socket.io server implementation in Rust that integrates with the Tower ecosystem and the Tokio stack.
https://docs.rs/socketioxide
MIT License
1.21k stars 52 forks source link

Implement Clone for SocketRef #260

Closed AkiraMiyakoda closed 6 months ago

AkiraMiyakoda commented 8 months ago

Motivation

I sometimes find it useful to pass SocketRef across threads. However, I need to use Arc<SocketRef> to do that despite that SocketRef itself is essentially an Arc.

SocketIo which also wraps Arc already has clone method. So I believe that we can have SocketRef::clone too.

Solution

This PR implements the Clone trait for SocketRef like SocketIo.

Totodore commented 8 months ago

The primary purpose of SocketRef before being an extractor is to remove the clone method from the inner Arc<Socket> the main reason is to protect the user from memory leaks. If the user for instance builds a HashMap<String, Arc<SocketRef>> and doesn't remove the socket from the hashmap when it is closed, the socket is never freed.

However it is possible to keep this behaviour as you said by creating an Arc<SocketRef>. I'm going to do some research on this to be sure that never freeing the socket doesn't create a bad state in the socketio model.