Fix data races, robustness & cleanup (#10-#21) #22

Merged
thuanle merged 3 commits from fix/review-10-21 into main 2026-07-22 17:23:08 +07:00
Showing only changes of commit b9ebe6f3af - Show all commits
+15 -6
View File
@@ -44,9 +44,18 @@ func fetchDbs() {
// restart (which then fails to open the empty file). Back the file up // restart (which then fails to open the empty file). Back the file up
// first so we can restore it on failure. // first so we can restore it on failure.
backup, backupErr := backupMmdb() backup, backupErr := backupMmdb()
if backupErr != nil {
// We could not back up the current canonical mmdb. Do NOT proceed:
// mergeMmdb() truncates the canonical file before writing, and with
// no backup a later merge/reload failure would leave it corrupt
// (unrecoverable on restart). Keep serving the current DB and let
// the next run retry.
log.Err(backupErr).Msg("Failed to back up mmdb, skipping rebuild")
return
}
if err := mergeMmdb(); err != nil { if err := mergeMmdb(); err != nil {
log.Err(err).Msg("Failed to merge mmdb") log.Err(err).Msg("Failed to merge mmdb")
rollBackMmdb(backup, backupErr) rollBackMmdb(backup)
return return
} }
@@ -55,7 +64,7 @@ func fetchDbs() {
log.Err(err).Msg("Failed to reload mmdb") log.Err(err).Msg("Failed to reload mmdb")
// Reload failed on the newly merged file: restore the previous // Reload failed on the newly merged file: restore the previous
// canonical mmdb so we keep serving known-good data. // canonical mmdb so we keep serving known-good data.
rollBackMmdb(backup, backupErr) rollBackMmdb(backup)
return return
} }
@@ -103,10 +112,10 @@ func backupMmdb() (string, error) {
// rollBackMmdb restores the canonical mmdb from backup (if any) and deletes // rollBackMmdb restores the canonical mmdb from backup (if any) and deletes
// the etag files so the next cron run re-downloads and retries the merge. // the etag files so the next cron run re-downloads and retries the merge.
func rollBackMmdb(backup string, backupErr error) { // backup is "" on a first-ever build (no canonical file existed to back up),
if backupErr != nil { // in which case there is nothing to restore.
log.Err(backupErr).Msg("No mmdb backup available; cannot restore") func rollBackMmdb(backup string) {
} else if backup != "" { if backup != "" {
if err := osx.Copy(backup, configs.MmdbDbFile); err != nil { if err := osx.Copy(backup, configs.MmdbDbFile); err != nil {
log.Err(err).Msg("Failed to restore mmdb backup") log.Err(err).Msg("Failed to restore mmdb backup")
} else { } else {