Skip to content
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 unix socket #158

Merged
merged 22 commits into from
Feb 2, 2022
Merged

Add unix socket #158

merged 22 commits into from
Feb 2, 2022

Conversation

cecton
Copy link
Contributor

@cecton cecton commented Nov 17, 2021

Linking this issue here as these traits could be moved to tokio-util: tokio-rs/tokio#4244 tokio-rs/tokio#4385

It's working!

[7] [16:11:40] ~ > curl -v --unix-socket /tmp/router.sock http://localhost:4000/graphql -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'content-type: application/json' -H 'Origin: https://studio.apollographql.com' -H 'DNT: 1' -H 'Connection: keep-alive' -H 'Sec-Fetch-Dest: empty' -H 'Sec-Fetch-Mode: cors' -H 'Sec-Fetch-Site: cross-site' -H 'Sec-GPC: 1' --data-raw '{"query":"{topProducts{upc name __typename}}"}'
*   Trying /tmp/router.sock:0...
* Connected to localhost (/tmp/router.sock) port 4000 (#0)
> POST /graphql HTTP/1.1
> Host: localhost:4000
> Accept-Encoding: deflate, gzip, br, zstd
> User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0
> Accept: */*
> Accept-Language: en-US,en;q=0.5
> content-type: application/json
> Origin: https://studio.apollographql.com
> DNT: 1
> Connection: keep-alive
> Sec-Fetch-Dest: empty
> Sec-Fetch-Mode: cors
> Sec-Fetch-Site: cross-site
> Sec-GPC: 1
> Content-Length: 46
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< access-control-expose-headers: content-type
< access-control-allow-origin: https://studio.apollographql.com
< transfer-encoding: chunked
< date: Thu, 18 Nov 2021 16:11:47 GMT
< 
* Connection #0 to host localhost left intact
{"data":{"topProducts":[{"upc":"1","name":"Table","__typename":"Product"},{"upc":"2","name":"Couch","__typename":"Product"},{"upc":"3","name":"Chair","__typename":"Product"}]}}⏎

Fixes #154

@cecton cecton self-assigned this Nov 18, 2021
@cecton cecton marked this pull request as ready for review November 18, 2021 16:21
@o0Ignition0o
Copy link
Contributor

o0Ignition0o commented Nov 18, 2021

This looks super cool! is there any issue / context this pr refers to ?

EDIT: my bad, i didn't notice #154 below

@cecton
Copy link
Contributor Author

cecton commented Nov 18, 2021

This looks super cool! is there any issue / context this pr refers to ?

EDIT: my bad, i didn't notice #154 below

I added it after I requested your review lol

@cecton
Copy link
Contributor Author

cecton commented Nov 18, 2021

This looks super cool!

Actually it's terrible xD There should be a way to get a listener that is either a unix socket or a tcp listener. I made a PR to add that to tokio-util 🤞

Copy link
Contributor

@o0Ignition0o o0Ignition0o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow this is great work! 🎉 the pull request reads incredibly well!

+1 for me, with only one question / comment.

Pre-approving this, although we might want to wait a bit and see if the tokio team wants to accept the listener trait.

crates/apollo-router/src/configuration/mod.rs Outdated Show resolved Hide resolved
@cecton
Copy link
Contributor Author

cecton commented Nov 19, 2021

Pre-approving this, although we might want to wait a bit and see if the tokio team wants to accept the listener trait.

Yes I would too but I expect this to take some time. The current implementation is quite easily replaceable so I wouldn't mind merging this for now.

Geal
Geal previously requested changes Nov 19, 2021
pub(crate) type BoxAsyncReadWrite = Box<dyn AsyncReadWrite + Send + Unpin + 'static>;

impl AsyncReadWrite for TcpStream {
fn set_nodelay(&self, nodelay: bool) -> io::Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be done right at socket creation, there's no need to add an empty method for UnixStream

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated, I do not touch that part anymore

fn set_nodelay(&self, nodelay: bool) -> io::Result<()>;
}

pub(crate) type BoxAsyncReadWrite = Box<dyn AsyncReadWrite + Send + Unpin + 'static>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not a good idea to have a Box<dyn> to hold either a TcpStream or UnixStream. It is useful when we're handling both kinds of objects at runtime, but here we're always creating one kind of object (we won't run in TCP and Unix socket modes at the same time), so we should not bear the cost of adding an allocation in the hot path for that.
AsyncRead has one method, AsyncWrite has 5 methods, so it's still reasonable to make an enum here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed with new impl

@cecton cecton marked this pull request as draft December 1, 2021 13:29
@cecton cecton marked this pull request as ready for review January 28, 2022 15:40
@cecton
Copy link
Contributor Author

cecton commented Jan 28, 2022

This PR is ready for review again

apollo-router/src/http_server_factory.rs Outdated Show resolved Hide resolved
@cecton cecton dismissed Geal’s stale review January 31, 2022 13:05

code changed completely

Co-authored-by: Coenen Benjamin <[email protected]>
@cecton cecton merged commit 948dbc6 into main Feb 2, 2022
@cecton cecton deleted the cecton-unix-socket branch February 2, 2022 07:00
tinnou pushed a commit to Netflix-Skunkworks/router that referenced this pull request Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: Listen to unix socket path
6 participants