-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
adaptor ResponseWriter - adding Hijack method and pass proper fields #1525
adaptor ResponseWriter - adding Hijack method and pass proper fields #1525
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the addition. One request, can you add a test that shows that this works and makes sure it doesn't break in the future?
@gilwo friendly ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry but that's not a good test. A good test would have shown that you need to call ctx.Hijack()
for this to really work. The way you implemented it now the net/http handler would think the connection is hijacked but fasthttp would just continue using the connection as it isn't aware of the hijacking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid that this won't work. What you implemented now is not how the Hijack function of net/http expects to work. What you need to do is something like this:
type wrapperConn struct {
wg sync.WaitGroup
net.Conn
}
func (c *wrapperConn) Close() error {
err := c.Conn.Close()
c.wg.Done()
return err
}
func (w *netHTTPResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
conn := wrapperConn{Conn: w.conn}
conn.wg.Add(1)
// This Hijack() function should only return when fasthttp calls the Hijack handler, so use a WaitGroup to block until then.
var wg sync.WaitGroup
wg.add(1)
w.ctx.Hijack(func(net.Conn) {
wg.Done()
// When this Hijack handler returns, fasthttp will close the connection, so wait until the connection is already closed.
// The alternative would be to set Server.KeepHijackedConns = true, but we can't control that here.
conn.wg.Wait()
})
wg.Wait()
return conn, bufio.NewReadWriter{bufio.NewReader(conn), bufio.NewWriter(conn)}, nil
}
…, safe conn handling for proper release of resources and custom hijack handler for more controlled by hijacking implementation
Hi, so what the story with this changeset? One is taking ownership on the connection after hijacking the request and not reaching the later fasthttp hijack handler - which does not make any difference as the connection ends/closed after socket.io connection is closed. The other returns from the hijack and continue the flow assuming no one touch (or close) the response/request afterwards (this differ in the flow of the fasthttp handling). So there it is - i wanted to make the support for both behaviors, this is why the custom hijackhandler. I am not sure if this pass the review, if not maybe it is reasonable to create another custom adaptor? |
You mean one returns from the handler and the other doesn't? Both cases should work fine with the code you have now, that's why you have the WaitGroup. I just don't understand what the |
yes (for reference the projects are go-socket.io and socket.io)
notice the comment for the hijackHandler:
in my code i used fiber as the web framework, fiber initialize KeepHijackedConns as false (default) and do not provide any mean to override this value see discussion reference here, this is why the handler exist as optional param. is the go doc not clear enough, should it be revised ? 🤔 |
8d51843
to
cede3f8
Compare
cede3f8
to
9c12719
Compare
@gilwo I have just pushed some changes to make it work how a net/http handler expects the Hijack method to work. Can you check if this works for you? |
@erikdubbelboer - the change appear to work seamlessly with my test app. this change seems much simpler, thanks! |
this change properly add the adaptor ResponseWriter the capability of Hijacker.
and so it will address issues (#946 and #87) , and maybe others I am not aware of