mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-11-26 06:37:07 +00:00
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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user