@@ -28,7 +28,8 @@ import (
28
28
// DiscoveryManager is required to handle multiple pluggable-discovery that
29
29
// may be shared across platforms
30
30
type DiscoveryManager struct {
31
- discoveries map [string ]* discovery.PluggableDiscovery
31
+ discoveriesMutex sync.Mutex
32
+ discoveries map [string ]* discovery.PluggableDiscovery
32
33
}
33
34
34
35
var tr = i18n .Tr
@@ -43,12 +44,16 @@ func New() *DiscoveryManager {
43
44
// Clear resets the DiscoveryManager to its initial state
44
45
func (dm * DiscoveryManager ) Clear () {
45
46
dm .QuitAll ()
47
+ dm .discoveriesMutex .Lock ()
48
+ defer dm .discoveriesMutex .Unlock ()
46
49
dm .discoveries = map [string ]* discovery.PluggableDiscovery {}
47
50
}
48
51
49
52
// IDs returns the list of discoveries' ids in this DiscoveryManager
50
53
func (dm * DiscoveryManager ) IDs () []string {
51
54
ids := []string {}
55
+ dm .discoveriesMutex .Lock ()
56
+ defer dm .discoveriesMutex .Unlock ()
52
57
for id := range dm .discoveries {
53
58
ids = append (ids , id )
54
59
}
@@ -58,6 +63,8 @@ func (dm *DiscoveryManager) IDs() []string {
58
63
// Add adds a discovery to the list of managed discoveries
59
64
func (dm * DiscoveryManager ) Add (disc * discovery.PluggableDiscovery ) error {
60
65
id := disc .GetID ()
66
+ dm .discoveriesMutex .Lock ()
67
+ defer dm .discoveriesMutex .Unlock ()
61
68
if _ , has := dm .discoveries [id ]; has {
62
69
return errors .Errorf (tr ("pluggable discovery already added: %s" ), id )
63
70
}
@@ -78,7 +85,13 @@ func (dm *DiscoveryManager) remove(id string) {
78
85
func (dm * DiscoveryManager ) parallelize (f func (d * discovery.PluggableDiscovery ) error ) []error {
79
86
var wg sync.WaitGroup
80
87
errChan := make (chan error )
88
+ dm .discoveriesMutex .Lock ()
89
+ discoveries := []* discovery.PluggableDiscovery {}
81
90
for _ , d := range dm .discoveries {
91
+ discoveries = append (discoveries , d )
92
+ }
93
+ dm .discoveriesMutex .Unlock ()
94
+ for _ , d := range discoveries {
82
95
wg .Add (1 )
83
96
go func (d * discovery.PluggableDiscovery ) {
84
97
defer wg .Done ()
@@ -215,7 +228,13 @@ func (dm *DiscoveryManager) List() ([]*discovery.Port, []error) {
215
228
Port * discovery.Port
216
229
}
217
230
msgChan := make (chan listMsg )
231
+ dm .discoveriesMutex .Lock ()
232
+ discoveries := []* discovery.PluggableDiscovery {}
218
233
for _ , d := range dm .discoveries {
234
+ discoveries = append (discoveries , d )
235
+ }
236
+ dm .discoveriesMutex .Unlock ()
237
+ for _ , d := range discoveries {
219
238
wg .Add (1 )
220
239
go func (d * discovery.PluggableDiscovery ) {
221
240
defer wg .Done ()
@@ -254,7 +273,13 @@ func (dm *DiscoveryManager) List() ([]*discovery.Port, []error) {
254
273
// ListCachedPorts return the current list of ports detected from all discoveries
255
274
func (dm * DiscoveryManager ) ListCachedPorts () []* discovery.Port {
256
275
res := []* discovery.Port {}
276
+ dm .discoveriesMutex .Lock ()
277
+ discoveries := []* discovery.PluggableDiscovery {}
257
278
for _ , d := range dm .discoveries {
279
+ discoveries = append (discoveries , d )
280
+ }
281
+ dm .discoveriesMutex .Unlock ()
282
+ for _ , d := range discoveries {
258
283
if d .State () != discovery .Syncing {
259
284
// Discovery is not syncing
260
285
continue
0 commit comments