nastra commented on code in PR #6489:
URL: https://github.com/apache/iceberg/pull/6489#discussion_r1056427267


##########
core/src/main/java/org/apache/iceberg/rest/auth/OAuth2Util.java:
##########
@@ -376,5 +409,105 @@ public Pair<Integer, TimeUnit> refresh(RESTClient client) 
{
 
       return null;
     }
+
+    @SuppressWarnings("FutureReturnValueIgnored")
+    public static void scheduleTokenRefresh(
+        RESTClient client,
+        ScheduledExecutorService tokenRefreshExecutor,
+        AuthSession session,
+        long startTimeMillis,
+        long expiresIn,
+        TimeUnit unit) {
+      // convert expiration interval to milliseconds
+      long expiresInMillis = unit.toMillis(expiresIn);
+      // how much ahead of time to start the request to allow it to complete
+      long refreshWindowMillis = Math.min(expiresInMillis / 10, 
MAX_REFRESH_WINDOW_MILLIS);
+      // how much time to wait before expiration
+      long waitIntervalMillis = expiresInMillis - refreshWindowMillis;
+      // how much time has already elapsed since the new token was issued
+      long elapsedMillis = System.currentTimeMillis() - startTimeMillis;
+      // how much time to actually wait
+      long timeToWait = Math.max(waitIntervalMillis - elapsedMillis, 
MIN_REFRESH_WAIT_MILLIS);
+
+      tokenRefreshExecutor.schedule(
+          () -> {
+            long refreshStartTime = System.currentTimeMillis();
+            Pair<Integer, TimeUnit> expiration = session.refresh(client);
+            if (expiration != null) {
+              scheduleTokenRefresh(
+                  client,
+                  tokenRefreshExecutor,
+                  session,
+                  refreshStartTime,
+                  expiration.first(),
+                  expiration.second());
+            }
+          },
+          timeToWait,
+          TimeUnit.MILLISECONDS);
+    }
+
+    public static AuthSession sessionFromToken(

Review Comment:
   `sessionFromToken` / `sessionFromFetchedToken` / `sessionFromTokenExchange` 
are mainly public so that they can also be used for 
https://github.com/apache/iceberg/pull/6169



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to