core: perform synchronized persist if GC took some time

The intention here is to reduce the amount of in-flight changes and prevent
OOM. It doesn't matter what we're doing, persisting or collecting garbage,
what matters is that we're behind the schedule of regular persist cycle.

Refs. #3783.

Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2025-01-13 23:37:42 +03:00
parent 1f83f472c7
commit ba0ca6a4ab

View file

@ -1119,7 +1119,6 @@ func (bc *Blockchain) Run() {
return
case <-persistTimer.C:
var oldPersisted uint32
var gcDur time.Duration
if bc.config.Ledger.RemoveUntraceableBlocks {
oldPersisted = atomic.LoadUint32(&bc.persistedHeight)
@ -1129,10 +1128,10 @@ func (bc *Blockchain) Run() {
bc.log.Warn("failed to persist blockchain", zap.Error(err))
}
if bc.config.Ledger.RemoveUntraceableBlocks {
gcDur = bc.tryRunGC(oldPersisted)
dur += bc.tryRunGC(oldPersisted)
}
nextSync = dur > persistInterval*2
interval := persistInterval - dur - gcDur
interval := persistInterval - dur
interval = max(interval, time.Microsecond) // Reset doesn't work with zero or negative value.
persistTimer.Reset(interval)
}