From 1619df2d5e02360da2522775c662861d8f3decd3 Mon Sep 17 00:00:00 2001 From: RamiBerm Date: Thu, 15 Jul 2021 09:54:27 +0300 Subject: [PATCH] Update size_enforcer.go --- api/pkg/database/size_enforcer.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/api/pkg/database/size_enforcer.go b/api/pkg/database/size_enforcer.go index 08914f0e2..a1e98e4d3 100644 --- a/api/pkg/database/size_enforcer.go +++ b/api/pkg/database/size_enforcer.go @@ -81,9 +81,6 @@ func checkFileSize(maxSizeBytes int64) { } func pruneOldEntries(currentFileSize int64) { - IsDBLocked = true - defer func() {IsDBLocked = false}() - amountOfBytesToTrim := currentFileSize / (100 / percentageOfMaxSizeBytesToPrune) rows, err := GetEntriesTable().Limit(10000).Order("id").Rows() @@ -110,6 +107,10 @@ func pruneOldEntries(currentFileSize int64) { } if len(entryIdsToRemove) > 0 { + // sqlite locks the database while delete or VACUUM are running and sqlite is terrible at handling its own db lock while a lot of inserts are attempted, we prevent a significant bottleneck by handling the db lock ourselves here + IsDBLocked = true + defer func() {IsDBLocked = false}() + GetEntriesTable().Where(entryIdsToRemove).Delete(models.MizuEntry{}) // VACUUM causes sqlite to shrink the db file after rows have been deleted, the db file will not shrink without this DB.Exec("VACUUM")