@@ -35,8 +35,7 @@ type GrpcDialer interface {
3535 Dial (ctx context.Context ) (* grpc.ClientConn , error )
3636}
3737
38- // CompatV1 is a shim that acts like a plugin server and delegates request to
39- // something that implements a V1-style API interface.
38+ // CompatV1 is an API interface that is compatible with the V1 API.
4039type CompatV1 struct {
4140 GetPluginInfo func () papiv1.Info
4241 GetInstance func (user papiv1.UserContext ) (papiv1.Plugin , error )
@@ -260,11 +259,14 @@ func (h *shimV1MessageHandler) SendMessage(msg papiv1.Message) error {
260259}
261260
262261type shimV1StorageHandler struct {
262+ mutex * sync.RWMutex
263263 currentStorage []byte
264264 stream * protobuf.Plugin_RunUserInstanceServer
265265}
266266
267267func (h * shimV1StorageHandler ) Save (b []byte ) error {
268+ h .mutex .Lock ()
269+ defer h .mutex .Unlock ()
268270 h .currentStorage = slices .Clone (b )
269271 return (* h .stream ).Send (& protobuf.InstanceUpdate {
270272 Update : & protobuf.InstanceUpdate_Storage {
@@ -274,6 +276,8 @@ func (h *shimV1StorageHandler) Save(b []byte) error {
274276}
275277
276278func (h * shimV1StorageHandler ) Load () (b []byte , err error ) {
279+ h .mutex .RLock ()
280+ defer h .mutex .RUnlock ()
277281 b = slices .Clone (h .currentStorage )
278282 return
279283}
@@ -285,6 +289,16 @@ type compatV1ShimServer struct {
285289 protobuf.UnimplementedConfigurerServer
286290}
287291
292+ func (s * CompatV1Shim ) getInstanceByUserId (userId uint64 ) (papiv1.Plugin , error ) {
293+ s .mu .RLock ()
294+ instance , ok := s .instances [userId ]
295+ s .mu .RUnlock ()
296+ if ! ok {
297+ return nil , errors .New ("instance not found" )
298+ }
299+ return instance , nil
300+ }
301+
288302func (s * compatV1ShimServer ) GetPluginInfo (ctx context.Context , req * emptypb.Empty ) (* protobuf.Info , error ) {
289303 return & protobuf.Info {
290304 Version : s .shim .pluginInfo .Version ,
@@ -312,11 +326,9 @@ func (s *compatV1ShimServer) SetEnable(ctx context.Context, req *protobuf.SetEna
312326}
313327
314328func (s * compatV1ShimServer ) Display (ctx context.Context , req * protobuf.DisplayRequest ) (* protobuf.DisplayResponse , error ) {
315- s .shim .mu .RLock ()
316- instance , ok := s .shim .instances [req .User .Id ]
317- s .shim .mu .RUnlock ()
318- if ! ok {
319- return nil , errors .New ("instance not found" )
329+ instance , err := s .shim .getInstanceByUserId (req .User .Id )
330+ if err != nil {
331+ return nil , err
320332 }
321333 if displayer , ok := instance .(papiv1.Displayer ); ok {
322334 location , err := url .Parse (req .Location )
@@ -333,11 +345,9 @@ func (s *compatV1ShimServer) Display(ctx context.Context, req *protobuf.DisplayR
333345}
334346
335347func (s * compatV1ShimServer ) DefaultConfig (ctx context.Context , req * protobuf.DefaultConfigRequest ) (* protobuf.Config , error ) {
336- s .shim .mu .RLock ()
337- instance , ok := s .shim .instances [req .User .Id ]
338- s .shim .mu .RUnlock ()
339- if ! ok {
340- return nil , errors .New ("instance not found" )
348+ instance , err := s .shim .getInstanceByUserId (req .User .Id )
349+ if err != nil {
350+ return nil , err
341351 }
342352 if configurer , ok := instance .(papiv1.Configurer ); ok {
343353 defaultConfig := configurer .DefaultConfig ()
@@ -353,11 +363,9 @@ func (s *compatV1ShimServer) DefaultConfig(ctx context.Context, req *protobuf.De
353363}
354364
355365func (s * compatV1ShimServer ) ValidateAndSetConfig (ctx context.Context , req * protobuf.ValidateAndSetConfigRequest ) (* protobuf.ValidateAndSetConfigResponse , error ) {
356- s .shim .mu .RLock ()
357- instance , ok := s .shim .instances [req .User .Id ]
358- s .shim .mu .RUnlock ()
359- if ! ok {
360- return nil , errors .New ("instance not found" )
366+ instance , err := s .shim .getInstanceByUserId (req .User .Id )
367+ if err != nil {
368+ return nil , err
361369 }
362370 if configurer , ok := instance .(papiv1.Configurer ); ok {
363371 currentConfig := configurer .DefaultConfig ()
@@ -487,7 +495,9 @@ func (s *compatV1ShimServer) RunUserInstance(req *protobuf.UserInstanceRequest,
487495 if slices .Contains (req .ServerVersion .Capabilities , protobuf .Capability_CONFIGURER ) {
488496 currentConfig := configurer .DefaultConfig ()
489497 if req .Config != nil {
490- yaml .Unmarshal (req .Config , & currentConfig )
498+ if err := yaml .Unmarshal (req .Config , & currentConfig ); err != nil {
499+ return err
500+ }
491501 if err := configurer .ValidateAndSetConfig (currentConfig ); err != nil {
492502 return err
493503 }
@@ -499,6 +509,7 @@ func (s *compatV1ShimServer) RunUserInstance(req *protobuf.UserInstanceRequest,
499509
500510 if storager , ok := instance .(papiv1.Storager ); ok {
501511 storageHandler := & shimV1StorageHandler {
512+ mutex : & sync.RWMutex {},
502513 currentStorage : req .Storage ,
503514 stream : & stream ,
504515 }
0 commit comments