mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 15:10:12 +01:00
1.4 KiB
1.4 KiB
Proposal: Clock interface for deterministic time
Summary
Introduce a Clock interface and replace package-level now = time.Now usages in targeted packages (e.g. pkg/registry/auth) with dependency injection via a Clock to allow deterministic tests and easier control over time in the future.
Motivation
- Currently some packages expose a package-level
nowvariable used by tests to override time. A dedicatedClockinterface reduces global state and improves testability across packages.
Proposal
- Define
type Clock interface { Now() time.Time }inpkg/clock/clock.goand provideRealClockimplementation that callstime.Now(). - Introduce constructors or package-level
SetClock(clock Clock)functions where needed (e.g.pkg/registry/auth) or prefer injectingClockvia function parameters in higher-level constructors.
Compatibility
- Backwards-compatible: default behavior uses
RealClockwhen no override is provided.
Migration
- Add
pkg/clock/clock.gocontainingClockandRealClock. - Update
pkg/registry/authto useclock.Now()through injection or a package-levelclockvariable initialized toRealClock{}. - Replace test overrides of
nowby using aFakeClockin tests.
Risks
- Small refactor; tests will need updates but should be straightforward.
References
- Current usage:
pkg/registry/auth/auth.gousesvar now = time.Now.