-
Notifications
You must be signed in to change notification settings - Fork 889
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
Conversation
@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. |
BTW this is pretty much the exact same as server-side events. |
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. |
As far as where this is coming from, it's just porting a trimmed down version of what we do in V1 for the |
Ahh, I'm more so wondering how other products attack this polling problem. I know Discord has a WebSocket that updates information |
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:
But I've chosen this way so far because:
|
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. |
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.
Is this actually necessary if we're already sending updates every second?
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 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.
c24fac0
to
ec066aa
Compare
// SessionTokenKey represents the name of the cookie or query paramater the API key is stored in. | ||
const SessionTokenKey = "session_token" |
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.
Wise rename. AuthCookie
was questionable misdirection anyways!
codersdk/workspaces.go
Outdated
return nil | ||
} | ||
|
||
func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) { |
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.
Could this return a channel instead? <-chan Workspace
could be nice!
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.
Sure thing that sounds nice
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 looks good to me - we're returning a codersdk.Workspace
on an interval via WS
.
8db2066
to
cc3f57f
Compare
cc3f57f
to
534bcdb
Compare
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:
session_token
query parameter on authenticated callshttpmw.AuthCookie
tohttpmw.SessionTokeyKey
since we now use it for query parametersdialWebsocket
method tocodersdk.Client