From c366933416f138305ae3f9eddd9ebfece86f8fba Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Mon, 8 Nov 2021 15:12:57 +0100 Subject: [PATCH] lib/sync: Make the clock a function pointer --- lib/sync/sync.go | 26 ++++++++------------------ lib/sync/sync_test.go | 18 +++++++++--------- 2 files changed, 17 insertions(+), 27 deletions(-) diff --git a/lib/sync/sync.go b/lib/sync/sync.go index e1b401e16..9d538c037 100644 --- a/lib/sync/sync.go +++ b/lib/sync/sync.go @@ -19,11 +19,7 @@ import ( "github.com/sasha-s/go-deadlock" ) -type clock interface { - Now() time.Time -} - -var defaultClock clock = (*standardClock)(nil) +var timeNow = time.Now type Mutex interface { Lock() @@ -86,7 +82,7 @@ func (h holder) String() string { if h.at == "" { return "not held" } - return fmt.Sprintf("at %s goid: %d for %s", h.at, h.goid, defaultClock.Now().Sub(h.time)) + return fmt.Sprintf("at %s goid: %d for %s", h.at, h.goid, timeNow().Sub(h.time)) } type loggedMutex struct { @@ -101,7 +97,7 @@ func (m *loggedMutex) Lock() { func (m *loggedMutex) Unlock() { currentHolder := m.holder.Load().(holder) - duration := defaultClock.Now().Sub(currentHolder.time) + duration := timeNow().Sub(currentHolder.time) if duration >= threshold { l.Debugf("Mutex held for %v. Locked at %s unlocked at %s", duration, currentHolder.at, getHolder().at) } @@ -125,7 +121,7 @@ type loggedRWMutex struct { } func (m *loggedRWMutex) Lock() { - start := defaultClock.Now() + start := timeNow() atomic.StoreInt32(&m.logUnlockers, 1) m.RWMutex.Lock() @@ -153,7 +149,7 @@ func (m *loggedRWMutex) Lock() { func (m *loggedRWMutex) Unlock() { currentHolder := m.holder.Load().(holder) - duration := defaultClock.Now().Sub(currentHolder.time) + duration := timeNow().Sub(currentHolder.time) if duration >= threshold { l.Debugf("RWMutex held for %v. Locked at %s unlocked at %s", duration, currentHolder.at, getHolder().at) } @@ -205,9 +201,9 @@ type loggedWaitGroup struct { } func (wg *loggedWaitGroup) Wait() { - start := defaultClock.Now() + start := timeNow() wg.WaitGroup.Wait() - duration := defaultClock.Now().Sub(start) + duration := timeNow().Sub(start) if duration >= threshold { l.Debugf("WaitGroup took %v at %s", duration, getHolder()) } @@ -219,7 +215,7 @@ func getHolder() holder { return holder{ at: fmt.Sprintf("%s:%d", file, line), goid: goid(), - time: defaultClock.Now(), + time: timeNow(), } } @@ -300,9 +296,3 @@ func (w *TimeoutCondWaiter) Wait() bool { func (w *TimeoutCondWaiter) Stop() { w.timer.Stop() } - -type standardClock struct{} - -func (*standardClock) Now() time.Time { - return time.Now() -} diff --git a/lib/sync/sync_test.go b/lib/sync/sync_test.go index 4b109bdd0..54e11a641 100644 --- a/lib/sync/sync_test.go +++ b/lib/sync/sync_test.go @@ -57,10 +57,10 @@ func TestTypes(t *testing.T) { } func TestMutex(t *testing.T) { - oldClock := defaultClock + oldClock := timeNow clock := newTestClock() - defaultClock = clock - defer func() { defaultClock = oldClock }() + timeNow = clock.Now + defer func() { timeNow = oldClock }() debug = true l.SetDebug("sync", true) @@ -97,10 +97,10 @@ func TestMutex(t *testing.T) { } func TestRWMutex(t *testing.T) { - oldClock := defaultClock + oldClock := timeNow clock := newTestClock() - defaultClock = clock - defer func() { defaultClock = oldClock }() + timeNow = clock.Now + defer func() { timeNow = oldClock }() debug = true l.SetDebug("sync", true) @@ -170,10 +170,10 @@ func TestRWMutex(t *testing.T) { } func TestWaitGroup(t *testing.T) { - oldClock := defaultClock + oldClock := timeNow clock := newTestClock() - defaultClock = clock - defer func() { defaultClock = oldClock }() + timeNow = clock.Now + defer func() { timeNow = oldClock }() debug = true l.SetDebug("sync", true)