-
Notifications
You must be signed in to change notification settings - Fork 894
feat: Make workspace watching realtime instead of polling #4922
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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.
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -634,6 +634,10 @@ func (api *API) patchWorkspace(rw http.ResponseWriter, r *http.Request) { | |
return | ||
} | ||
|
||
if !api.publishWorkspaceUpdate(ctx, rw, workspace.ID) { | ||
return | ||
} | ||
|
||
aReq.New = newWorkspace | ||
rw.WriteHeader(http.StatusNoContent) | ||
} | ||
|
@@ -839,7 +843,7 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) { | |
return err | ||
} | ||
|
||
if err := s.UpdateWorkspaceBuildByID(ctx, database.UpdateWorkspaceBuildByIDParams{ | ||
if _, err := s.UpdateWorkspaceBuildByID(ctx, database.UpdateWorkspaceBuildByIDParams{ | ||
ID: build.ID, | ||
UpdatedAt: build.UpdatedAt, | ||
ProvisionerState: build.ProvisionerState, | ||
|
@@ -883,48 +887,60 @@ func (api *API) watchWorkspace(rw http.ResponseWriter, r *http.Request) { | |
// Ignore all trace spans after this, they're not too useful. | ||
ctx = trace.ContextWithSpan(ctx, tracing.NoopSpan) | ||
|
||
t := time.NewTicker(time.Second * 1) | ||
defer t.Stop() | ||
cancelSubscribe, err := api.Pubsub.Subscribe(watchWorkspaceChannel(workspace.ID), func(_ context.Context, _ []byte) { | ||
workspace, err := api.Database.GetWorkspaceByID(ctx, workspace.ID) | ||
if err != nil { | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeError, | ||
Data: codersdk.Response{ | ||
Message: "Internal error fetching workspace.", | ||
Detail: err.Error(), | ||
}, | ||
}) | ||
return | ||
} | ||
|
||
data, err := api.workspaceData(ctx, []database.Workspace{workspace}) | ||
if err != nil { | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeError, | ||
Data: codersdk.Response{ | ||
Message: "Internal error fetching workspace data.", | ||
Detail: err.Error(), | ||
}, | ||
}) | ||
return | ||
} | ||
|
||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeData, | ||
Data: convertWorkspace( | ||
workspace, | ||
data.builds[0], | ||
data.templates[0], | ||
findUser(workspace.OwnerID, data.users), | ||
), | ||
}) | ||
}) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error subscribing to workspace events.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
defer cancelSubscribe() | ||
|
||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypePing, | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want the caller to indicate readiness by sending the If we don't a race can occur where the client's request has been completed but Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed that error case! Goooood catch |
||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case <-senderClosed: | ||
return | ||
case <-t.C: | ||
workspace, err := api.Database.GetWorkspaceByID(ctx, workspace.ID) | ||
if err != nil { | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeError, | ||
Data: codersdk.Response{ | ||
Message: "Internal error fetching workspace.", | ||
Detail: err.Error(), | ||
}, | ||
}) | ||
return | ||
} | ||
|
||
data, err := api.workspaceData(ctx, []database.Workspace{workspace}) | ||
if err != nil { | ||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeError, | ||
Data: codersdk.Response{ | ||
Message: "Internal error fetching workspace data.", | ||
Detail: err.Error(), | ||
}, | ||
}) | ||
return | ||
} | ||
|
||
_ = sendEvent(ctx, codersdk.ServerSentEvent{ | ||
Type: codersdk.ServerSentEventTypeData, | ||
Data: convertWorkspace( | ||
workspace, | ||
data.builds[0], | ||
data.templates[0], | ||
findUser(workspace.OwnerID, data.users), | ||
), | ||
}) | ||
} | ||
} | ||
} | ||
|
@@ -1213,3 +1229,19 @@ func splitQueryParameterByDelimiter(query string, delimiter rune, maintainQuotes | |
|
||
return parts | ||
} | ||
|
||
func watchWorkspaceChannel(id uuid.UUID) string { | ||
return fmt.Sprintf("workspace:%s", id) | ||
} | ||
|
||
func (api *API) publishWorkspaceUpdate(ctx context.Context, rw http.ResponseWriter, workspaceID uuid.UUID) bool { | ||
err := api.Pubsub.Publish(watchWorkspaceChannel(workspaceID), []byte{}) | ||
if err != nil { | ||
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Internal error publishing workspace update.", | ||
Detail: err.Error(), | ||
}) | ||
return false | ||
} | ||
return true | ||
} |
Uh oh!
There was an error while loading. Please reload this page.