Skip to content

Conversation

@anavin-pub
Copy link

  • websocket requests are translated into HTTP requests between proxy and agent
  • client-proxy and agent-server are connected with websockets

@google-cla
Copy link

google-cla bot commented Jun 26, 2025

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.

Copy link
Collaborator

@ojarjur ojarjur left a 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"
Copy link
Collaborator

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)?

Copy link
Author

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)
Copy link
Collaborator

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Three suggestions for this:

  1. Wrap the calls to the response handlers inside of a check that the ws variable is not nil; that way we don't risk a panic if an inbound request comes in with the same URL path.
  2. At the start of the nested block, perform the check that the status code from the response is 200.
  3. 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)
Copy link
Collaborator

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.

Copy link
Author

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!

@anavin-pub anavin-pub force-pushed the shim-pr branch 2 times, most recently from c461996 to e5d2e43 Compare October 27, 2025 08:54
@anavin-pub
Copy link
Author

Thanks for reviewing, let me review your comments and get back!

@anavin-pub anavin-pub force-pushed the shim-pr branch 2 times, most recently from b793c1e to 5103a0e Compare January 22, 2026 11:05
* websocket requests are translated into HTTP requests between proxy and agent
* client-proxy and agent-server are connected with websockets
@anavin-pub
Copy link
Author

Hi @ojarjur
Sorry for the delay in response, I couldn't work on this towards the end of last year.

I have made some progress, also had a few questions, please have a look.

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