Skip to content

Commit

Permalink
Reduce unnecessary OAuth token refresh retry.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-alhuang committed Mar 27, 2024
1 parent dbd0ce0 commit 42f2cce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
36 changes: 16 additions & 20 deletions src/main/java/net/snowflake/ingest/connection/OAuthClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,24 @@ public AtomicReference<OAuthCredential> getOAuthCredentialRef() {
}

/** Refresh access token using a valid refresh token */
public void refreshToken() {
String respBodyString = null;
try (CloseableHttpResponse httpResponse = httpClient.execute(makeRefreshTokenRequest())) {
respBodyString = EntityUtils.toString(httpResponse.getEntity());

if (httpResponse.getStatusLine().getStatusCode() == HttpStatusCodes.STATUS_CODE_OK) {
JsonObject respBody = JsonParser.parseString(respBodyString).getAsJsonObject();

if (respBody.has(ACCESS_TOKEN) && respBody.has(EXPIRES_IN)) {
// Trim surrounding quotation marks
String newAccessToken = respBody.get(ACCESS_TOKEN).toString().replaceAll("^\"|\"$", "");
oAuthCredential.get().setAccessToken(newAccessToken);
oAuthCredential.get().setExpiresIn(respBody.get(EXPIRES_IN).getAsInt());
return;
}
public void refreshToken() throws IOException {
CloseableHttpResponse httpResponse = httpClient.execute(makeRefreshTokenRequest());
String respBodyString = EntityUtils.toString(httpResponse.getEntity());

if (httpResponse.getStatusLine().getStatusCode() == HttpStatusCodes.STATUS_CODE_OK) {
JsonObject respBody = JsonParser.parseString(respBodyString).getAsJsonObject();

if (respBody.has(ACCESS_TOKEN) && respBody.has(EXPIRES_IN)) {
// Trim surrounding quotation marks
String newAccessToken = respBody.get(ACCESS_TOKEN).toString().replaceAll("^\"|\"$", "");
oAuthCredential.get().setAccessToken(newAccessToken);
oAuthCredential.get().setExpiresIn(respBody.get(EXPIRES_IN).getAsInt());
return;
}
throw new SFException(
ErrorCode.OAUTH_REFRESH_TOKEN_ERROR,
"Refresh access token fail with response: " + respBodyString);
} catch (IOException e) {
throw new SFException(ErrorCode.OAUTH_REFRESH_TOKEN_ERROR, e.getMessage());
}
throw new SFException(
ErrorCode.OAUTH_REFRESH_TOKEN_ERROR,
"Refresh access token fail with response: " + respBodyString);
}

/** Helper method for making refresh request */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

package net.snowflake.ingest.connection;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import net.snowflake.client.jdbc.internal.apache.http.client.utils.URIBuilder;
import net.snowflake.ingest.utils.Constants;
Expand Down Expand Up @@ -154,6 +155,9 @@ void refreshToken() {
"Refresh access token, next refresh is scheduled after {} seconds", nextRefreshDelay);

return;
} catch (IOException e) {
// Http client already retried on IO exception, skip retires
break;
} catch (SFException e1) {
// Exponential backoff retries
try {
Expand Down

0 comments on commit 42f2cce

Please sign in to comment.