GH-1049 cache RoutingFunction inside of CustomRuntimeEventLoop

There is no need to lookup function once it has been locked up on the first iteration of loop as there is no possibility for it to change. Any change to environment would trigger a new instsance of the loop

Resolves #1049
This commit is contained in:
Oleg Zhurakousky
2023-09-25 16:48:28 +02:00
parent 3c4d10e56b
commit c20837828c

View File

@@ -78,6 +78,8 @@ public final class CustomRuntimeEventLoop implements SmartLifecycle {
private ExecutorService executor = Executors.newSingleThreadExecutor();
private FunctionInvocationWrapper routingFunction;
public CustomRuntimeEventLoop(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@@ -202,6 +204,9 @@ public final class CustomRuntimeEventLoop implements SmartLifecycle {
private FunctionInvocationWrapper locateFunction(Environment environment, FunctionCatalog functionCatalog,
HttpHeaders httpHeaders) {
if (this.routingFunction != null) {
return this.routingFunction;
}
MediaType contentType = httpHeaders.getContentType();
String handlerName = environment.getProperty("DEFAULT_HANDLER");
if (logger.isDebugEnabled()) {
@@ -241,12 +246,13 @@ public final class CustomRuntimeEventLoop implements SmartLifecycle {
}
if (function == null) {
function = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME, "application/json");
if (function != null && logger.isInfoEnabled()) {
this.routingFunction = functionCatalog.lookup(RoutingFunction.FUNCTION_NAME, "application/json");
if (this.routingFunction != null && logger.isInfoEnabled()) {
logger.info("Will default to RoutingFunction, since multiple functions available in FunctionCatalog."
+ "Expecting 'spring.cloud.function.definition' or 'spring.cloud.function.routing-expression' as Message headers. "
+ "If invocation is over API Gateway, Message headers can be provided as HTTP headers.");
}
function = this.routingFunction;
}
Assert.notNull(function, "Failed to locate function. Tried locating default function, "