Closed encendre closed 4 years ago
Yep, I very much like this functionality. I have a few thoughts about this. It looks to me like tokio models the incoming streams as separate structs. I don't have enough knowledge about why they're doing this, probably because of tcp vs unix data streams, but I bet they have good reasons.
With that in mind, I'd like to propose adding build_on_unix_socket
to the HyperServer
impl for now.
async fn build_on_unix_socket(self, socket_addr: &str);
This could then be used just like start
does with build
internally, that is
let server = HyperServer::new(app);
tokio::runtime::Runtime::new()
.unwrap()
.block_on(server.build_on_unix_socket("/tmp/thruster.sock"))
Thoughts? This seems less invasive than adding it to a trait, and less work than maintaining a third set of ThrusterServer implementations.
I've been thinking about it. I think a better solution would be to implement a method that allows the application to build on any hyper server configuration.
async fn build_on_hyper_builder(mut self, builder: hyper::server::Builder)
and then we just have to use this new method for writing the build
method required by the ThrusterServer
trait, like this
async fn build(mut self, host: &str, port: u16) {
let addr = (host, port).to_socket_addrs().unwrap().next().unwrap();
let builder = Server::bind(&addr);
self.build_on_hyper_builder(builder);
}
This seems it covers more use cases, is still easy to build on unix socket for my use case
let mut listener = tokio::net::UnixListener::bind("/tmp/thruster.sock").unwrap();
let incoming = listener.incoming();
let incoming = hyper::server::accept::from_stream(incoming);
let builder = Server::builder(incoming);
let server = HyperServer::new(app);
tokio::runtime::Runtime::new()
.unwrap()
.block_on(server.build_on_hyper_builder(builder));
Mmm, yes, I like that solution very much. Feel free to open a PR, otherwise I'll hopefully get to this midweek!
On Sun, Apr 5, 2020 at 1:52 PM encendre notifications@github.com wrote:
I've been doing some thinking and I think the best solution would be to implement a method that allows the application to build on any hyper server configuration.
async fn build_on_hyper_builder(mut self, builder: hyper::server::Builder)
and then we just have to use this new method for writing the build method required by the ThrusterServer trait, like this
async fn build(mut self, host: &str, port: u16) { let addr = (host, port).to_socket_addrs().unwrap().next().unwrap(); let builder = Server::bind(&addr); self.build_on_hyper_builder(builder); }
This seems it covers more use cases and is still easy to build on unix socket
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/trezm/Thruster/issues/147#issuecomment-609455950, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJWOLKFZ5WIZJZUVKHUBSTRLDANTANCNFSM4L66HAOA .
I tried to implement it but since I'm pretty new to rust I didn't succeed. I have successfully compiled the crate adding the new method with and a few tricks that seem unnatural to me... But the example I wrote doesn't work.
I can try this out today, not to worry! I'll post the PR here when it's done so you can compare with what you were doing and hopefully see where you were going astray :)
So after some noodling, I realized that hyper doesn't actually natively support unix sockets! I had to use hyperlocal in order to get all of the traits on the correct Unix streams.
With that in mind, I thought it easiest to add hyperlocal
as an optional dependency and hide the whole thing behind a feature flag. This means that I actually went with the original solution you proposed @encendre, of implementing another server and ignoring the port. Not the most elegant solution, but will definitely work for now!
Hello, is there any way to use thruster with unix domain sockets? If they is no way to do that now, I wonder if is possible to add a method
.build_from_incoming
which usehyper::Server::builder
insteadhyper::Server::bind
for create the underlyinghyper::Server
or maybe just a new type of server
thruster::UsdHyperServer
which impl theThrusterServer
trait but ignoring theport
argument, like this: