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: Make workspace watching realtime instead of polling #4922

Merged
merged 2 commits into from Nov 7, 2022
Merged

Conversation

kylecarbs
Copy link
Member

This was leading to performance issues on the frontend, where the page should only be rendered if changes occur. While this could be changed on the frontend, it was always the intention to make this socket ~realtime anyways.

This was leading to performance issues on the frontend, where
the page should only be rendered if changes occur. While this
could be changed on the frontend, it was always the intention
to make this socket ~realtime anyways.
@kylecarbs kylecarbs self-assigned this Nov 6, 2022
Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

Nice fix! I was thinking that 1 * time.Second would become problematic when I saw it last week. 😄


_ = sendEvent(ctx, codersdk.ServerSentEvent{
Type: codersdk.ServerSentEventTypePing,
})
Copy link
Member

Choose a reason for hiding this comment

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

Is this needed because ServerSentEventSender() doesn't send the first ping until the timeout has been hit once? Should we "prime" it in there instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think we want the caller to indicate readiness by sending the ping themselves.

If we don't a race can occur where the client's request has been completed but subscribe hasn't been triggered yet. We could fix this by moving the initialization below the subscribe and making it send a ping, which seems more reasonable.

Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

Seems like this is a bit trickier than I thought... because we use the sendEvent callback in the Subscribe function, we can't initialize after safely. Seems like we'll have to keep it the way it is, but I'll add a comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a comment and will leave it as-is for now because nothing comes to mind as being cleaner... if you have ideas leave em here and I'll implement!

Copy link
Member

@mafredri mafredri Nov 7, 2022

Choose a reason for hiding this comment

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

Hmm, I don't fully understand the race scenario, but I just realized that the httpapi.Write above (if err) could be problematic since we've already primed rw for server side events in httpapi.ServerSentEventSender (and started a timer for pings). So it might be a good idea to change things around a bit 👍🏻. If the subscription fails, I believe this handler wouldn't return until the first SSE auto-ping has been sent (because of the defer <-chan above).

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed that error case! Goooood catch

coderd/workspaces_test.go Outdated Show resolved Hide resolved
coderd/workspacebuilds.go Outdated Show resolved Hide resolved
@BrunoQuaresma
Copy link
Collaborator

@mafredri I also think this is why your CPU usage was so high on the workspace page since the UI had to refresh every second.

@kylecarbs kylecarbs enabled auto-merge (squash) November 7, 2022 15:02
@kylecarbs kylecarbs enabled auto-merge (squash) November 7, 2022 15:15
@kylecarbs kylecarbs merged commit 56b963a into main Nov 7, 2022
@kylecarbs kylecarbs deleted the realtimews branch November 7, 2022 15:25
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants