stillalex commented on code in PR #1921:
URL: https://github.com/apache/solr/pull/1921#discussion_r1326392234
##########
solr/core/src/java/org/apache/solr/security/PKIAuthenticationPlugin.java:
##########
@@ -485,34 +487,75 @@ private Optional<String> getUser() {
}
}
+ private static class CachedToken {
+ Instant generatedAt;
+ String token;
+
+ private CachedToken(Instant generatedAt, String token) {
+ this.generatedAt = generatedAt;
+ this.token = token;
+ }
+ }
+
+ private volatile ConcurrentHashMap<String, AtomicReference<CachedToken>>
cachedV1Tokens =
+ new ConcurrentHashMap<>();
+ private volatile ConcurrentHashMap<String, AtomicReference<CachedToken>>
cachedV2Tokens =
+ new ConcurrentHashMap<>();
+
+ private static final Duration cacheExpiryTime = Duration.ofSeconds(1);
+
+ private String getToken(String usr) {
+ AtomicReference<CachedToken> tokenRef =
+ cachedV1Tokens.computeIfAbsent(usr, u -> new
AtomicReference<>(generateToken(u)));
+ if
(tokenRef.get().generatedAt.isBefore(Instant.now().minus(cacheExpiryTime))) {
+ synchronized (tokenRef) {
+ if
(tokenRef.get().generatedAt.isBefore(Instant.now().minus(cacheExpiryTime))) {
+ tokenRef.set(generateToken(usr));
+ }
+ }
+ }
+ return tokenRef.get().token;
+ }
+
+ private synchronized String getTokenV2(String usr) {
Review Comment:
the 2 methods `getToken` and `getTokenV2` are very similar. would you
consider unifying into a single one? (one that gets a map and a 'token
generator' function)
--
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]