Skip to content

Commit

Permalink
Performance improvement by caching the computation of the interceptor…
Browse files Browse the repository at this point in the history
…s that are applicable for each request
  • Loading branch information
ujibang committed Jan 29, 2021
1 parent d22a335 commit f29adb8
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 213 deletions.
12 changes: 7 additions & 5 deletions commons/src/main/java/org/restheart/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@
*/
public interface Cache<K,V> {
public enum EXPIRE_POLICY { NEVER, AFTER_WRITE, AFTER_READ };

public Optional<V> get(K key);

public void put(K key, V value);

public void cleanUp();

public void invalidate(K key);


public void invalidateAll();

public Map<K, Optional<V>> asMap();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public void invalidate(K key) {
wrapped.invalidate(key);
}

@Override
public void invalidateAll() {
wrapped.invalidateAll();
}

@Override
public Map<K, Optional<V>> asMap() {
return wrapped.asMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public void invalidate(K key) {
wrapped.invalidate(key);
}

@Override
public void invalidateAll() {
wrapped.invalidateAll();
}

@Override
public Map<K, Optional<V>> asMap() {
return wrapped.asMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.restheart.exchange.Response;

/**
* Abstract class to get the response and request implementation classes at
* Interface to get the response and request implementation classes at
* runtime
*
* @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
Expand Down
33 changes: 32 additions & 1 deletion commons/src/main/java/org/restheart/plugins/PluginsRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import io.undertow.predicate.Predicate;
import io.undertow.server.handlers.PathHandler;

import java.util.List;
import java.util.Set;
import org.restheart.ConfigurationException;
import org.restheart.exchange.PipelineInfo;
Expand Down Expand Up @@ -69,6 +71,18 @@ public interface PluginsRegistry {
*/
public Set<PluginRecord<Initializer>> getInitializers();

/**
* @return add the interceptor
*/
@SuppressWarnings("rawtypes")
public void addInterceptor(PluginRecord<Interceptor> i);

/**
* @return remove all interceptors that match the filter predicate
*/
@SuppressWarnings("rawtypes")
public boolean removeInterceptorIf(java.util.function.Predicate<? super PluginRecord<Interceptor>> filter);

/**
* @return the services
*/
Expand All @@ -78,6 +92,23 @@ public interface PluginsRegistry {
@SuppressWarnings("rawtypes")
public Set<PluginRecord<Interceptor>> getInterceptors();

/**
* @return the interceptors of the service srv
* @param srv
* @param interceptPoint
*
*/
@SuppressWarnings("rawtypes")
public List<Interceptor> getServiceInterceptors(Service<?,?> srv, InterceptPoint interceptPoint);

/**
* @return the interceptors of the proxy
* @param interceptPoint
*
*/
@SuppressWarnings("rawtypes")
public List<Interceptor> getProxyInterceptors(InterceptPoint interceptPoint);

/**
* global security predicates must all resolve to true to allow the request
*
Expand All @@ -88,7 +119,7 @@ public interface PluginsRegistry {

/**
* Gets the RESTHeart root handler
*
*
* Avoid adding handlers using PathHandler.addPrefixPath() or
* PathHandler.addExactPath(). Instead use PluginsRegistry.plug() which sets
* also the correct PipelineInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
package org.restheart.handlers;

import io.undertow.server.HttpServerExchange;
import java.util.Arrays;

import java.util.List;

import org.restheart.exchange.ByteArrayProxyRequest;
import org.restheart.exchange.ByteArrayProxyResponse;
import org.restheart.exchange.Exchange;
Expand All @@ -30,14 +32,12 @@
import org.restheart.exchange.ServiceRequest;
import org.restheart.exchange.ServiceResponse;
import org.restheart.plugins.InterceptPoint;
import org.restheart.plugins.Interceptor;
import org.restheart.plugins.PluginsRegistry;
import org.restheart.plugins.PluginsRegistryImpl;
import org.restheart.utils.HttpStatus;
import org.restheart.utils.LambdaUtils;
import org.restheart.utils.PluginUtils;
import static org.restheart.utils.PluginUtils.cachedRequestType;
import static org.restheart.utils.PluginUtils.cachedResponseType;
import static org.restheart.utils.PluginUtils.interceptPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,8 +47,7 @@
*/
public class RequestInterceptorsExecutor extends PipelinedHandler {

private static final Logger LOGGER = LoggerFactory
.getLogger(RequestInterceptorsExecutor.class);
private static final Logger LOGGER = LoggerFactory.getLogger(RequestInterceptorsExecutor.class);

private final ResponseSender sender = new ResponseSender();

Expand Down Expand Up @@ -90,42 +89,19 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {

var handlingService = PluginUtils.handlingService(pluginsRegistry, exchange);

if (handlingService != null) {
// if the request is handled by a service set to not execute interceptors
// at this interceptPoint, skip interceptors execution
// var vip = PluginUtils.dontIntercept(PluginsRegistryImpl.getInstance(), exchange);
var vip = PluginUtils.dontIntercept(handlingService);
if (Arrays.stream(vip).anyMatch(interceptPoint::equals)) {
next(exchange);
return;
}
List<Interceptor> interceptors;

if (handlingService != null) {
request = ServiceRequest.of(exchange, ServiceRequest.class);
response = ServiceResponse.of(exchange, ServiceResponse.class);
interceptors = pluginsRegistry.getServiceInterceptors(handlingService, interceptPoint);
} else {
request = ByteArrayProxyRequest.of(exchange);
response = ByteArrayProxyResponse.of(exchange);
interceptors = pluginsRegistry.getProxyInterceptors(interceptPoint);
}

pluginsRegistry
.getInterceptors()
.stream()
.filter(ri -> ri.isEnabled())
.map(ri -> ri.getInstance())
// IMPORTANT: An interceptor can intercept
// - requests handled by a Service when its request and response
// types are equal to the ones declared by the Service
// - request handled by a Proxy when its request and response
// are ByteArrayProxyRequest and ByteArrayProxyResponse
.filter(ri
-> (handlingService == null
&& cachedRequestType(ri).equals(ByteArrayProxyRequest.type())
&& cachedResponseType(ri).equals(ByteArrayProxyResponse.type()))
|| (handlingService != null
&& cachedRequestType(ri).equals(cachedRequestType(handlingService))
&& cachedResponseType(ri).equals(cachedResponseType(handlingService))))
.filter(ri -> interceptPoint == interceptPoint(ri))
.filter(ri -> {
interceptors.stream().filter(ri -> {
try {
return ri.resolve(request, response);
} catch (Exception e) {
Expand Down
Loading

0 comments on commit f29adb8

Please sign in to comment.