internal/storage/timed: initialize w.latest
NewWatcher initializes w.latest from the DB,
so it will never be 0 for a Watcher that already
exists.
Hopefully this will address the problem that some
watcher metrics are zero.
Change-Id: I357896b6605342c7464a1bcd1a7b29b8b35ca39f
Reviewed-on: https://go-review.googlesource.com/c/oscar/+/617816
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
diff --git a/internal/storage/timed/timed.go b/internal/storage/timed/timed.go
index e736af7..80452f7 100644
--- a/internal/storage/timed/timed.go
+++ b/internal/storage/timed/timed.go
@@ -288,13 +288,16 @@
// The Watcher applies decode(e) to each time-stamped Entry to obtain the T returned
// in the iteration.
func NewWatcher[T any](lg *slog.Logger, db storage.DB, name, kind string, decode func(*Entry) T) *Watcher[T] {
- return &Watcher[T]{
+ w := &Watcher[T]{
slog: lg,
db: db,
dkey: ordered.Encode(kind+"Watcher", name),
kind: kind,
decode: decode,
}
+ // Set w.latest to current DB value.
+ w.cutoffUnlocked()
+ return w
}
func (w *Watcher[T]) lock() {
@@ -318,6 +321,10 @@
// unreachable unless called wrong in this file
w.db.Panic("timed.Watcher not locked")
}
+ return w.cutoffUnlocked()
+}
+
+func (w *Watcher[T]) cutoffUnlocked() DBTime {
var t int64
if dval, ok := w.db.Get(w.dkey); ok {
if err := ordered.Decode(dval, &t); err != nil {
@@ -408,10 +415,6 @@
// Latest returns the latest known DBTime marked old by the Watcher.
// It does not require the lock to be held.
-//
-// Latest may return a stale value. In particular, it will return
-// 0 until the first call to [Watcher.Recent] or [Watcher.MarkOld] in
-// the process.
func (w *Watcher[T]) Latest() DBTime {
return DBTime(w.latest.Load())
}