@@ -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
}
@@ -69,6 +76,8 @@ func (dm *DiscoveryManager) Add(disc *discovery.PluggableDiscovery) error {
69
76
// from the discoveries managed by this DiscoveryManager
70
77
func (dm * DiscoveryManager ) remove (id string ) {
71
78
dm .discoveries [id ].Quit ()
79
+ dm .discoveriesMutex .Lock ()
80
+ defer dm .discoveriesMutex .Unlock ()
72
81
delete (dm .discoveries , id )
73
82
logrus .Infof ("Closed and removed discovery %s" , id )
74
83
}
@@ -78,7 +87,13 @@ func (dm *DiscoveryManager) remove(id string) {
78
87
func (dm * DiscoveryManager ) parallelize (f func (d * discovery.PluggableDiscovery ) error ) []error {
79
88
var wg sync.WaitGroup
80
89
errChan := make (chan error )
90
+ dm .discoveriesMutex .Lock ()
91
+ discoveries := []* discovery.PluggableDiscovery {}
81
92
for _ , d := range dm .discoveries {
93
+ discoveries = append (discoveries , d )
94
+ }
95
+ dm .discoveriesMutex .Unlock ()
96
+ for _ , d := range discoveries {
82
97
wg .Add (1 )
83
98
go func (d * discovery.PluggableDiscovery ) {
84
99
defer wg .Done ()
@@ -215,7 +230,13 @@ func (dm *DiscoveryManager) List() ([]*discovery.Port, []error) {
215
230
Port * discovery.Port
216
231
}
217
232
msgChan := make (chan listMsg )
233
+ dm .discoveriesMutex .Lock ()
234
+ discoveries := []* discovery.PluggableDiscovery {}
218
235
for _ , d := range dm .discoveries {
236
+ discoveries = append (discoveries , d )
237
+ }
238
+ dm .discoveriesMutex .Unlock ()
239
+ for _ , d := range discoveries {
219
240
wg .Add (1 )
220
241
go func (d * discovery.PluggableDiscovery ) {
221
242
defer wg .Done ()
@@ -254,7 +275,13 @@ func (dm *DiscoveryManager) List() ([]*discovery.Port, []error) {
254
275
// ListCachedPorts return the current list of ports detected from all discoveries
255
276
func (dm * DiscoveryManager ) ListCachedPorts () []* discovery.Port {
256
277
res := []* discovery.Port {}
278
+ dm .discoveriesMutex .Lock ()
279
+ discoveries := []* discovery.PluggableDiscovery {}
257
280
for _ , d := range dm .discoveries {
281
+ discoveries = append (discoveries , d )
282
+ }
283
+ dm .discoveriesMutex .Unlock ()
284
+ for _ , d := range discoveries {
258
285
if d .State () != discovery .Syncing {
259
286
// Discovery is not syncing
260
287
continue
0 commit comments