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:
@@ -0,0 +1,68 @@
|
||||
package scanner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
|
||||
)
|
||||
|
||||
type healthProbe struct {
|
||||
err error
|
||||
calls atomic.Int32
|
||||
}
|
||||
|
||||
func (s *healthProbe) Name() string { return "counting" }
|
||||
|
||||
func (s *healthProbe) Scan(context.Context, string, string, string, string) (*metadata.ScanResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *healthProbe) Health(context.Context) error {
|
||||
s.calls.Add(1)
|
||||
return s.err
|
||||
}
|
||||
|
||||
// Health runs each scanner's version command; under scan load that took over a second, so
|
||||
// repeated probes must reuse a recent result.
|
||||
func TestHealthReusesRecentResult(t *testing.T) {
|
||||
tests := []struct {
|
||||
err error
|
||||
name string
|
||||
}{
|
||||
{name: "healthy", err: nil},
|
||||
{name: "unhealthy", err: errors.New("grype missing")},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := &healthProbe{err: tt.err}
|
||||
m := &Manager{enabled: true, scanners: []Scanner{s}}
|
||||
for range 3 {
|
||||
if err := m.Health(context.Background()); (err != nil) != (tt.err != nil) {
|
||||
t.Fatalf("Health() error = %v, want error %v", err, tt.err != nil)
|
||||
}
|
||||
}
|
||||
if got := s.calls.Load(); got != 1 {
|
||||
t.Fatalf("scanner health ran %d times, want 1", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// A probe whose context was cancelled must not pin its failure for the cache period.
|
||||
func TestHealthDoesNotCacheCancelledProbe(t *testing.T) {
|
||||
s := &healthProbe{err: context.Canceled}
|
||||
m := &Manager{enabled: true, scanners: []Scanner{s}}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_ = m.Health(ctx)
|
||||
s.err = nil
|
||||
if err := m.Health(context.Background()); err != nil {
|
||||
t.Fatalf("Health() after cancelled probe = %v, want nil", err)
|
||||
}
|
||||
if got := s.calls.Load(); got != 2 {
|
||||
t.Fatalf("scanner health ran %d times, want 2", got)
|
||||
}
|
||||
}
|
||||
@@ -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