-
Notifications
You must be signed in to change notification settings - Fork 229
Description
Currently I am trying to refactor the idea of a Stream in this library.
A stream should simply be something that can be written to and read from.
The hyper has had some issues with this idea as well, and recently refactored their NetworkStream which implemented (in my opinion too many) traits: Read + Write + Any + Send + Typeable, into a Transport trait which simply implements Read + Write (and Evented for mio support).
The Problem
We are supporting a (maybe strange, maybe normal) use case of being able to split the client into two owned copies (sender and receiver) so they may be used in different threads.
This creates a need for streams to support the ability to clone themselves into two owned copies.
This is OK with TcpStream, but in SslStream the try_clone(&self) function is deprecated in newer versions and will be removed.
Hyper integration is also blocked since Hyper's NetworkStream and now Transport do not implement try_clone(&self), presumably because of the above reason.
Solution: Remove Support, Switch to Arc<Mutex<Client<...>>>
The solution as I see it is to no longer support multi-threading as a first class use-case.
If someone needs to use a client on multiple threads they can wrap it in a lock.
I am under the impression that this is how it currently works when one clones a TcpStream and tries to read and write to it: the kernel has a lock on this file descriptor and blocks calls accordingly. Please correct me if I am wrong. This change would just make the locking more explicit.
This would also allow the Client to not own its stream (which is wanted in the use case of hyper integration).
Multi-threading for servers is still practical though, since each Client the server creates can be dispatched to worker threads safely.
For Those Still Holding Out
There will be a method on the Client called .stream(&self) -> &S which would return the concrete type of the stream the Client is using, so it can be cloned by people who want to.
Implications
Clientcan no longer besplit.SenderandReceiverwill no longer be able to own/borrow their stream.