michaeljmarshall commented on a change in pull request #13951:
URL: https://github.com/apache/pulsar/pull/13951#discussion_r794012032



##########
File path: 
pulsar-client/src/main/java/org/apache/pulsar/client/impl/auth/oauth2/AuthenticationOAuth2.java
##########
@@ -96,21 +127,71 @@ public void start() throws PulsarClientException {
         flow.initialize();
     }
 
+    /**
+     * The first time that this method is called, it retrieves a token. All 
subsequent
+     * calls should get a cached value. However, if there is an issue with the 
Identity
+     * Provider, there is a chance that the background thread responsible for 
keeping
+     * the refresh token hot will
+     * @return The authentication data identifying this client that will be 
sent to the broker
+     * @throws PulsarClientException
+     */
     @Override
     public synchronized AuthenticationDataProvider getAuthData() throws 
PulsarClientException {
         if (this.cachedToken == null || this.cachedToken.isExpired()) {
-            TokenResult tr = this.flow.authenticate();
-            this.cachedToken = new CachedToken(tr);
+            this.authenticate();
         }
         return this.cachedToken.getAuthData();
     }
 
+    /**
+     * Retrieve the token (synchronously), and then schedule refresh runnable.
+     */
+    private void authenticate() throws PulsarClientException {
+        if (log.isDebugEnabled()) {
+            log.debug("Attempting to retrieve OAuth2 token now.");
+        }
+        TokenResult tr = this.flow.authenticate();
+        this.cachedToken = new CachedToken(tr);
+        handleSuccessfulTokenRefresh();
+    }
+
+    private void handleSuccessfulTokenRefresh() {
+        if (scheduler != null) {
+            backoff.reset();
+            long expiresInMillis = 
TimeUnit.SECONDS.toMillis(cachedToken.latest.getExpiresIn());
+            scheduleRefresh((long) (expiresInMillis * expiryAdjustment));
+        }
+    }
+
+    /**
+     * Attempt to refresh the token. If successful, schedule the next refresh 
task according to the
+     * {@link #expiryAdjustment}. If failed, schedule another attempt to 
refresh the token according to the
+     * {@link #backoff} policy.
+     */
+    private void refreshToken() {
+        try {
+            this.authenticate();
+        } catch (Throwable e) {

Review comment:
       I will update it to: 
   
   ```java
           try {
               this.authenticate();
           } catch (PulsarClientException | RuntimeException e) {
               long delayMillis = backoff.next();
               log.error("Error refreshing token. Will retry in {} millis.", 
delayMillis, e);
               scheduleRefresh(delayMillis);
           }
   ```
   
   I want to be careful here to ensure that we don't let the token refreshing 
accidentally die.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to