Skip to content

Commit

Permalink
chore(spanner): use time.Since instead of time.Now().Sub (#10096)
Browse files Browse the repository at this point in the history
time.Since has few optimizations to get monotonic time, also it's shorter and clearer.
  • Loading branch information
egonelbre authored May 3, 2024
1 parent a4a8fbc commit 63f947d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions spanner/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions spanner/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion spanner/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 63f947d

Please sign in to comment.