Fix data races, robustness, and cleanup across codebase (#10-#21)
Concurrency & correctness: - #10: guard IpDb Query/IsLoaded with RLock so Reload() (which reassigns d.r and closes the old reader) cannot race with concurrent lookups. Confirmed via -race regression test. - #12: replace DbUpdatedAt time.Time with atomic.Int64 (cron writes, /metrics reads) to fix the read/write data race. - #11: graceful HTTP shutdown (srv.Shutdown with 10s timeout) instead of srv.Close() aborting in-flight requests. - #14: stop swallowing mergeMmdb() errors in fetchDbs() — keep the previous DB when a merge fails instead of reloading a possibly-empty output. - #13: add resty timeout (2m) + retry (x2) to download() so a hung CDN can't stall the daily cron forever. - #15: correct HTTP status codes (503 db loading, 404 not found via new ErrNotFound sentinel, 500 otherwise) instead of 200 on query error. Robustness: - #16: surface osx.Copy dstFile.Close() errors (flush may fail) via named return + defer. Dependency migration: - #17: migrate maxminddb-golang v1 -> v2. v2 is a breaking API (LookupNetwork -> Lookup returning Result, netip.Addr), so Query was rewritten; v1 dropped from go.mod. Tests: - #21: add internal/data unit tests (valid/invalid/not-found lookup) plus a concurrent Query/Reload race regression test. chdir to repo root in TestMain because data helpers use relative paths. Cleanup: - #18: README Go version 1.22 -> 1.25. - #19: replace stray fmt.Printf with zerolog in ipdb.go. - #20: .env.example API_PORT 28080 -> 8080 (container port, matches docker-compose 28080:8080 mapping) with an explanatory comment. Closes #10, #11, #12, #13, #14, #15, #16, #17, #18, #19, #20, #21 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3,12 +3,22 @@ package db_updater
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/rs/zerolog/log"
|
||||
"sync/atomic"
|
||||
"thuanle.me/ip-info/configs"
|
||||
"thuanle.me/ip-info/internal/data"
|
||||
"time"
|
||||
)
|
||||
|
||||
var DbUpdatedAt time.Time
|
||||
// DbUpdatedAt stores the DB last-updated time as unix milliseconds.
|
||||
// Accessed concurrently by the cron goroutine (writer) and the /metrics
|
||||
// handler (reader), so it must be synchronized — hence atomic.Int64.
|
||||
var DbUpdatedAt atomic.Int64
|
||||
|
||||
// DbUpdatedAtMillis returns the last DB update time in unix milliseconds,
|
||||
// or 0 if the DB has never been updated.
|
||||
func DbUpdatedAtMillis() int64 {
|
||||
return DbUpdatedAt.Load()
|
||||
}
|
||||
|
||||
func StartUpdateDbService() {
|
||||
c := cron.New()
|
||||
@@ -25,7 +35,12 @@ func fetchDbs() {
|
||||
|
||||
if newFlag {
|
||||
log.Info().Msg("New DB downloaded. Recreating mmdb")
|
||||
_ = mergeMmdb()
|
||||
if err := mergeMmdb(); err != nil {
|
||||
// Keep the current (previous) DB rather than reloading a
|
||||
// potentially empty/corrupt merge output.
|
||||
log.Err(err).Msg("Failed to merge mmdb, keeping current DB")
|
||||
return
|
||||
}
|
||||
|
||||
err := data.Ins().Reload()
|
||||
if err != nil {
|
||||
@@ -33,6 +48,6 @@ func fetchDbs() {
|
||||
return
|
||||
}
|
||||
|
||||
DbUpdatedAt = time.Now()
|
||||
DbUpdatedAt.Store(time.Now().UnixMilli())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user