Skip to content

Commit

Permalink
fix(rest): Add retries and synchronize OAuth credentials init for the…
Browse files Browse the repository at this point in the history
… Cloud Function (#3474)

* fix(rest): Add retries and synchronize OAuth credentials init for the Cloud Function

* fix(rest): Use constant

(cherry picked from commit 6ce2802)
  • Loading branch information
johnBgood authored and github-actions[bot] committed Oct 11, 2024
1 parent 443fa79 commit d8578c4
Showing 1 changed file with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

public class CloudFunctionCredentialsCache {

public static final int MAX_ATTEMPTS = 3;
private static final Logger LOG = LoggerFactory.getLogger(CloudFunctionCredentialsCache.class);
private OAuth2Credentials credentials;

Expand All @@ -35,33 +36,40 @@ public class CloudFunctionCredentialsCache {
* @param credentialsSupplier Supplier to fetch new credentials
* @return Optional of credentials
*/
public OAuth2Credentials get(Supplier<OAuth2Credentials> credentialsSupplier) {
public synchronized OAuth2Credentials get(Supplier<OAuth2Credentials> credentialsSupplier) {
return getWithRetries(credentialsSupplier, MAX_ATTEMPTS);
}

private OAuth2Credentials getWithRetries(
Supplier<OAuth2Credentials> credentialsSupplier, int attempts) {
if (credentials == null) {
LOG.debug("Credentials cache is empty, fetching new credentials");
credentials = credentialsSupplier.get();
}

try {
refreshAccessTokenIfExpired(credentials);
} catch (Exception e) {
this.credentials = credentialsSupplier.get();
} catch (IOException e) {
if (attempts > 1) {
LOG.warn("Failed to refresh access token, retrying... Attempts left: {}", attempts - 1);
return getWithRetries(credentialsSupplier, attempts - 1);
} else {
LOG.error("Failed to refresh access token after retries", e);
throw new RuntimeException("Failed to refresh access token after retries", e);
}
}
return credentials;
}

private void refreshAccessTokenIfExpired(OAuth2Credentials credentials) {
try {
AccessToken accessToken = credentials.getAccessToken();
if (accessToken == null || hasExpired(accessToken)) {
// Credentials are not initialized before calling refreshIfExpired
// See OAuth2Credentials#getAccessToken for more details
if (accessToken != null) {
LOG.debug("Access token expired, refreshing");
}
credentials.refreshIfExpired();
private void refreshAccessTokenIfExpired(OAuth2Credentials credentials) throws IOException {
AccessToken accessToken = credentials.getAccessToken();
if (accessToken == null || hasExpired(accessToken)) {
// Credentials are not initialized before calling refreshIfExpired
// See OAuth2Credentials#getAccessToken for more details
if (accessToken != null) {
LOG.debug("Access token expired, refreshing");
}
} catch (IOException e) {
throw new RuntimeException("Failed to refresh access token", e);
credentials.refreshIfExpired();
}
}

Expand Down

0 comments on commit d8578c4

Please sign in to comment.