-
Notifications
You must be signed in to change notification settings - Fork 80
adding websocket support #151
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
base: master
Are you sure you want to change the base?
Conversation
anavin-pub
commented
Jun 26, 2025
- websocket requests are translated into HTTP requests between proxy and agent
- client-proxy and agent-server are connected with websockets
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
ojarjur
left a comment
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.
Thank you so much for putting in the time and effort to put this together.
Sorry for the long delay in responding; there was just too much other things going on and making it hard to make time for a proper review.
Overall this looks good but I do have some comments for how to improve this.
In particular, I think there is one potential scenario that could lead to the server code panicking. I left details in the comments.
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/coder/websocket" |
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.
The coder/websocket library looks like a nice option.
Unfortunately, we're already using gorilla on the agent side to recreate the connection, so this means we'll have two completely separate libraries that do the same thing.
That's not a blocker for this PR in any way; it's just something that seems unfortunate.
Do you have any idea how hard it would be to converge on a single library (either coder or gorilla)?
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.
Would you be ok with me raising another PR to convert the agent part of the websocket code to coder? I had a high level look at agent/websockets code, and it seems to be doable. I am more familiar with the coder library than gorilla :)
Would you have any preference for coder or gorilla?
| p.handleAgentRequest(w, r, backendID) | ||
| return | ||
| func websocketShimResponseHandlerOpen(resp *http.Response, ws *wsSessionHelper) error { | ||
| p, err := io.ReadAll(resp.Body) |
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.
This method doesn't handle the case that the status in the open response is not http.StatusOK.
Additionally, the corresponding logic in the other response handler methods is duplicated.
I think we should move that to the handleFrontendRequest method so that we avoid duplicating it.
I'll leave a comment below for my suggestion on how to do that.
server/server.go
Outdated
| return fmt.Errorf("timeout waiting for the response to %q", id) | ||
| case resp := <-pending.respChan: | ||
| // websocket shim endpoint handling | ||
| switch resp.Request.URL.Path { |
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.
Three suggestions for this:
- Wrap the calls to the response handlers inside of a check that the
wsvariable is not nil; that way we don't risk a panic if an inbound request comes in with the same URL path. - At the start of the nested block, perform the check that the status code from the response is 200.
- Only the open and poll handlers actually do anything with the response (other than checking the status code), so their handlers can be dropped.
E.G.
if ws != nil {
// websocket shim endpoint handling
if resp.StatusCode != http.StatusOK {
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("%v: http status code is %v, error reading response body", r.URL.Path, resp.StatusCode)
}
return fmt.Errorf("%v: http status code %v, response: %v", r.URL.Path, resp.StatusCode, string(respBody))
}
switch r.URL.Path {
case shimPath + "/open":
return websocketShimResponseHandlerOpen(resp, ws)
case shimPath + "/poll":
return websocketShimResponseHandlerPoll(resp, ws)
}
return nil
}| ws := newWsSessionHelper() | ||
|
|
||
| // websocket: shimPath/open | ||
| req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("http://:%v%v/open", port, shimPath), nil) |
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 think the URL for these shim requests has to be based off of the incoming (websocket upgrade) request in order to maintain consistency and make it easier to properly configure the backend server.
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.
Do you mean the shimPath?
In the current setup, it seems that the shimPath needs to be communicated both to the server and the agent before starting up either of the processes. (because it is a command line parameter to both of them and both values need to be same)
We might have to do some changes to both the agent and the server if we want to communicate the shimPath between the agent and server within the websocket upgrade request.
Please let me know your thoughts!
c461996 to
e5d2e43
Compare
|
Thanks for reviewing, let me review your comments and get back! |
b793c1e to
5103a0e
Compare
* websocket requests are translated into HTTP requests between proxy and agent * client-proxy and agent-server are connected with websockets
|
Hi @ojarjur I have made some progress, also had a few questions, please have a look. |