mirror of
https://github.com/lukaszraczylo/gohoarder.git
synced 2026-12-06 07:34:00 +00:00
Each trivy scan updated the vulnerability database when the database was stale. Six concurrent rescans then downloaded it at the same time. A download took about 11 seconds alone and 38 seconds under contention. Five of six trivy processes crashed with SIGBUS, because they read a database that another process replaced. The other scans hit their timeout. Scans now run with --skip-db-update. A refresh loop updates the database every 6 hours (trivy.db_update_interval). Scans hold a read lock and an update holds the write lock, so an update never replaces the database under a running scan. A scan that finds no database downloads it once before it scans. The grype refresh loop moves to the dbrefresh package, which both scanners use.
46 lines
1013 B
Go
46 lines
1013 B
Go
// Package dbrefresh runs a scanner's vulnerability database update on a fixed interval.
|
|
package dbrefresh
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// DefaultInterval applies when the configured interval is 0.
|
|
const DefaultInterval = 6 * time.Hour
|
|
|
|
// Run calls update every interval until stop closes. immediate runs the first update at once;
|
|
// otherwise it waits one interval.
|
|
func Run(stop <-chan struct{}, interval time.Duration, immediate bool, scanner string, update func(context.Context) error) {
|
|
if interval <= 0 {
|
|
interval = DefaultInterval
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
go func() {
|
|
<-stop
|
|
cancel()
|
|
}()
|
|
|
|
once := func() {
|
|
if err := update(ctx); err != nil {
|
|
log.Error().Err(err).Str("scanner", scanner).Msg("Scheduled database update failed")
|
|
}
|
|
}
|
|
if immediate {
|
|
once()
|
|
}
|
|
ticker := time.NewTicker(interval)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
once()
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|