mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
prevent data race in pkg/plugins
Signed-off-by: Victor Vieux <vieux@docker.com>
This commit is contained in:
parent
392b816199
commit
f7af80860c
1 changed files with 17 additions and 8 deletions
|
@ -41,9 +41,14 @@ type plugins struct {
|
||||||
plugins map[string]*Plugin
|
plugins map[string]*Plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type extpointHandlers struct {
|
||||||
|
sync.RWMutex
|
||||||
|
extpointHandlers map[string][]func(string, *Client)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
storage = plugins{plugins: make(map[string]*Plugin)}
|
storage = plugins{plugins: make(map[string]*Plugin)}
|
||||||
extpointHandlers = make(map[string][]func(string, *Client))
|
handlers = extpointHandlers{extpointHandlers: make(map[string][]func(string, *Client))}
|
||||||
)
|
)
|
||||||
|
|
||||||
// Manifest lists what a plugin implements.
|
// Manifest lists what a plugin implements.
|
||||||
|
@ -128,15 +133,17 @@ func (p *Plugin) activateWithLock() error {
|
||||||
|
|
||||||
p.Manifest = m
|
p.Manifest = m
|
||||||
|
|
||||||
|
handlers.RLock()
|
||||||
for _, iface := range m.Implements {
|
for _, iface := range m.Implements {
|
||||||
handlers, handled := extpointHandlers[iface]
|
hdlrs, handled := handlers.extpointHandlers[iface]
|
||||||
if !handled {
|
if !handled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, handler := range handlers {
|
for _, handler := range hdlrs {
|
||||||
handler(p.name, p.client)
|
handler(p.name, p.client)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
handlers.RUnlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -228,16 +235,18 @@ func Get(name, imp string) (*Plugin, error) {
|
||||||
|
|
||||||
// Handle adds the specified function to the extpointHandlers.
|
// Handle adds the specified function to the extpointHandlers.
|
||||||
func Handle(iface string, fn func(string, *Client)) {
|
func Handle(iface string, fn func(string, *Client)) {
|
||||||
handlers, ok := extpointHandlers[iface]
|
handlers.Lock()
|
||||||
|
hdlrs, ok := handlers.extpointHandlers[iface]
|
||||||
if !ok {
|
if !ok {
|
||||||
handlers = []func(string, *Client){}
|
hdlrs = []func(string, *Client){}
|
||||||
}
|
}
|
||||||
|
|
||||||
handlers = append(handlers, fn)
|
hdlrs = append(hdlrs, fn)
|
||||||
extpointHandlers[iface] = handlers
|
handlers.extpointHandlers[iface] = hdlrs
|
||||||
for _, p := range storage.plugins {
|
for _, p := range storage.plugins {
|
||||||
p.activated = false
|
p.activated = false
|
||||||
}
|
}
|
||||||
|
handlers.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAll returns all the plugins for the specified implementation
|
// GetAll returns all the plugins for the specified implementation
|
||||||
|
|
Loading…
Add table
Reference in a new issue