-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for listening on Unix socket #594
Conversation
I am using the $ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
Running `/Users/messense/Projects/Rocket/target/debug/hello_world`
🔧 Configured for development.
=> address: unix:///tmp/hello.sock
=> port: 8000
=> log: normal
=> workers: 1
=> secret key: generated
=> limits: forms = 32KiB, json* = 1MiB, msgpack* = 1MiB
=> keep-alive: 5s
=> tls: disabled
=> [extra] hi: "Hello!"
=> [extra] is_extra: true
🛰 Mounting '/':
=> GET / (hello)
🚀 Rocket has launched from http://unix:///tmp/hello.sock:0 $ curl --unix-socket /tmp/hello.sock http://localhost// -v
* Trying /tmp/hello.sock...
* Connected to localhost (/tmp/hello.sock) port 80 (#0)
> GET // HTTP/1.1
> Host: localhost
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain; charset=utf-8
< Server: Rocket
< Content-Length: 13
< Date: Wed, 28 Mar 2018 05:40:28 GMT
<
* Connection #0 to host localhost left intact
Hello, world! |
lib/src/data/net_stream.rs
Outdated
@@ -16,6 +17,8 @@ pub enum NetStream { | |||
Http(HttpStream), | |||
#[cfg(feature = "tls")] | |||
Https(HttpsStream), | |||
#[cfg(unix)] | |||
UnixHttp(UnixSocketStream), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this, but it is needed to make concrete_stream work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's all very frustrating.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this deal with sockets that don't already exist? What about those that do? I'd like for configuration to fail if a socket already exists and for Rocket to create the socket if it doesn't.
lib/src/data/net_stream.rs
Outdated
@@ -16,6 +17,8 @@ pub enum NetStream { | |||
Http(HttpStream), | |||
#[cfg(feature = "tls")] | |||
Https(HttpsStream), | |||
#[cfg(unix)] | |||
UnixHttp(UnixSocketStream), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it's all very frustrating.
Awesome, @messense! This is really great. Excited to get this in. :) |
It will create a new one. if the socket file exists, it crashes with Running `/Users/messense/Projects/Rocket/target/debug/hello_world`
🔧 Configured for development.
=> address: unix:///tmp/hello.sock
=> port: 8000
=> log: normal
=> workers: 1
=> secret key: generated
=> limits: forms = 32KiB, json* = 1MiB, msgpack* = 1MiB
=> keep-alive: 5s
=> tls: disabled
=> [extra] hi: "Hello!"
=> [extra] is_extra: true
🛰 Mounting '/':
=> GET / (hello)
Error: Rocket failed to launch due to an I/O error.
thread 'main' panicked at 'Address already in use (os error 48)', lib/src/error.rs:207:17
note: Run with `RUST_BACKTRACE=1` for a backtrace. I think it's the exact behavior of std::unix::net::UnixListener::bind method. |
Thanks for the detailed review, I hope to address them soon. |
cf53a31
to
4acd52a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like more code that depends on whether we're on Unix or not to be contained in the unix
module. The cfg
s sprinkled around make it harder to follow what the intended flow is.
e62be17
to
466350e
Compare
Unix domain socket support for Windows is added in messense@47c9dea Tested on Windows 10 version 1903 |
What happens if you try to use UDS on a unsupported version of Windows? Does Rocket still compile on unsupported Windows versions? Is it simply a runtime error? Which error message do we print if so? |
Windows Unix domain socket is created with
|
Side note: |
I've created a Windows Server 2016 on Azure which does not support UDS, it failed to launch with error message:
What do you think? @SergioBenitez @jebrosen |
That looks reasonable. We could detect that error and print a custom message but it might not be worth the effort. We can note in the documentation that this only works on Windows 1809 or later. |
Added the detection code here: 3c9e579 |
Do you plan to merge this? |
I need to review this again, but I'm generally in favor of this. If it's backwards-compatible we could do it in 0.4, but I suspect the changes to For 0.5 the socket and connection code will need to be rewritten, but it can probably be done somewhat more simply. Furthermore, I would like to make the connection handling extensible such that one could implement unix socket handling, FastCGI, and other I/O all without modifying rocket. |
74113c3
to
95c981d
Compare
Hello, what's the status on this? I'd love to use Rocket over unix socket. Thank you |
In Ruby ecosystem this is done by separating common HTTP server interface (Rack), HTTP server implementations (Puma, Unicorn, etc) and HTTP applications (which may use Ruby on Rails, Sinatra, etc, or no frameworks at all). In other words, every HTTP application is a Rack application, every HTTP server is a Rack server, every HTTP middleware is a Rack middleware. Everything is built around Rack, which is a very thin and common interface. |
@jebrosen , @SergioBenitez : what is the current status and how would you prefer to have unix-socket support implemented ? |
#594 (comment) is still accurate. Unix socket support requires roughly these steps:
At this point I don't believe there are any more significant changes planned for those areas, and I'd be happy to work on and/or review this soon. @SergioBenitez, any preferences for timing this breaking change to configuration or handling it in an non-breaking way? As far as I can see, the other two changes could be done separately and even in a minor release. |
I know open-source people hate this, but can I offer $50 (I do not have a pound sterling symbol on my keyboard) for this? |
This is now wholly outdated. On the one hand, prioritizing this earlier would have prevented staleness. On the other, there are still unresolved questions, and the implementation now would look nothing like the implementation then. Nevertheless, I've implemented support for a Unix domain socket listener in a local domain branch. The holdback is configuration: how do we configure which listener to use? There's #1070, which if implemented, would let us say "well, do so programatically." But this isn't particularly satisfying. Starting a Rocket application that listens on a UDS should be as simple as: ROCKET_ADDRESS="unix:/path/to/socket" cargo run This is nice and tenable, but the problem is that it ignores the existence of the
I'm inclined to suggest we follow 1. In any case, I'm closing out this particular PR as the implementation here has no path forward given the divergence upstream. |
TODO:
address
andport
are specified(But what if I want to listen on both tcp and unix sockets?)Closes #545