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

feat: improve ws.conn handling #57

Closed
wants to merge 5 commits into from

Conversation

denopink
Copy link
Contributor

@denopink denopink commented Apr 2, 2024

Doesn't include func (ws *websocket) Send() update

@denopink denopink requested a review from johnabass April 2, 2024 17:08
@denopink denopink marked this pull request as draft April 2, 2024 17:08
@denopink denopink force-pushed the denopink/feat/improve-ws-conn-handling branch 3 times, most recently from cddd22e to 9b0b578 Compare April 2, 2024 22:07
@@ -44,14 +48,19 @@ func TestEndToEnd(t *testing.T) {
require.NoError(err)

mt, got, err := c.Read(ctx)
// server will halt until the websocket closes resulting in a EOF
if finished && errors.Is(err, io.EOF) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use errors.Is here. In fact, you really shouldn't. io.Reader.Read is documented as returning exactly io.EOF in this case. I realize the linter will complain, which is why I hate our linter :)

https://pkg.go.dev/io#Reader

internal/websocket/e2e_test.go Outdated Show resolved Hide resolved
@denopink denopink force-pushed the denopink/feat/improve-ws-conn-handling branch 2 times, most recently from 8d1fa9b to 3dd1841 Compare April 2, 2024 22:14
Comment on lines -259 to +267
ctx, cancel := context.WithTimeout(r.Context(), 2000000*time.Millisecond)
ctx, cancel := context.WithTimeout(r.Context(), 200*time.Millisecond)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a silly artifact of some test debugging code in a previous pr

@denopink denopink force-pushed the denopink/feat/improve-ws-conn-handling branch from 3dd1841 to 47faddb Compare April 2, 2024 22:21
}

// Stop stops the websocket connection.
func (ws *Websocket) Stop() {
ws.m.Lock()
// Avoid the overhead of the close handshake.
ws.conn.Close(nhws.StatusNormalClosure, "")
Copy link
Contributor Author

@denopink denopink Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like nhooyr.io/websocket's websocket doesn't like it when you use its own nhws.StatusNormalClosure to close the socket... I was expecting a normal EOF:

Received unexpected error:
        	            	failed to get reader: received close frame: status = StatusNormalClosure and reason = ""

mt, got, err := c.Read(ctx)

    	Error Trace:	/Users/odc/Documents/GitHub/xmidt-org/xmidt-agent/internal/websocket/e2e_test.go:56
    	            				/Users/odc/go/pkg/mod/golang.org/[email protected]/src/net/http/server.go:2136
    	            				/Users/odc/go/pkg/mod/golang.org/[email protected]/src/net/http/server.go:2938
    	            				/Users/odc/go/pkg/mod/golang.org/[email protected]/src/net/http/server.go:2009
    	            				/Users/odc/go/pkg/mod/golang.org/[email protected]/src/runtime/asm_amd64.s:1650
    	Error:      	Received unexpected error:
    	            	failed to get reader: received close frame: status = StatusNormalClosure and reason = ""
    	Test:       	TestEndToEnd

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume nhws.StatusNormalClosure would trigger a io.EOF and not a unexpected error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like nhooyr.io/websocket's Reader doesn't return an io.EOF for a StatusNormalClosure (status code 1000).
#57 (comment)

just a fyi

@denopink denopink force-pushed the denopink/feat/improve-ws-conn-handling branch from 47faddb to 54bb265 Compare April 2, 2024 22:39
@denopink denopink force-pushed the denopink/feat/improve-ws-conn-handling branch from ad58af4 to d5f5710 Compare April 2, 2024 22:52
@denopink denopink requested a review from schmidtw April 3, 2024 00:14
@denopink denopink requested a review from johnabass April 3, 2024 10:31
@denopink denopink marked this pull request as ready for review April 3, 2024 11:08
@denopink denopink closed this Apr 3, 2024
@denopink denopink deleted the denopink/feat/improve-ws-conn-handling branch July 15, 2024 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants