Skip to content

Commit

Permalink
Prevent concurrent reads as well.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchell-as committed Aug 30, 2024
1 parent 9065ff1 commit ac704be
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pkg/runtime/internal/envdef/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func New() *Collection {
}

func (c *Collection) Load(path string) (*EnvironmentDefinition, error) {
// Prevent concurrent reads and writes
c.mutex.Lock()
defer c.mutex.Unlock()

if envDef, ok := c.raw.EnvDefs[path]; ok {
return envDef, nil
}
Expand All @@ -35,23 +39,19 @@ func (c *Collection) Load(path string) (*EnvironmentDefinition, error) {
return nil, errs.Wrap(err, "Failed to initialize environment definition")
}

// Prevent concurrent writes
c.mutex.Lock()
defer c.mutex.Unlock()

c.raw.EnvDefs[path] = envDef
return envDef, nil
}

func (c *Collection) Unload(path string) error {
// Prevent concurrent reads and writes
c.mutex.Lock()
defer c.mutex.Unlock()

if _, ok := c.raw.EnvDefs[path]; !ok {
return errs.New("Environment definition not found for path: %s", path)
}

// Prevent concurrent writes
c.mutex.Lock()
defer c.mutex.Unlock()

delete(c.raw.EnvDefs, path)

return nil
Expand Down

0 comments on commit ac704be

Please sign in to comment.