From 63f947d78a726001e0b176599ffc503447b25d78 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Fri, 3 May 2024 22:34:18 +0300 Subject: [PATCH] chore(spanner): use time.Since instead of time.Now().Sub (#10096) time.Since has few optimizations to get monotonic time, also it's shorter and clearer. --- spanner/client_test.go | 8 ++++---- spanner/integration_test.go | 4 ++-- spanner/session.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/spanner/client_test.go b/spanner/client_test.go index 20d0951b65ea..2dea7476461f 100644 --- a/spanner/client_test.go +++ b/spanner/client_test.go @@ -1487,10 +1487,10 @@ func TestClient_ReadOnlyTransaction_WhenMultipleOperations_SessionLastUseTimeSho t.Fatalf("Session lastUseTime times should not be equal") } - if (time.Now().Sub(sessionPrevLastUseTime)).Milliseconds() < 400 { + if time.Since(sessionPrevLastUseTime).Milliseconds() < 400 { t.Fatalf("Expected session to be checkedout for more than 400 milliseconds") } - if (time.Now().Sub(sessionCheckoutTime)).Milliseconds() < 400 { + if time.Since(sessionCheckoutTime).Milliseconds() < 400 { t.Fatalf("Expected session to be checkedout for more than 400 milliseconds") } // force run task to clean up unexpected long-running sessions whose lastUseTime >= 3sec. @@ -2000,10 +2000,10 @@ func TestClient_ReadWriteTransaction_WhenMultipleOperations_SessionLastUseTimeSh t.Fatalf("Session lastUseTime times should not be equal") } - if (time.Now().Sub(sessionPrevLastUseTime)).Milliseconds() < 400 { + if time.Since(sessionPrevLastUseTime).Milliseconds() < 400 { t.Fatalf("Expected session to be checkedout for more than 400 milliseconds") } - if (time.Now().Sub(sessionCheckoutTime)).Milliseconds() < 400 { + if time.Since(sessionCheckoutTime).Milliseconds() < 400 { t.Fatalf("Expected session to be checkedout for more than 400 milliseconds") } // force run task to clean up unexpected long-running sessions whose lastUseTime >= 3sec. diff --git a/spanner/integration_test.go b/spanner/integration_test.go index 9063dfbf8f53..99388e7d8d34 100644 --- a/spanner/integration_test.go +++ b/spanner/integration_test.go @@ -586,7 +586,7 @@ func TestIntegration_SingleUse(t *testing.T) { } // Calculate time difference between Cloud Spanner server and localhost to // use to determine the exact staleness value to use. - timeDiff := maxDuration(time.Now().Sub(writes[0].ts), 0) + timeDiff := maxDuration(time.Since(writes[0].ts), 0) // Test reading rows with different timestamp bounds. for i, test := range []struct { @@ -649,7 +649,7 @@ func TestIntegration_SingleUse(t *testing.T) { skipForPG: true, want: nil, // Specify a staleness which should be already before this test. - tb: ExactStaleness(time.Now().Sub(writes[0].ts) + timeDiff + 30*time.Second), + tb: ExactStaleness(time.Since(writes[0].ts) + timeDiff + 30*time.Second), checkTs: func(ts time.Time) error { if !ts.Before(writes[0].ts) { return fmt.Errorf("read got timestamp %v, want it to be earlier than %v", ts, writes[0].ts) diff --git a/spanner/session.go b/spanner/session.go index a201b5ff51db..76d611fe262d 100644 --- a/spanner/session.go +++ b/spanner/session.go @@ -746,7 +746,7 @@ func (p *sessionPool) getLongRunningSessionsLocked() []*sessionHandle { element = element.Next() continue } - diff := time.Now().Sub(sh.lastUseTime) + diff := time.Since(sh.lastUseTime) if !sh.eligibleForLongRunning && diff.Seconds() >= p.idleTimeThreshold.Seconds() { if (p.ActionOnInactiveTransaction == Warn || p.ActionOnInactiveTransaction == WarnAndClose) && !sh.isSessionLeakLogged { if p.ActionOnInactiveTransaction == Warn {