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
|
||||
}
|
||||
|
||||
type extpointHandlers struct {
|
||||
sync.RWMutex
|
||||
extpointHandlers map[string][]func(string, *Client)
|
||||
}
|
||||
|
||||
var (
|
||||
storage = plugins{plugins: make(map[string]*Plugin)}
|
||||
extpointHandlers = make(map[string][]func(string, *Client))
|
||||
storage = plugins{plugins: make(map[string]*Plugin)}
|
||||
handlers = extpointHandlers{extpointHandlers: make(map[string][]func(string, *Client))}
|
||||
)
|
||||
|
||||
// Manifest lists what a plugin implements.
|
||||
|
@ -128,15 +133,17 @@ func (p *Plugin) activateWithLock() error {
|
|||
|
||||
p.Manifest = m
|
||||
|
||||
handlers.RLock()
|
||||
for _, iface := range m.Implements {
|
||||
handlers, handled := extpointHandlers[iface]
|
||||
hdlrs, handled := handlers.extpointHandlers[iface]
|
||||
if !handled {
|
||||
continue
|
||||
}
|
||||
for _, handler := range handlers {
|
||||
for _, handler := range hdlrs {
|
||||
handler(p.name, p.client)
|
||||
}
|
||||
}
|
||||
handlers.RUnlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -228,16 +235,18 @@ func Get(name, imp string) (*Plugin, error) {
|
|||
|
||||
// Handle adds the specified function to the extpointHandlers.
|
||||
func Handle(iface string, fn func(string, *Client)) {
|
||||
handlers, ok := extpointHandlers[iface]
|
||||
handlers.Lock()
|
||||
hdlrs, ok := handlers.extpointHandlers[iface]
|
||||
if !ok {
|
||||
handlers = []func(string, *Client){}
|
||||
hdlrs = []func(string, *Client){}
|
||||
}
|
||||
|
||||
handlers = append(handlers, fn)
|
||||
extpointHandlers[iface] = handlers
|
||||
hdlrs = append(hdlrs, fn)
|
||||
handlers.extpointHandlers[iface] = hdlrs
|
||||
for _, p := range storage.plugins {
|
||||
p.activated = false
|
||||
}
|
||||
handlers.Unlock()
|
||||
}
|
||||
|
||||
// GetAll returns all the plugins for the specified implementation
|
||||
|
|
Loading…
Add table
Reference in a new issue