fix(scanner): reuse a recent scanner health result

Each /health request ran the version command of every scanner. Under rescan load one round took about 1.4 seconds, which exceeds the client probes that use /health to choose the cache. The result is now reused for 30 seconds. A probe that ends with a cancelled context is not stored.
This commit is contained in:
2026-11-23 19:47:16 +00:00
parent 40ed8f8213
commit e6d8ed0f2e
2 changed files with 92 additions and 0 deletions
+24
View File
@@ -71,8 +71,18 @@ func (m *Manager) currentFileSource() FileSource {
return m.fileSource
}
// healthTTL bounds how often /health forks each scanner's version command; under scan load one round took over a second.
const healthTTL = 30 * time.Second
type healthCache struct {
at time.Time
err error
mu sync.Mutex
}
// Manager manages multiple security scanners
type Manager struct {
health healthCache
metadataStore metadata.MetadataStore
broadcaster events.Broadcaster
retryTimers map[string]*time.Timer
@@ -913,6 +923,20 @@ func (m *Manager) Health(ctx context.Context) error {
return nil
}
m.health.mu.Lock()
defer m.health.mu.Unlock()
if !m.health.at.IsZero() && time.Since(m.health.at) < healthTTL {
return m.health.err
}
err := m.checkScanners(ctx)
if ctx.Err() != nil {
return err
}
m.health.at, m.health.err = time.Now(), err
return err
}
func (m *Manager) checkScanners(ctx context.Context) error {
for _, scanner := range m.scanners {
if err := scanner.Health(ctx); err != nil {
return fmt.Errorf("scanner %s health check failed: %w", scanner.Name(), err)