Skip to content

Commit

Permalink
[7.4.0] Fix cache duration computation in CredentialCacheExpiry. (#23453
Browse files Browse the repository at this point in the history
)

Also use the supplied current time instead of calling Instant.now().

I don't know how to meaningfully test this; there's so little going on
that the test would essentially mirror the implementation.

Fixes #23429.

PiperOrigin-RevId: 668351088
Change-Id: I12f1575e5280330c61361e4cf1b7d9f9231f16eb

Commit
5209ce7

Co-authored-by: Googler <[email protected]>
  • Loading branch information
bazel-io and tjgq authored Aug 28, 2024
1 parent be99997 commit 6980120
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ public void setDefaultCacheDuration(Duration duration) {
this.defaultCacheDuration = Preconditions.checkNotNull(duration);
}

private Duration getExpirationTime(GetCredentialsResponse response) {
private Duration getExpirationTime(GetCredentialsResponse response, Instant currentTime) {
Preconditions.checkNotNull(response);

var expires = response.getExpires();
if (expires.isEmpty()) {
return defaultCacheDuration;
}

var now = Instant.now();
return Duration.between(expires.get(), now);
return Duration.between(currentTime, expires.get());
}

@Override
public long expireAfterCreate(URI uri, GetCredentialsResponse response, long currentTime) {
Preconditions.checkNotNull(uri);
Preconditions.checkNotNull(response);

return getExpirationTime(response).toNanos();
return getExpirationTime(response, Instant.ofEpochMilli(nanoToMilli(currentTime))).toNanos();
}

@Override
Expand All @@ -58,7 +57,7 @@ public long expireAfterUpdate(
Preconditions.checkNotNull(uri);
Preconditions.checkNotNull(response);

return getExpirationTime(response).toNanos();
return getExpirationTime(response, Instant.ofEpochMilli(nanoToMilli(currentTime))).toNanos();
}

@CanIgnoreReturnValue
Expand All @@ -71,4 +70,8 @@ public long expireAfterRead(
// We don't extend the duration on access.
return currentDuration;
}

private static final long nanoToMilli(long nano) {
return Duration.ofNanos(nano).toMillis();
}
}

0 comments on commit 6980120

Please sign in to comment.