mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Merge branch '2.4.x' into 2.5.x
Closes gh-28347
This commit is contained in:
commit
2430d8a41a
@ -18,9 +18,10 @@ package org.springframework.boot.actuate.endpoint.invoker.cache;
|
||||
|
||||
import java.security.Principal;
|
||||
import java.time.Duration;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@ -30,6 +31,7 @@ import org.springframework.boot.actuate.endpoint.InvocationContext;
|
||||
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
@ -45,6 +47,8 @@ public class CachingOperationInvoker implements OperationInvoker {
|
||||
|
||||
private static final boolean IS_REACTOR_PRESENT = ClassUtils.isPresent("reactor.core.publisher.Mono", null);
|
||||
|
||||
private static final int CACHE_CLEANUP_THRESHOLD = 40;
|
||||
|
||||
private final OperationInvoker invoker;
|
||||
|
||||
private final long timeToLive;
|
||||
@ -61,7 +65,7 @@ public class CachingOperationInvoker implements OperationInvoker {
|
||||
Assert.isTrue(timeToLive > 0, "TimeToLive must be strictly positive");
|
||||
this.invoker = invoker;
|
||||
this.timeToLive = timeToLive;
|
||||
this.cachedResponses = new ConcurrentHashMap<>();
|
||||
this.cachedResponses = new ConcurrentReferenceHashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,6 +82,9 @@ public class CachingOperationInvoker implements OperationInvoker {
|
||||
return this.invoker.invoke(context);
|
||||
}
|
||||
long accessTime = System.currentTimeMillis();
|
||||
if (this.cachedResponses.size() > CACHE_CLEANUP_THRESHOLD) {
|
||||
cleanExpiredCachedResponses(accessTime);
|
||||
}
|
||||
ApiVersion contextApiVersion = context.resolveArgument(ApiVersion.class);
|
||||
Principal principal = context.resolveArgument(Principal.class);
|
||||
CacheKey cacheKey = new CacheKey(contextApiVersion, principal);
|
||||
@ -90,6 +97,20 @@ public class CachingOperationInvoker implements OperationInvoker {
|
||||
return cached.getResponse();
|
||||
}
|
||||
|
||||
private void cleanExpiredCachedResponses(long accessTime) {
|
||||
try {
|
||||
Iterator<Entry<CacheKey, CachedResponse>> iterator = this.cachedResponses.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Entry<CacheKey, CachedResponse> entry = iterator.next();
|
||||
if (entry.getValue().isStale(accessTime, this.timeToLive)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasInput(InvocationContext context) {
|
||||
Map<String, Object> arguments = context.getArguments();
|
||||
if (!ObjectUtils.isEmpty(arguments)) {
|
||||
|
@ -23,6 +23,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.assertj.core.api.InstanceOfAssertFactories;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@ -101,6 +102,31 @@ class CachingOperationInvokerTests {
|
||||
assertThat(response).isSameAs(cachedResponse);
|
||||
}
|
||||
|
||||
@Test // gh-28313
|
||||
void cacheWhenEachPrincipalIsUniqueDoesNotConsumeTooMuchMemory() throws Exception {
|
||||
MonoOperationInvoker target = new MonoOperationInvoker();
|
||||
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 50L);
|
||||
int count = 1000;
|
||||
for (int i = 0; i < count; i++) {
|
||||
invokeWithUniquePrincipal(invoker);
|
||||
}
|
||||
long expired = System.currentTimeMillis() + 50;
|
||||
while (System.currentTimeMillis() < expired) {
|
||||
Thread.sleep(10);
|
||||
}
|
||||
invokeWithUniquePrincipal(invoker);
|
||||
assertThat(invoker).extracting("cachedResponses").asInstanceOf(InstanceOfAssertFactories.MAP)
|
||||
.hasSizeLessThan(count);
|
||||
}
|
||||
|
||||
private void invokeWithUniquePrincipal(CachingOperationInvoker invoker) {
|
||||
SecurityContext securityContext = mock(SecurityContext.class);
|
||||
Principal principal = mock(Principal.class);
|
||||
given(securityContext.getPrincipal()).willReturn(principal);
|
||||
InvocationContext context = new InvocationContext(securityContext, Collections.emptyMap());
|
||||
((Mono<?>) invoker.invoke(context)).block();
|
||||
}
|
||||
|
||||
private void assertCacheIsUsed(Map<String, Object> parameters) {
|
||||
assertCacheIsUsed(parameters, null);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user