Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: Add watch workspace endpoint #1493

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

Merged
merged 13 commits into from
May 18, 2022
Merged

chore: Add watch workspace endpoint #1493

merged 13 commits into from
May 18, 2022

Conversation

f0ssel
Copy link
Contributor

@f0ssel f0ssel commented May 16, 2022

To avoid polling frequently I'm adding a /watch endpoint to workspaces that will pump the data we currently poll for over a one-way websocket.

Changes:

  • Added support for session_token query parameter on authenticated calls
  • Changed httpmw.AuthCookie to httpmw.SessionTokeyKey since we now use it for query parameters
  • Added watch workspace endpoint
  • Added dialWebsocket method to codersdk.Client

@kylecarbs
Copy link
Member

@f0ssel I don't have much information on polling, but I'm curious where you got this pattern from. I've seen server-side events before, but I haven't seen this! Seems reasonable though... so not sure why I haven't seen it.

@kylecarbs
Copy link
Member

BTW this is pretty much the exact same as server-side events.

@f0ssel
Copy link
Contributor Author

f0ssel commented May 16, 2022

Yeah I'm unfamiliar with Server Side Events, my brief look seems like it's a javascript websocket standard. If there's anything I need to change to make this PR compatible with that web API I'm down to do so, would just like to be pointed in the right direction.

@f0ssel
Copy link
Contributor Author

f0ssel commented May 16, 2022

As far as where this is coming from, it's just porting a trimmed down version of what we do in V1 for the watch-stats, devurls/watch-status, etc.

@kylecarbs
Copy link
Member

Ahh, I'm more so wondering how other products attack this polling problem. I know Discord has a WebSocket that updates information

@f0ssel
Copy link
Contributor Author

f0ssel commented May 16, 2022

I'm not sure but I'll break down my thoughts so far

I think there's a few things we could improve here like:

  • Multiplex updates to many different entities over the same websocket instead of opening multiple
  • Only send updates on changes or on a backoff if data has not changed
  • Gracefully handle errors instead of closing the websocket connection

But I've chosen this way so far because:

  • The workspace entity having near-real-time updates seems like it will get us pretty far, I don't think there's plans to have other resources that will use this pattern.
  • We already have code to support this pattern on the frontend and I'm optimizing for not adding more scope for community MVP.
  • It's a familiar pattern from V1 that seemed to work fine, I don't think we've had much issues around it at the api layer.

@dwahler
Copy link
Contributor

dwahler commented May 17, 2022

I like this approach because in the future we can optimize the backend by removing unchanged values and/or replacing the internal DB polling loop with some kind of pub/sub system, without having to change the API at all.

// Makes the websocket connection write-only
ctx := c.CloseRead(r.Context())

// Send a heartbeat every 15 seconds to avoid the websocket being killed.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this actually necessary if we're already sending updates every second?

Copy link
Contributor Author

@f0ssel f0ssel May 17, 2022

Choose a reason for hiding this comment

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

This is a great callout, I haven't considered it but you are correct.

That said, I'd like to have the precedent that we always do a heartbeat for websockets as a best practice. If the behavior of this websocket changes to only send updates and we go longer than 15 seconds without an update the browser could close the connection on us.

@f0ssel f0ssel force-pushed the f0ssel/watch-workspace branch from c24fac0 to ec066aa Compare May 17, 2022 20:38
@f0ssel f0ssel marked this pull request as ready for review May 17, 2022 20:59
@f0ssel f0ssel requested review from greyscaled and kylecarbs May 17, 2022 20:59
Comment on lines +20 to +21
// SessionTokenKey represents the name of the cookie or query paramater the API key is stored in.
const SessionTokenKey = "session_token"
Copy link
Member

Choose a reason for hiding this comment

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

Wise rename. AuthCookie was questionable misdirection anyways!

return nil
}

func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Could this return a channel instead? <-chan Workspace could be nice!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing that sounds nice

Copy link
Contributor

@greyscaled greyscaled left a comment

Choose a reason for hiding this comment

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

This looks good to me - we're returning a codersdk.Workspace on an interval via WS.

@f0ssel f0ssel requested a review from kylecarbs May 18, 2022 16:14
@f0ssel f0ssel force-pushed the f0ssel/watch-workspace branch from 8db2066 to cc3f57f Compare May 18, 2022 16:41
@f0ssel f0ssel force-pushed the f0ssel/watch-workspace branch from cc3f57f to 534bcdb Compare May 18, 2022 16:44
@f0ssel f0ssel merged commit 0706c60 into main May 18, 2022
@f0ssel f0ssel deleted the f0ssel/watch-workspace branch May 18, 2022 21:16
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
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.

4 participants