Giter Club home page Giter Club logo

Comments (26)

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024 1

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

gRPC in websocket.Conn

from grpc-go.

purnesh42H avatar purnesh42H commented on July 23, 2024

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

from grpc-go.

purnesh42H avatar purnesh42H commented on July 23, 2024

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

from grpc-go.

easwars avatar easwars commented on July 23, 2024

gRPC in websocket.Conn

Could you please be more specific.

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

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

from grpc-go.

purnesh42H avatar purnesh42H commented on July 23, 2024

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

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

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.

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

So must add a websocket wrapping let connection passthrough middleware.

from grpc-go.

purnesh42H avatar purnesh42H commented on July 23, 2024

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.

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

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.

from grpc-go.

arjan-bal avatar arjan-bal commented on July 23, 2024

@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

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

@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.

from grpc-go.

easwars avatar easwars commented on July 23, 2024

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.

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

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.

from grpc-go.

easwars avatar easwars commented on July 23, 2024

I tried them. But it has some bugs.

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

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

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.

from grpc-go.

easwars avatar easwars commented on July 23, 2024

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?

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

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

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

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

We need blocking I/O for serve single connection.

from grpc-go.

github-actions avatar github-actions commented on July 23, 2024

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.

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

anyone here?

from grpc-go.

github-actions avatar github-actions commented on July 23, 2024

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.

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

anyone here?

from grpc-go.

purnesh42H avatar purnesh42H commented on July 23, 2024

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

from grpc-go.

CoiaPrant233 avatar CoiaPrant233 commented on July 23, 2024

@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

from grpc-go.

purnesh42H avatar purnesh42H commented on July 23, 2024

@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

from grpc-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.