-
Notifications
You must be signed in to change notification settings - Fork 2
⚡ Bolt: Run datasource health checks concurrently in Registry.HealthReport #131
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
base: main
Are you sure you want to change the base?
Changes from all commits
c36fc62
e517e09
7ef91b3
2a5d73a
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2026-08-09 - Parallel Datasource Health Checks | ||
| **Learning:** In proxy registries managing multiple datasources (DB, SSH, HTTP, Kafka, etc.), sequential health checks cause total latency to scale linearly as O(N * timeout), blocking reporting threads when target endpoints time out. | ||
| **Action:** Always perform multi-datasource health probes and metadata collection concurrently using goroutines with per-check context timeouts. |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -149,50 +149,64 @@ type DatasourceHealth struct { | |||||||||||||
| LastCheck string `json:"last_check"` // RFC3339 | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // HealthReport runs health checks on all registered datasources and returns a map | ||||||||||||||
| // HealthReport runs health checks on all registered datasources concurrently and returns a map | ||||||||||||||
| // of datasource ID → health status. Used for periodic reporting to the cloud. | ||||||||||||||
| func (r *Registry) HealthReport(ctx context.Context) map[string]DatasourceHealth { | ||||||||||||||
| r.mu.RLock() | ||||||||||||||
| ids := make([]string, 0, len(r.proxies)) | ||||||||||||||
| for id := range r.proxies { | ||||||||||||||
| ids = append(ids, id) | ||||||||||||||
| type target struct { | ||||||||||||||
| id string | ||||||||||||||
| proxy Proxy | ||||||||||||||
| cfg DatasourceEntry | ||||||||||||||
| } | ||||||||||||||
| targets := make([]target, 0, len(r.proxies)) | ||||||||||||||
| for id, p := range r.proxies { | ||||||||||||||
| if cfg, ok := r.configs[id]; ok { | ||||||||||||||
| targets = append(targets, target{id: id, proxy: p, cfg: cfg}) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| r.mu.RUnlock() | ||||||||||||||
|
|
||||||||||||||
| report := make(map[string]DatasourceHealth, len(ids)) | ||||||||||||||
| now := time.Now().UTC().Format(time.RFC3339) | ||||||||||||||
| if len(targets) == 0 { | ||||||||||||||
| return make(map[string]DatasourceHealth) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| for _, id := range ids { | ||||||||||||||
| r.mu.RLock() | ||||||||||||||
| p, pOk := r.proxies[id] | ||||||||||||||
| cfg, cOk := r.configs[id] | ||||||||||||||
| r.mu.RUnlock() | ||||||||||||||
| report := make(map[string]DatasourceHealth, len(targets)) | ||||||||||||||
| now := time.Now().UTC().Format(time.RFC3339) | ||||||||||||||
| var mu sync.Mutex | ||||||||||||||
| var wg sync.WaitGroup | ||||||||||||||
|
|
||||||||||||||
| if !pOk || !cOk { | ||||||||||||||
| continue | ||||||||||||||
| } | ||||||||||||||
| // Run health checks concurrently to prevent a single slow or timing-out | ||||||||||||||
| // datasource from blocking the health status of other datasources. | ||||||||||||||
| for _, t := range targets { | ||||||||||||||
| wg.Add(1) | ||||||||||||||
| go func(t target) { | ||||||||||||||
| defer wg.Done() | ||||||||||||||
|
|
||||||||||||||
| health := DatasourceHealth{ | ||||||||||||||
| Type: cfg.Type, | ||||||||||||||
| ProxyType: cfg.ProxyType, | ||||||||||||||
| Name: cfg.Name, | ||||||||||||||
| LastCheck: now, | ||||||||||||||
| } | ||||||||||||||
| health := DatasourceHealth{ | ||||||||||||||
| Type: t.cfg.Type, | ||||||||||||||
| ProxyType: t.cfg.ProxyType, | ||||||||||||||
| Name: t.cfg.Name, | ||||||||||||||
| LastCheck: now, | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+188
to
+190
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| checkCtx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||||||||||||||
| err := p.HealthCheck(checkCtx) | ||||||||||||||
| cancel() | ||||||||||||||
| checkCtx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||||||||||||||
| defer cancel() | ||||||||||||||
| err := t.proxy.HealthCheck(checkCtx) | ||||||||||||||
|
|
||||||||||||||
| if err != nil { | ||||||||||||||
| health.Status = "error" | ||||||||||||||
| health.Error = err.Error() | ||||||||||||||
| } else { | ||||||||||||||
| health.Status = "healthy" | ||||||||||||||
| } | ||||||||||||||
| if err != nil { | ||||||||||||||
| health.Status = "error" | ||||||||||||||
| health.Error = err.Error() | ||||||||||||||
| } else { | ||||||||||||||
| health.Status = "healthy" | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| report[id] = health | ||||||||||||||
| mu.Lock() | ||||||||||||||
| report[t.id] = health | ||||||||||||||
| mu.Unlock() | ||||||||||||||
| }(t) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| wg.Wait() | ||||||||||||||
| return report | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
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.
Running health checks concurrently across all registered datasources without any limit can lead to resource exhaustion (such as exceeding the open file descriptor limit due to concurrent network connections, or overwhelming downstream services) if there are many datasources. Introducing a simple semaphore using a buffered channel limits the maximum concurrency while still allowing parallel execution.