Skip to content

Commit 777a9cb

Browse files
authored
[supervisor] cache default workspace image (#18845)
1 parent d897a6c commit 777a9cb

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

components/supervisor/pkg/supervisor/services.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,14 @@ type InfoService struct {
647647
GitpodService serverapi.APIInterface
648648

649649
api.UnimplementedInfoServiceServer
650+
651+
cacheMu sync.RWMutex
652+
cacheTimestamp time.Time
653+
defaultWorkspaceImage string
650654
}
651655

656+
var infoCacheDuration = 5 * time.Second
657+
652658
// RegisterGRPC registers the gRPC info service.
653659
func (is *InfoService) RegisterGRPC(srv *grpc.Server) {
654660
api.RegisterInfoServiceServer(srv, is)
@@ -659,20 +665,34 @@ func (is *InfoService) RegisterREST(mux *runtime.ServeMux, grpcEndpoint string)
659665
return api.RegisterInfoServiceHandlerFromEndpoint(context.Background(), mux, grpcEndpoint, []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())})
660666
}
661667

662-
func (is *InfoService) getDefaultWorkspaceImage(ctx context.Context) (defaultWorkspaceImage string) {
663-
defaultWorkspaceImage = is.cfg.DefaultWorkspaceImage
664-
if defaultWorkspaceImage == "" {
665-
// TODO: delete-me, added for compatibility before server is deployed / rollback
666-
defaultWorkspaceImage = "gitpod/workspace-full:latest"
668+
func (is *InfoService) getDefaultWorkspaceImage(ctx context.Context) string {
669+
is.cacheMu.RLock()
670+
671+
if time.Since(is.cacheTimestamp) < infoCacheDuration {
672+
is.cacheMu.RUnlock()
673+
return is.defaultWorkspaceImage
667674
}
675+
is.cacheMu.RUnlock()
676+
677+
is.cacheMu.Lock()
678+
defer is.cacheMu.Unlock()
679+
defer func() {
680+
is.cacheTimestamp = time.Now()
681+
}()
682+
683+
if time.Since(is.cacheTimestamp) < infoCacheDuration {
684+
return is.defaultWorkspaceImage
685+
}
686+
687+
is.defaultWorkspaceImage = is.cfg.DefaultWorkspaceImage
668688
if is.GitpodService == nil {
669-
return
689+
return is.defaultWorkspaceImage
670690
}
671691
wsImage, err := is.GitpodService.GetDefaultWorkspaceImage(ctx)
672692
if err == nil {
673-
defaultWorkspaceImage = wsImage
693+
is.defaultWorkspaceImage = wsImage
674694
}
675-
return
695+
return is.defaultWorkspaceImage
676696
}
677697

678698
// WorkspaceInfo provides information about the workspace.

0 commit comments

Comments
 (0)