Comments (26)
@CoiaPrant233 could you describe your use case for this request?
gRPC in websocket.Conn
from grpc-go.
@CoiaPrant233 I will take a look and get back to you
from grpc-go.
@CoiaPrant233 could you describe your use case for this request?
from grpc-go.
gRPC in websocket.Conn
Could you please be more specific.
from grpc-go.
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.
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.
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.
So must add a websocket wrapping let connection passthrough middleware.
from grpc-go.
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.
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.
@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 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.
I want to passthrough CDN to protect origin server.
from grpc-go.
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 (togrpc.Serve
) and do your custom handling there.
from grpc-go.
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.
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.
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.
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.
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.
I think add new function ServeConn
like ServeHTTP
its best resolution.
We need blocking I/O for serve single connection.
from grpc-go.
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.
anyone here?
from grpc-go.
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.
anyone here?
from grpc-go.
@CoiaPrant233 could you provide more details on how you are implementing net.Listener
and net.Conn
for websocket connection?
from grpc-go.
@CoiaPrant233 could you provide more details on how you are implementing
net.Listener
andnet.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.
@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)
- protoc gen adds comment, does not follow godoc. HOT 5
- Flaky test: Test/AggregateCluster_BadDNS_GoodEDS HOT 3
- Setting HTTPS proxy without setting HTTPS_PROXY env variable HOT 2
- channelz has a startup sequencing bug HOT 2
- ring_hash stuck in TransientFailure despite having available endpoints HOT 1
- Flaky Test/AuthorityRevive in google.golang.org/grpc/xds/internal/xdsclient/tests HOT 3
- Implement Custom Types for gRPC-Go Framer HOT 1
- xds: implement ADS stream flow control mechanism
- xds/csds: unskip test after ADS stream flow control changes are merged
- 'use of closed network connection', what's the best practice to deal with this? HOT 7
- Infinite loop in bufWriter.Write() HOT 7
- metadata: MD.String should only show metadata known to not contain tokens or other PII HOT 1
- Over 300+ groutines after only a few minutes of testing HOT 6
- Allow client-side access to `grpc-status` and `grpc-message` HOT 2
- scripts: fixes/improvements to regenerate.sh / install_protoc.sh
- Surface response body when receiving an unexpected status code and content-type HOT 1
- Default fallback scheme changed from `passthrough` to `dns` HOT 2
- Support for custom code check HOT 3
- grpc: error unmarshalling request: failed to unmarshal, message is X, want proto.Message HOT 1
- Is it ok to call grpc.UnaryInvoker multiple times inside an interceptor? HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grpc-go.