Skip to content

Commit

Permalink
🐛 "Prevent null return in HashMapLoadingCache.getLoading() to address…
Browse files Browse the repository at this point in the history
… potential multithreading cache issue
  • Loading branch information
ujibang committed Apr 10, 2024
1 parent 6e3ed24 commit 6805292
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import com.google.common.collect.Maps;

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ public Optional<V> remove(K key) {

@Override
public Optional<V> getLoading(K key) {
if(wrapped.containsKey(key)) {
if(get(key) != null) {
return get(key);
} else {
var value = Optional.ofNullable(loader.apply(key));
Expand Down
30 changes: 17 additions & 13 deletions commons/src/main/java/org/restheart/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@
*/
package org.restheart.utils;

import io.undertow.server.HttpServerExchange;

import java.lang.reflect.Type;
import java.util.Map;

import org.restheart.cache.CacheFactory;
import org.restheart.cache.LoadingCache;
import static org.restheart.exchange.PipelineInfo.PIPELINE_TYPE.SERVICE;
import org.restheart.exchange.Request;
import org.restheart.exchange.Response;
import static org.restheart.exchange.PipelineInfo.PIPELINE_TYPE.SERVICE;
import org.restheart.plugins.ExchangeTypeResolver;
import org.restheart.plugins.InitPoint;
import org.restheart.plugins.Initializer;
Expand All @@ -37,18 +36,23 @@
import org.restheart.plugins.PluginRecord;
import org.restheart.plugins.PluginsRegistry;
import org.restheart.plugins.RegisterPlugin;
import org.restheart.plugins.Service;
import org.restheart.plugins.RegisterPlugin.MATCH_POLICY;
import org.restheart.plugins.Service;
import org.restheart.plugins.security.Authorizer;

import io.undertow.server.HttpServerExchange;

/**
*
* @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
*
* @return the interceptPoint as defined by the @RegisterPlugin annotation if
* annotated or the value of the field interceptPoint if exists
*/
public class PluginUtils {
/**
*
* @param interceptor
* @return the interceptPoint as defined by the @RegisterPlugin annotation if
* annotated or the value of the field interceptPoint if exists
*/
@SuppressWarnings("rawtypes")
public static InterceptPoint interceptPoint(Interceptor interceptor) {
var a = interceptor.getClass().getDeclaredAnnotation(RegisterPlugin.class);
Expand All @@ -75,8 +79,8 @@ private static InterceptPoint findInterceptPointField(Class<?> clazz, Object o)
field.setAccessible(true);

var value = field.get(o);
if (value instanceof InterceptPoint) {
return (InterceptPoint) value;
if (value instanceof InterceptPoint ip) {
return ip;
} else {
return null;
}
Expand Down Expand Up @@ -140,8 +144,8 @@ private static String findNameField(Class<?> clazz, Object o) {
field.setAccessible(true);

var value = field.get(o);
if (value instanceof String) {
return (String) value;
if (value instanceof String s) {
return s;
} else {
return null;
}
Expand Down Expand Up @@ -318,10 +322,10 @@ public static InterceptPoint[] dontIntercept(PluginsRegistry registry, HttpServe
}

@SuppressWarnings("rawtypes")
private static LoadingCache<ExchangeTypeResolver, Type> RC = CacheFactory.createHashMapLoadingCache(plugin -> plugin.requestType());
private static final LoadingCache<ExchangeTypeResolver, Type> RC = CacheFactory.createHashMapLoadingCache(plugin -> plugin.requestType());

@SuppressWarnings("rawtypes")
private static LoadingCache<ExchangeTypeResolver, Type> SC = CacheFactory.createHashMapLoadingCache(plugin -> plugin.responseType());
private static final LoadingCache<ExchangeTypeResolver, Type> SC = CacheFactory.createHashMapLoadingCache(plugin -> plugin.responseType());

/**
* Plugin.requestType() is heavy. This helper methods speeds up invocation using
Expand Down

0 comments on commit 6805292

Please sign in to comment.