summaryrefslogtreecommitdiff
path: root/internal/stats/stats.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/stats/stats.go')
-rw-r--r--internal/stats/stats.go12
1 files changed, 4 insertions, 8 deletions
diff --git a/internal/stats/stats.go b/internal/stats/stats.go
index 65d1b2d..16327f6 100644
--- a/internal/stats/stats.go
+++ b/internal/stats/stats.go
@@ -31,16 +31,12 @@ var windowSeconds int64 = int64(defaultWindow.Seconds())
// engine carries the injectable dependencies for stats operations. The only
// dependency today is the clock source for event timestamps and pruning
-// cutoffs. Production code uses defaultEngine (backed by time.Now); tests
-// construct an engine with a fake clock to control time without sleeping.
+// cutoffs. Production wrappers construct it with time.Now; tests can use a fake
+// clock to control time without sleeping.
type engine struct {
now func() time.Time
}
-// defaultEngine is the production engine used by the package-level Update and
-// TakeSnapshot wrappers. It reads the real wall clock.
-var defaultEngine = engine{now: time.Now}
-
// SetWindow sets the sliding window used for pruning and aggregation.
func SetWindow(d time.Duration) {
if d < time.Second {
@@ -115,7 +111,7 @@ func (s Snapshot) ScopeRPM(provider, model string) float64 {
// Update appends one event and prunes old entries under lock, using the
// production clock. It delegates to engine.update.
func Update(ctx context.Context, provider, model string, sentBytes, recvBytes int) error {
- return defaultEngine.update(ctx, provider, model, sentBytes, recvBytes)
+ return engine{now: time.Now}.update(ctx, provider, model, sentBytes, recvBytes)
}
// update appends one event and prunes old entries under lock.
@@ -232,7 +228,7 @@ func writeStatsFileAtomic(dir, path string, sf *File) error {
// TakeSnapshot reads the stats file and aggregates events within the stored
// window, using the production clock. It delegates to engine.takeSnapshot.
func TakeSnapshot() (Snapshot, error) {
- return defaultEngine.takeSnapshot()
+ return engine{now: time.Now}.takeSnapshot()
}
// takeSnapshot reads the stats file and aggregates events within the stored