[ws-proxy] Don't leak workspace info Go routines

This commit is contained in:
Christian Weichel 2021-01-19 09:25:02 +00:00
parent 1ac8fbeef3
commit 396b51ed1f
2 changed files with 9 additions and 5 deletions

View File

@ -275,8 +275,15 @@ func mapWorkspaceStatusToInfo(status *wsapi.WorkspaceStatus) *WorkspaceInfo {
}
}
// WorkspaceInfo return the WorkspaceInfo avaiable for the given workspaceID
// WorkspaceInfo return the WorkspaceInfo avaiable for the given workspaceID.
// Callers should make sure their context gets canceled properly. For good measure
// this function will timeout by itself as well.
func (p *RemoteWorkspaceInfoProvider) WorkspaceInfo(ctx context.Context, workspaceID string) *WorkspaceInfo {
// In case the parent context does not cancel for some reason, we want to make sure
// we clean up after ourselves to not leak Go routines.
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
info, present := p.cache.WaitFor(ctx, workspaceID)
if !present {
return nil

View File

@ -482,10 +482,7 @@ func workspaceMustExistHandler(config *Config, infoProvider WorkspaceInfoProvide
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
coords := getWorkspaceCoords(req)
// it might take some time until the event comes in. Let's wait for it at most 3 secs.
ctx, cancel := context.WithTimeout(req.Context(), 3*time.Second)
defer cancel()
info := infoProvider.WorkspaceInfo(ctx, coords.ID)
info := infoProvider.WorkspaceInfo(req.Context(), coords.ID)
if info == nil {
log.WithFields(log.OWI("", coords.ID, "")).Info("no workspace info found - redirecting to start")
redirectURL := fmt.Sprintf("%s://%s/start/#%s", config.GitpodInstallation.Scheme, config.GitpodInstallation.HostName, coords.ID)