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.
diff --git a/spanner/client_test.go b/spanner/client_test.go
index 20d0951..2dea747 100644
--- a/spanner/client_test.go
+++ b/spanner/client_test.go
@@ -1487,10 +1487,10 @@
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 @@
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 9063dfb..99388e7 100644
--- a/spanner/integration_test.go
+++ b/spanner/integration_test.go
@@ -586,7 +586,7 @@
}
// 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 @@
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 a201b5f..76d611f 100644
--- a/spanner/session.go
+++ b/spanner/session.go
@@ -746,7 +746,7 @@
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 {