[ 
https://issues.apache.org/jira/browse/THRIFT-5283?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17501749#comment-17501749
 ] 

Tobias Mucke commented on THRIFT-5283:
--------------------------------------

Hi, I've looked into adding the capability to listen to Unix Domain Sockets 
(UDS) with the following principles in mind:
 # Avoid introducing a breaking change.
 # Keep the power of ToSocketAddrs trait.

I would like to share my findings so you can follow my thinking and also my 
conclusion. Please correct me if I'm wrong, thank you.

The fn we are talking about is in server::threaded.
{code:java}
pub fn listen<A: ToSocketAddrs>(&mut self, listen_address: A) -> 
crate::Result<()> {code}
When adding UDS support to this fn we can not stick to ToSocketAddrs trait as 
its only capable to create std::net::SocketAddr which does not cover UDS. So, 
we need a new trait able to cover all cases from ToSocketAddrs (fulfilling 
principle 1) + UDS. I tried to achieve this with a new enum TSocket and a trait 
ToTSocket. So the new fn signature looks like:
{code:java}
pub fn listen<A: ToTSocket>(&mut self, listen_address: A) -> crate::Result<()> 
{code}
 

The enum TSocket and trait ToTSocket:
{code:java}
pub enum TSocket {
    Ip(SocketAddr),
    #[cfg(unix)]
    Uds(UnixListener),
}

pub trait ToTSocket {
    fn to_socket(&self) -> TSocket;
}{code}
However with this simple approach I'm unable to fulfill principle 2 because 
ToSocketAddress defines different Iterator types depending on the Type the 
trait is implemented for. Thus, I did experiment with dyn + Box to bring the 
TSocket::Ip variant to eye level of ToSocketAddress trait.

This is one example I experimented with:
{code:java}
Ip(Box<dyn ToSocketAddrs<Iter = dyn IntoIter>>) {code}
However, I didn't manage to implement this.

To conclude, I suggest to implement a new fn in server::threaded supporting 
UDS. This approach will also meet the two principles. I'm confident that this 
approach will work and keeps the code base clean. I've found this very same 
approach in actix_web::HttpServer which e.g. offers a fn listen and fn 
listen_uds.

For your reference: 
[https://docs.rs/actix-web/latest/actix_web/struct.HttpServer.html#method.listen]

Any other thoughts on this?

Kind regards,

Tobias

> Ability to listen over unix socket in thrift Rust crate
> -------------------------------------------------------
>
>                 Key: THRIFT-5283
>                 URL: https://issues.apache.org/jira/browse/THRIFT-5283
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Rust - Library
>    Affects Versions: 0.13.0
>            Reporter: Prateek Kumar Nischal
>            Priority: Major
>
> The rust crate for [thrift|https://crates.io/crates/thrift] right now has the 
> ability to create a server [but only over a TCP 
> socket|https://github.com/apache/thrift/blob/master/lib/rs/src/server/threaded.rs#L172].
> {code}
> pub fn listen<A: ToSocketAddrs>(&mut self, listen_address: A) -> 
> thrift::Result<()> 
> {code}
> The API requires the trait 
> [ToSocketAddrs|https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html] to 
> be implemented which is not possible for a Unix Domain Socket. 
> Other libraries, for example, python has an option to serve over unix 
> sockets. eg
> {code:python}
> TSocket.TServerSocket(unix_socket='/tmp/service.sock')
> {code}
> It would be really nice to be able to get a similar API in rust as well 
> unless there is a way to do it already. Please let me know if it already 
> exists.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to