Skip to content

Commit ed0ee30

Browse files
committed
replace stateless random sampling with a deletion tracker
1 parent 67c528f commit ed0ee30

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

core/logic.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"html/template"
99
"io"
1010
"log"
11-
"math/rand"
1211
"net/http"
1312
"os"
1413
"strconv"
@@ -429,6 +428,7 @@ func DeleteAccountHandler(w http.ResponseWriter, r *http.Request) {
429428
}
430429

431430
// Delete invalid pastes
431+
var vacuumCounter int // Track no of deletions to trigger VACUUM
432432
func DeleteExpiredPastes() {
433433
db.DB.Where("expiration IS NOT NULL AND expiration < ?", time.Now()).Delete(&models.Paste{})
434434

@@ -440,8 +440,12 @@ func DeleteExpiredPastes() {
440440
}
441441
threshold := time.Now().AddDate(0, 0, -retention)
442442
result := db.DB.Unscoped().Where("deleted_at IS NOT NULL AND deleted_at < ?", threshold).Delete(&models.Paste{})
443-
if result.RowsAffected > 0 && rand.Intn(5) == 0 { // random sampling 20% chance
444-
db.DB.Exec("VACUUM;")
443+
if result.RowsAffected > 0 {
444+
vacuumCounter++
445+
if vacuumCounter >= 5 { // every 5 deletions
446+
db.DB.Exec("VACUUM;")
447+
vacuumCounter = 0
448+
}
445449
}
446450
}()
447451
}

0 commit comments

Comments
 (0)