stillalex commented on code in PR #1921:
URL: https://github.com/apache/solr/pull/1921#discussion_r1326406193
##########
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) {
+ AtomicReference<CachedToken> tokenRef =
+ cachedV2Tokens.computeIfAbsent(usr, u -> new
AtomicReference<>(generateTokenV2(u)));
+ if
(tokenRef.get().generatedAt.isBefore(Instant.now().minus(cacheExpiryTime))) {
Review Comment:
just wondering how this performs if it would be a single
`ConcurrentHashMap#compute` method instead of all the locks and checks. is
there a benchmark for this change?
--
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]