-
Notifications
You must be signed in to change notification settings - Fork 0
Concurrency #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Concurrency #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "log/slog" | ||
| "pg-bash-exporter/internal/cache" | ||
| "pg-bash-exporter/internal/config" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
|
|
@@ -67,13 +68,34 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) { | |
| func (c *Collector) Collect(ch chan<- prometheus.Metric) { | ||
| c.logger.Info("Metrics collection started") | ||
|
|
||
| wg := sync.WaitGroup{} | ||
|
|
||
| maxConcurrent := c.config.Global.MaxConcurrent | ||
| if maxConcurrent <= 0 { | ||
| maxConcurrent = config.DefaultMaxConcurrent | ||
| } | ||
| smph := make(chan struct{}, maxConcurrent) | ||
|
|
||
| for _, metricConfig := range c.config.Metrics { | ||
| if len(metricConfig.SubMetrics) == 0 { | ||
| c.collectSimpleMetric(ch, metricConfig) | ||
| } else { | ||
| c.collectComplicatedMetric(ch, metricConfig) | ||
| } | ||
| wg.Add(1) | ||
|
|
||
| go func(mc config.Metric) { | ||
| defer wg.Done() | ||
|
|
||
| smph <- struct{}{} | ||
| defer func() { | ||
| <-smph | ||
| }() | ||
|
|
||
| if len(mc.SubMetrics) == 0 { | ||
| c.collectSimpleMetric(ch, mc) | ||
| } else { | ||
| c.collectComplicatedMetric(ch, mc) | ||
| } | ||
| }(metricConfig) | ||
|
Comment on lines
+82
to
+95
|
||
| } | ||
|
|
||
| wg.Wait() | ||
|
|
||
| c.logger.Info("Metrics collection finished") | ||
|
Comment on lines
+90
to
100
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Goroutines that panic will not be recovered and could cause issues. Consider adding panic recovery with defer recover() to ensure the WaitGroup is properly decremented and semaphore is released even if collectSimpleMetric or collectComplicatedMetric panic.