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

Creating a gRPC service listener on an existing TCP socket #2317

Closed
ItalyPaleAle opened this issue Jan 9, 2023 · 6 comments · Fixed by #2675
Closed

Creating a gRPC service listener on an existing TCP socket #2317

ItalyPaleAle opened this issue Jan 9, 2023 · 6 comments · Fixed by #2675

Comments

@ItalyPaleAle
Copy link

Is your feature request related to a problem? Please describe.

The problem we have is that app A needs to invoke app B via gRPC, but app B is behind a firewall and cannot open ports. In order to do that, we have configured app A to create an ephemeral TCP listener: app B makes an outbound TCP connection to app A's listener, and then the connection is reused so app B starts a gRPC server on that connection.

We (Dapr project) need to support this scenario with app B being written in a variety of languages. App A is written in Go.

Describe the solution you'd like

We have implemented this in Go using go-grpc. We are able to create a net.Listener that returns an existing net.Conn, and the gRPC server can "listen" on that.

I am not able to find a way to create a gRPC listener on top of an existing TCP socket in the grpc-node SDK.

Describe alternatives you've considered

I have not been able to find alternatives to the grpc.bind method in this SDK, which always starts a new listener.

@murgatroid99
Copy link
Member

I don't see any API in Node to use a custom TCP server implementation to create an HTTP/2 or TLS server. So, unless I'm missing something, it doesn't look like this is possible in Node.

@ItalyPaleAle
Copy link
Author

Thanks for the quick response!

Although I haven't tried this yet, looking at the code I wonder if perhaps this could be done by exposing the handlers for http2Server.On('session') and http2Server.On('stream')?

https://github.dev/grpc/grpc-node/blob/c09c874cd39fdfd6e1df95317c34ffa1091f8e42/packages/grpc-js/src/server.ts#L961-L963

According to the official Node.js docs, Http2Session objects are based on net.Socket so if I were to find a way to create a session from an existing socket, I may be able to get it to work?

Do you think this could even be possible?

@murgatroid99
Copy link
Member

The issue there is that ServerHttp2Session is a different type from ClientHttp2Session, and similarly, ServerHttp2Stream is a different type from ClientHttp2Stream, and they have significantly different behaviors. And you can't explicitly create the server types as far as I can tell, they are only provided by Http2Server and Http2SecureServer objects. So no, I don't see that working either.

@ItalyPaleAle
Copy link
Author

I've opened an issue upstream: nodejs/node#46152 Let's see if that goes anywhere :) Thanks again for sharing your thoughts

@murgatroid99
Copy link
Member

I was just looking through the Node documentation for a different reason, and I stumbled on something that might enable this functionality: In the documentation for the 'connection' event on http2.Server, it says

This event can also be explicitly emitted by users to inject connections into the HTTP server. In that case, any Duplex stream can be passed.

@murgatroid99
Copy link
Member

I have written a proposal to add an API for this: grpc/proposal#418

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants