grpc-go icon indicating copy to clipboard operation
grpc-go copied to clipboard

Feature Request: expose handleRawConn or add ServeConn

Open CoiaPrant233 opened this issue 1 year ago • 21 comments

Use case(s) - what problem will this feature solve?

gRPC in custom implemented net.Conn

Proposed Solution

expose handleRawConn

Alternatives Considered

add ServeConn interface

Additional Context

ServeConn should block

	go func() {
		s.serveStreams(context.Background(), st, rawConn)
		s.removeConn(lisAddr, st)
	}()

to

	defer s.removeConn(lisAddr, st)
	s.serveStreams(context.Background(), st, rawConn)

CoiaPrant233 avatar Jun 09 '24 17:06 CoiaPrant233

@CoiaPrant233 I will take a look and get back to you

purnesh42H avatar Jun 11 '24 16:06 purnesh42H

@CoiaPrant233 could you describe your use case for this request?

purnesh42H avatar Jun 14 '24 07:06 purnesh42H

@CoiaPrant233 could you describe your use case for this request?

gRPC in websocket.Conn

ghost avatar Jun 14 '24 07:06 ghost

gRPC in websocket.Conn

Could you please be more specific.

easwars avatar Jun 14 '24 14:06 easwars

I wrapped a websocket implement net.Conn, but its required block I/O. If http handler function was returned. websocket will be closed automatically.

ghost avatar Jun 14 '24 18:06 ghost

Could you explain what is your usecase that needs websocket connection? Is it for a web application? In case you haven't yet, take a look at grpc Bidirectional Streaming and see if that can help with your usecase

purnesh42H avatar Jun 14 '24 19:06 purnesh42H

Could you explain what is your usecase that needs websocket connection? Is it for a web application? In case you haven't yet, take a look at grpc Bidirectional Streaming and see if that can help with your usecase

Some middleware without http/2 or grpc support. Not all CDN like cloudflare support http/2 to origin or grpc to origin.

ghost avatar Jun 14 '24 19:06 ghost

So must add a websocket wrapping let connection passthrough middleware.

ghost avatar Jun 14 '24 19:06 ghost

Some middleware without http/2 or grpc support. Not all CDN like cloudflare support http/2 to origin or grpc to origin.

Have you tried exploring grpc-web? This allows grpc to work over HTTP/1.1 which is supported by cloudfare.

purnesh42H avatar Jun 15 '24 11:06 purnesh42H

Some middleware without http/2 or grpc support. Not all CDN like cloudflare support http/2 to origin or grpc to origin.

Have you tried exploring grpc-web? This allows grpc to work over HTTP/1.1 which is supported by cloudfare.

Yes, but I need Bi-directional stream.

ghost avatar Jun 15 '24 14:06 ghost

@CoiaPrant233 you can try using an HTTP tunnel (also called HTTP Connect Proxy). This should allow you to bypass the middleware if the HTTP 1.1 proxy server is placed behind the middleware.

gRPC-go supports HTTP tunneling

arjan-bal avatar Jun 15 '24 19:06 arjan-bal

@CoiaPrant233 you can try using an HTTP tunnel (also called HTTP Connect Proxy). This should allow you to bypass the middleware if the HTTP 1.1 proxy server is placed behind the middleware.

gRPC-go supports HTTP tunneling

I want to passthrough CDN to protect origin server.

ghost avatar Jun 15 '24 20:06 ghost

Providing a custom way to handle the incoming connection is not something we are very keen on doing. Would one of the following options work for you?

  • Run your own proxy next to your server and terminate the websocket connection there. Have the proxy talk gRPC to your server backend.
  • Provide a net.Listener which actually wraps a websocket conn (to grpc.Serve) and do your custom handling there.

easwars avatar Jun 17 '24 20:06 easwars

Providing a custom way to handle the incoming connection is not something we are very keen on doing. Would one of the following options work for you?

  • Run your own proxy next to your server and terminate the websocket connection there. Have the proxy talk gRPC to your server backend.
  • Provide a which actually wraps a websocket conn (to ) and do your custom handling there.net.Listener``grpc.Serve

I tried them. But it has some bugs.

ghost avatar Jun 18 '24 06:06 ghost

I tried them. But it has some bugs.

Could you provide details on what you tried and what bugs you found? Thanks.

easwars avatar Jun 18 '24 15:06 easwars

I tried them. But it has some bugs.

Could you provide details on what you tried and what bugs you found? Thanks.

WebSocket library not has wait close function, I tried wrapped to net.Listener, but I can't immediately determine if the connection is closed. If I want to check its state I must send a websocket ping message and check its has write error.

It looks like terrible. In a high concurrency environment, a websocket object will be held for a long time and will not be released even if it is closed. If blocking IO is used, grpc returns and frees the web socket object in the function immediately after processing the connection.

ghost avatar Jun 18 '24 18:06 ghost

I tried wrapped to net.Listener, but I can't immediately determine if the connection is closed.

From your wrapped net.Listener you can return a wrapped net.Conn, whose Close method should be invoked when the connection is closed. Does that work for you?

a websocket object will be held for a long time and will not be released even if it is closed

I don't know much about websocket, but gRPC does provide a way to close connections that have been open for more than a configurable time. See here: https://pkg.go.dev/google.golang.org/grpc/keepalive#ServerParameters

What issues did you run into when running your own proxy to terminate the websocket connection?

easwars avatar Jun 18 '24 18:06 easwars

I think blocking IO is better. Just add a new function.

or u can removed async call in handleRawConn

At server.go#L926 already call async function At server.go#L958 call async function again

I think its useless to call twice async. u can modify it like ServeHTTP

from

// handleRawConn forks a goroutine to handle a just-accepted connection that
// has not had any I/O performed on it yet.
func (s *Server) handleRawConn(lisAddr string, rawConn net.Conn) {
	if s.quit.HasFired() {
		rawConn.Close()
		return
	}
	rawConn.SetDeadline(time.Now().Add(s.opts.connectionTimeout))

	// Finish handshaking (HTTP2)
	st := s.newHTTP2Transport(rawConn)
	rawConn.SetDeadline(time.Time{})
	if st == nil {
		return
	}

	if cc, ok := rawConn.(interface {
		PassServerTransport(transport.ServerTransport)
	}); ok {
		cc.PassServerTransport(st)
	}

	if !s.addConn(lisAddr, st) {
		return
	}
	go func() {
		s.serveStreams(context.Background(), st, rawConn)
		s.removeConn(lisAddr, st)
	}()
}

to

// handleRawConn forks a goroutine to handle a just-accepted connection that
// has not had any I/O performed on it yet.
func (s *Server) handleRawConn(lisAddr string, rawConn net.Conn) {
	if s.quit.HasFired() {
		rawConn.Close()
		return
	}
	rawConn.SetDeadline(time.Now().Add(s.opts.connectionTimeout))

	// Finish handshaking (HTTP2)
	st := s.newHTTP2Transport(rawConn)
	rawConn.SetDeadline(time.Time{})
	if st == nil {
		return
	}

	if cc, ok := rawConn.(interface {
		PassServerTransport(transport.ServerTransport)
	}); ok {
		cc.PassServerTransport(st)
	}

	if !s.addConn(lisAddr, st) {
		return
	}
	defer s.removeConn(lisAddr, st)
	s.serveStreams(context.Background(), st, rawConn)
}

then I can use unsafe to force expose it

ghost avatar Jun 18 '24 18:06 ghost

I think add new function ServeConn like ServeHTTP its best resolution.

We need blocking I/O for serve single connection.

ghost avatar Jun 18 '24 18:06 ghost

This issue is labeled as requiring an update from the reporter, and no update has been received after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed.

github-actions[bot] avatar Jun 24 '24 20:06 github-actions[bot]

anyone here?

ghost avatar Jun 25 '24 03:06 ghost

This issue is labeled as requiring an update from the reporter, and no update has been received after 6 days. If no update is provided in the next 7 days, this issue will be automatically closed.

github-actions[bot] avatar Jul 01 '24 04:07 github-actions[bot]

anyone here?

ghost avatar Jul 03 '24 10:07 ghost

@CoiaPrant233 could you provide more details on how you are implementing net.Listener and net.Conn for websocket connection?

purnesh42H avatar Jul 03 '24 17:07 purnesh42H

@CoiaPrant233 could you provide more details on how you are implementing net.Listener and net.Conn for websocket connection?

package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"nhooyr.io/websocket"
)

func Serve(c *gin.Context) {
	if c.IsWebsocket() {
		c.AbortWithStatus(http.StatusUpgradeRequired)
		return
	}

	ws, err := websocket.Accept(c.Writer, c.Request, &websocket.AcceptOptions{})
	if err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	conn := websocket.NetConn(c,ws,websocket.MessageBinary)
	defer conn.Close()

	// grpc handleRawConn
}

Here is a example

ghost avatar Jul 04 '24 05:07 ghost

@CoiaPrant233 you need to first implement a custom net.Listener which actually wraps a WebSocket connection. The idea is to create a custom listener that accepts WebSocket connections and then wraps these connections to provide the standard net.Conn interface.

  • Create a struct for the WebSocket listener and implement the net.Listener interface.
  • Next, create a struct for the WebSocket connection and implement the net.Conn interface.
  • After that, you can create the handler to accept WebSocket connections and add them to the listener and finally set up the server to listen for WebSocket connections

This way, as suggested above, your wrapped net.Conn Close method will be invoked and you can handle it as you need.

With a listener we just call Accept and handleRawConn in a loop so you don't have to actually override Serve

purnesh42H avatar Jul 04 '24 06:07 purnesh42H