From c20837828c577a2ba9c84339a9165359a856d01b Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 25 Sep 2023 16:48:28 +0200 Subject: [PATCH] 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 --- .../function/adapter/aws/CustomRuntimeEventLoop.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeEventLoop.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeEventLoop.java index 7e91f7eab..917ca8693 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeEventLoop.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/CustomRuntimeEventLoop.java @@ -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, "