Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
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.
74 changes: 44 additions & 30 deletions pkg/proxy/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Comment on lines +175 to +183

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

	var mu sync.Mutex
	var wg sync.WaitGroup
	sem := make(chan struct{}, 10) // Limit concurrent health checks

	// 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()
			sem <- struct{}{}
			defer func() { <-sem }()


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using defer cancel() is more idiomatic and robust than calling cancel() manually at the end of the block. It ensures that the context resources are released on all execution paths, including any future refactoring that might introduce early returns or panics, and maintains consistency with CollectAllMetadata.

Suggested change
Name: t.cfg.Name,
LastCheck: now,
}
checkCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
err := t.proxy.HealthCheck(checkCtx)


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
}

Expand Down
31 changes: 31 additions & 0 deletions pkg/proxy/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proxy

import (
"context"
"fmt"
"sort"
"sync"
"testing"
Expand Down Expand Up @@ -157,3 +158,33 @@ func TestRegistry_ConcurrentAccess(t *testing.T) {
wg.Wait()
// No race detector failures = pass
}

func TestRegistry_HealthReportConcurrent(t *testing.T) {
r := NewRegistry()

// Register 5 fake proxies with mock health checks
for i := 1; i <= 5; i++ {
id := fmt.Sprintf("ds-%d", i)
entry := DatasourceEntry{
ID: id,
Type: "postgresql",
ProxyType: "db-proxy",
Name: fmt.Sprintf("DB %d", i),
}
r.Register(id, entry, &fakeProxy{proxyType: "db-proxy"})
}

report := r.HealthReport(context.Background())
if len(report) != 5 {
t.Fatalf("expected 5 health report entries, got %d", len(report))
}

for id, h := range report {
if h.Status != "healthy" {
t.Errorf("expected status 'healthy' for %s, got %s", id, h.Status)
}
if h.ProxyType != "db-proxy" {
t.Errorf("expected proxy_type 'db-proxy' for %s, got %s", id, h.ProxyType)
}
}
}
Loading