Skip to content

Commit e89e0b1

Browse files
committed
CR 6 - remove ctx.Done from go routines and simplify the main function
1 parent c5d69e2 commit e89e0b1

File tree

1 file changed

+12
-39
lines changed

1 file changed

+12
-39
lines changed

pkg/autoscaler/metricsclient/prometheus.go

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -115,26 +115,7 @@ func (pc *PrometheusMetricsClient) GetResourceMetrics(resources []scalertypes.Re
115115
defer cancel()
116116
metricToWindowSizes := pc.buildMetricLookup(resources)
117117

118-
// run getResourceMetrics in a goroutine so we can race it against the context timeout.
119-
// the select below returns whichever completes first: the metrics result or the timeout.
120-
type result struct {
121-
metrics map[string]map[string]int
122-
err error
123-
}
124-
resultCh := make(chan result, 1)
125-
defer close(resultCh)
126-
127-
go func() {
128-
metrics, err := pc.getResourceMetrics(ctx, metricToWindowSizes)
129-
resultCh <- result{metrics: metrics, err: err}
130-
}()
131-
132-
select {
133-
case <-ctx.Done():
134-
return nil, errors.Wrap(ctx.Err(), "timeout waiting for resource metrics")
135-
case res := <-resultCh:
136-
return res.metrics, res.err
137-
}
118+
return pc.getResourceMetrics(ctx, metricToWindowSizes)
138119
}
139120

140121
func (pc *PrometheusMetricsClient) getResourceMetrics(ctx context.Context, metricToWindowSizes metricLookup) (map[string]map[string]int, error) {
@@ -146,6 +127,7 @@ func (pc *PrometheusMetricsClient) getResourceMetrics(ctx context.Context, metri
146127
for metricName, queryTemplate := range pc.queryTemplates {
147128
windowSizeToResources := metricToWindowSizes[metricName]
148129

130+
// maximum goroutines is limited to the number of unique windowSize values across all metrics (5 by default).
149131
for windowSize, resourcesInWindowSize := range windowSizeToResources {
150132
wg.Add(1)
151133
go func(resourcesInWindowSize map[string]struct{}, metricName, windowSize string, resultChan chan<- *metricResult) {
@@ -230,28 +212,19 @@ func (pc *PrometheusMetricsClient) getResourceMetrics(ctx context.Context, metri
230212
// Collect results
231213
go func(resultChan chan *metricResult) {
232214
defer close(collectorDone)
233-
for {
234-
select {
235-
case result, ok := <-resultChan:
236-
if !ok {
237-
return
238-
}
239-
if _, exists := metricsByResource[result.resourceName]; !exists {
240-
metricsByResource[result.resourceName] = make(map[string]int)
241-
}
242-
if existingValue, exists := metricsByResource[result.resourceName][result.fullMetricName]; exists {
243-
if existingValue == result.metricValue {
244-
continue
245-
}
246-
collectorErr = errors.Errorf("conflicting metric values for resource: resourceName=%s, metricName=%s, existingValue=%d, newValue=%d",
247-
result.resourceName, result.fullMetricName, existingValue, result.metricValue)
248-
return
215+
for result := range resultChan {
216+
if _, exists := metricsByResource[result.resourceName]; !exists {
217+
metricsByResource[result.resourceName] = make(map[string]int)
218+
}
219+
if existingValue, exists := metricsByResource[result.resourceName][result.fullMetricName]; exists {
220+
if existingValue == result.metricValue {
221+
continue
249222
}
250-
metricsByResource[result.resourceName][result.fullMetricName] = result.metricValue
251-
252-
case <-ctx.Done():
223+
collectorErr = errors.Errorf("conflicting metric values for resource: resourceName=%s, metricName=%s, existingValue=%d, newValue=%d",
224+
result.resourceName, result.fullMetricName, existingValue, result.metricValue)
253225
return
254226
}
227+
metricsByResource[result.resourceName][result.fullMetricName] = result.metricValue
255228
}
256229
}(resultChan)
257230

0 commit comments

Comments
 (0)