Data race trên DbUpdatedAt (cron ghi, /metrics đọc) #12

Closed
opened 2026-07-22 11:22:27 +07:00 by claudecode · 1 comment
Owner

Vấn đề

DbUpdatedAt time.Time (db_updater/main.go:11) được ghi bởi goroutine cron (fetchDbs) và đọc bởi HTTP handler /metrics (handler_metrics.go:11) mà không có đồng bộ → data race trên struct time.Time.

Fix đề xuất

Dùng atomic.Int64 lưu unix milli:

var DbUpdatedAt atomic.Int64
// ghi: DbUpdatedAt.Store(time.Now().UnixMilli())
// đọc: time.UnixMilli(DbUpdatedAt.Load())

Hoặc dùng mutex. Ưu tiên atomic cho đơn giản.

## Vấn đề `DbUpdatedAt time.Time` (`db_updater/main.go:11`) được **ghi bởi goroutine cron** (`fetchDbs`) và **đọc bởi HTTP handler** `/metrics` (`handler_metrics.go:11`) mà **không có đồng bộ** → data race trên struct `time.Time`. ## Fix đề xuất Dùng `atomic.Int64` lưu unix milli: ```go var DbUpdatedAt atomic.Int64 // ghi: DbUpdatedAt.Store(time.Now().UnixMilli()) // đọc: time.UnixMilli(DbUpdatedAt.Load()) ``` Hoặc dùng mutex. Ưu tiên `atomic` cho đơn giản.
claudecode added the bug label 2026-07-22 11:22:27 +07:00
Author
Owner

Đã sửa trong PR #22 (merge commit 361eb40).

  • internal/services/db_updater/main.go: DbUpdatedAt time.Timeatomic.Int64 (cron goroutine ghi, handler /metrics đọc); thêm DbUpdatedAtMillis() accessor + stampDbUpdatedAt().
  • internal/services/api/handler_metrics.go: gọi db_updater.DbUpdatedAtMillis() thay vì truy cập field trực tiếp.

Verify: go test -race PASS (race regression test trong internal/data/data_test.go).

Đã sửa trong PR #22 (merge commit `361eb40`). - `internal/services/db_updater/main.go`: `DbUpdatedAt time.Time` → `atomic.Int64` (cron goroutine ghi, handler `/metrics` đọc); thêm `DbUpdatedAtMillis()` accessor + `stampDbUpdatedAt()`. - `internal/services/api/handler_metrics.go`: gọi `db_updater.DbUpdatedAtMillis()` thay vì truy cập field trực tiếp. Verify: `go test -race` PASS (race regression test trong `internal/data/data_test.go`).
This repo is archived. You cannot comment on issues.
1 Participants
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: public/ip-info#12