From bf81718eba312d46008f058b5579d7d59d7c79b1 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Tue, 18 Aug 2015 22:04:22 -0700 Subject: [PATCH] Don't globally lock on driver initialization This patch makes it such that plugin initialization is synchronized based on the plugin name and not globally Signed-off-by: Darren Shepherd --- plugins/plugins.go | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/plugins/plugins.go b/plugins/plugins.go index 87a8eba..f369053 100644 --- a/plugins/plugins.go +++ b/plugins/plugins.go @@ -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 { @@ -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 @@ -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.