Skip to content

Commit

Permalink
Don't globally lock on driver initialization
Browse files Browse the repository at this point in the history
This patch makes it such that plugin initialization is synchronized
based on the plugin name and not globally

Signed-off-by: Darren Shepherd <darren@rancher.com>
  • Loading branch information
ibuildthecloud committed Aug 19, 2015
1 parent 3dc30d4 commit 164208f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
41 changes: 28 additions & 13 deletions pkg/plugins/plugins.go
Expand Up @@ -63,6 +63,9 @@ type Plugin struct {
Client *Client `json:"-"`
// Manifest of the plugin (see above)
Manifest *Manifest `json:"-"`

activatErr error
activateOnce sync.Once
}

func newLocalPlugin(name, addr string) *Plugin {
Expand All @@ -74,6 +77,13 @@ func newLocalPlugin(name, addr string) *Plugin {
}

func (p *Plugin) activate() error {
p.activateOnce.Do(func() {
p.activatErr = p.activateWithLock()
})
return p.activatErr
}

func (p *Plugin) activateWithLock() error {
c, err := NewClient(p.Addr, p.TLSConfig)
if err != nil {
return err
Expand All @@ -99,32 +109,37 @@ func (p *Plugin) activate() error {
}

func load(name string) (*Plugin, error) {
storage.Lock()
registry := newLocalRegistry()
pl, err := registry.Plugin(name)
if err == nil {
storage.plugins[name] = pl
}
storage.Unlock()

if err != nil {
return nil, err
}
if err := pl.activate(); err != nil {
return nil, err

err = pl.activate()

if err != nil {
storage.Lock()
delete(storage.plugins, name)
storage.Unlock()
}
return pl, nil

return pl, err
}

func get(name string) (*Plugin, error) {
storage.Lock()
defer storage.Unlock()
pl, ok := storage.plugins[name]
storage.Unlock()
if ok {
return pl, nil
return pl, pl.activate()
}
pl, err := load(name)
if err != nil {
return nil, err
}

logrus.Debugf("Plugin: %v", pl)
storage.plugins[name] = pl
return pl, nil
return load(name)
}

// Get returns the plugin given the specified name and requested implementation.
Expand Down
8 changes: 7 additions & 1 deletion volume/drivers/extpoint.go
Expand Up @@ -51,8 +51,8 @@ func Unregister(name string) bool {
// there is a VolumeDriver plugin available with the given name.
func Lookup(name string) (volume.Driver, error) {
drivers.Lock()
defer drivers.Unlock()
ext, ok := drivers.extensions[name]
drivers.Unlock()
if ok {
return ext, nil
}
Expand All @@ -61,6 +61,12 @@ func Lookup(name string) (volume.Driver, error) {
return nil, fmt.Errorf("Error looking up volume plugin %s: %v", name, err)
}

drivers.Lock()
defer drivers.Unlock()
if ext, ok := drivers.extensions[name]; ok {
return ext, nil
}

d := NewVolumeDriver(name, pl.Client)
drivers.extensions[name] = d
return d, nil
Expand Down

0 comments on commit 164208f

Please sign in to comment.