fix: ограничить пул Postgres и реже писать прогресс скана
- DB_MAX_OPEN_CONNS (25), DB_MAX_IDLE_CONNS (10), DB_CONN_MAX_LIFETIME_SEC (1800) - Throttle UpdateScan прогресса в Runner (500ms), меньше одновременных запросов к БД Устраняет FATAL too many clients при больших сканах и частом опросе GetScan Made-with: Cursor
This commit is contained in:
@@ -163,11 +163,28 @@ func (r *Runner) run(ctx context.Context, job ScanJob) {
|
||||
workCh := make(chan string)
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
pushProgress := func() {
|
||||
// Каждый UpdateScan в Postgres тянет GetScan+UPDATE; при сотнях воркеров без паузы исчерпывается пул соединений к БД.
|
||||
const minProgressWriteInterval = 500 * time.Millisecond
|
||||
var progWriteMu sync.Mutex
|
||||
var lastProgressWrite time.Time
|
||||
|
||||
pushProgress := func(force bool) {
|
||||
mu.Lock()
|
||||
q, f, u := quickDone, fullDone, up
|
||||
mu.Unlock()
|
||||
prog := scanProgressBlend(q, f, total)
|
||||
|
||||
if !force {
|
||||
progWriteMu.Lock()
|
||||
now := time.Now()
|
||||
if !lastProgressWrite.IsZero() && now.Sub(lastProgressWrite) < minProgressWriteInterval {
|
||||
progWriteMu.Unlock()
|
||||
return
|
||||
}
|
||||
lastProgressWrite = now
|
||||
progWriteMu.Unlock()
|
||||
}
|
||||
|
||||
_, _ = r.store.UpdateScan(job.ID, ScanUpdate{
|
||||
Status: "running",
|
||||
Progress: prog,
|
||||
@@ -207,7 +224,7 @@ func (r *Runner) run(ctx context.Context, job ScanJob) {
|
||||
}
|
||||
quickDone++
|
||||
mu.Unlock()
|
||||
pushProgress()
|
||||
pushProgress(false)
|
||||
|
||||
if isUp && r.cfg.SNMPEnabled {
|
||||
snmpRes, lldpRes, ifRes, portDevRes := probeSNMPAndLLDP(ip, r.cfg.SNMPCommunity, checkedAt, job.Options.PingTimeoutMS)
|
||||
@@ -232,7 +249,7 @@ func (r *Runner) run(ctx context.Context, job ScanJob) {
|
||||
mu.Lock()
|
||||
fullDone++
|
||||
mu.Unlock()
|
||||
pushProgress()
|
||||
pushProgress(false)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user