rmannibucau commented on PR #7:
URL: https://github.com/apache/geronimo-jwt-auth/pull/7#issuecomment-1937495098

   @ArneLimburg let me try to put some pseudo code on github:
   
   ```
   // volatile Set<String> remoteKeyIds
   // volatile CompletionFuture<?> keyLoading
   // ScheduledExecutorService backgroundThread
   // HttpClient httpClient
   
   @PostConstruct private void init() {
       readFromConfUri(...).ifPresent(uri -> {
           backgroundThread = Executors.newSingleScheduledExecutorService(new 
Threadfactory() { /* name + ThisClass.class.getClassLoader() on the thread */ 
});
           httpClient = 
HttpClient.newBuilder().executor(backgroundThread).....build();
           keyLoading = reloadRemoteKeys();
           readFromConfRefreshIntervalMs(...).filter(i -> i > 0).ifPresent(i -> 
backgroundThread.scheduledAtFixedRate(this::reloadRemoteKeys, i, MILLISECONDS, 
i, MILLISECONDS);
       });
   }
   
   private Key getKey(String id) {
     if (!keyLoading.isDone()) { keyLoading.get(1, MINUTE); } // only give it a 
chance for the initial loading, then at runtime it is always true even for 
rotations
     // as now
   }
   
   private void reloadRemoteKeys() {
       keyLoading = httpClient.sendAsync(....).thenAccept(res -> {
           check200(res);
           final var keys = parse(res.body());
           final var newRemoteKeyIds = ids(keys);
           final var noMorePresent = diffRemoved(remoteKeyIds, newRemoteKeyIds);
           noMorePresent.forEach(keyMapping::removeKey);
           keyMapping.putAll(keys);
           remoteKeyIds = newRemoteKeyIds;
       });
   }
   
   @PreDestroy private void destroy() {
     // if != null ... on all statement
     keyLoading.get(1, MINUTES); // if pending give it a chance to finish
     backgroundThread.shutdownNow(); + await (1mn max and then give up?)
     if (httpClient instance AutoCloseable) { close(); } // java >= 21
   }
   ```


-- 
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: dev-unsubscr...@geronimo.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to