@@ -43,11 +43,6 @@ type DataSource interface {
43
43
// the data source output type upon registration.
44
44
AddExtractor (extractor Extractor ) error
45
45
46
- // TODO: the following is useful for a data source that operates on
47
- // endpoints and might not be relevant for "global/system" collectors which
48
- // might not need the concept of an endpoint. This can be split, if needed,
49
- // to a separate interface in the future.
50
-
51
46
// AddEndpoint adds an endpoint to collect from.
52
47
AddEndpoint (ep Endpoint ) error
53
48
76
71
// DataSourceRegistry stores named data sources and makes them
77
72
// accessible to GIE subsystems.
78
73
type DataSourceRegistry struct {
79
- mu sync.RWMutex
80
- sources map [string ]DataSource
74
+ sources sync.Map
81
75
}
82
76
83
77
// Register adds a source to the registry.
@@ -86,13 +80,10 @@ func (dsr *DataSourceRegistry) Register(src DataSource) error {
86
80
return errors .New ("unable to register a nil data source" )
87
81
}
88
82
89
- dsr .mu .Lock ()
90
- defer dsr .mu .Unlock ()
91
-
92
- if _ , found := dsr .sources [src .Name ()]; found {
83
+ if _ , found := dsr .sources .Load (src .Name ()); found {
93
84
return fmt .Errorf ("unable to register duplicate data source: %s" , src .Name ())
94
85
}
95
- dsr .sources [ src .Name ()] = src
86
+ dsr .sources . Store ( src .Name (), src )
96
87
return nil
97
88
}
98
89
@@ -102,10 +93,10 @@ func (dsr *DataSourceRegistry) GetNamedSource(name string) (DataSource, bool) {
102
93
return nil , false
103
94
}
104
95
105
- dsr .mu . RLock ()
106
- defer dsr . mu . RUnlock ()
107
- if ds , found := dsr . sources [ name ]; found {
108
- return ds , true
96
+ if val , found := dsr .sources . Load ( name ); found {
97
+ if ds , ok := val .( DataSource ); ok {
98
+ return ds , true
99
+ } // ignore type assertion failures and fall through
109
100
}
110
101
return nil , false
111
102
}
@@ -122,15 +113,15 @@ func (dsr *DataSourceRegistry) AddEndpoint(ep Endpoint) error {
122
113
return nil
123
114
}
124
115
125
- dsr .mu .RLock ()
126
- defer dsr .mu .RUnlock ()
127
116
errs := []error {}
128
-
129
- for _ , ds := range dsr .sources {
130
- if err := ds .AddEndpoint (ep ); err != nil {
131
- errs = append (errs , err )
117
+ dsr .sources .Range (func (_ , val interface {}) bool {
118
+ if ds , ok := val .(DataSource ); ok {
119
+ if err := ds .AddEndpoint (ep ); err != nil {
120
+ errs = append (errs , err )
121
+ }
132
122
}
133
- }
123
+ return true
124
+ })
134
125
return errors .Join (errs ... )
135
126
}
136
127
@@ -142,15 +133,15 @@ func (dsr *DataSourceRegistry) RemoveEndpoint(ep Endpoint) error {
142
133
return nil
143
134
}
144
135
145
- dsr .mu .RLock ()
146
- defer dsr .mu .RUnlock ()
147
136
errs := []error {}
148
-
149
- for _ , ds := range dsr .sources {
150
- if err := ds .RemoveEndpoint (ep ); err != nil {
151
- errs = append (errs , err )
137
+ dsr .sources .Range (func (_ , val interface {}) bool {
138
+ if ds , ok := val .(DataSource ); ok {
139
+ if err := ds .RemoveEndpoint (ep ); err != nil {
140
+ errs = append (errs , err )
141
+ }
152
142
}
153
- }
143
+ return true
144
+ })
154
145
return errors .Join (errs ... )
155
146
}
156
147
0 commit comments