-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Amazon Lambda - minor cleanup #2197
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package io.quarkus.amazon.lambda.runtime; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
/** | ||
* Various constants and util methods used for communication with the AWS API. | ||
*/ | ||
public class AmazonLambdaApi { | ||
|
||
// Response Headers | ||
public static final String LAMBDA_RUNTIME_AWS_REQUEST_ID = "Lambda-Runtime-Aws-Request-Id"; | ||
public static final String LAMBDA_RUNTIME_INVOKED_FUNCTION_ARN = "Lambda-Runtime-Invoked-Function-Arn"; | ||
public static final String LAMBDA_RUNTIME_COGNITO_IDENTITY = "Lambda-Runtime-Cognito-Identity"; | ||
public static final String LAMBDA_RUNTIME_CLIENT_CONTEXT = "Lambda-Runtime-Client-Context"; | ||
public static final String LAMBDA_RUNTIME_DEADLINE_MS = "Lambda-Runtime-Deadline-Ms"; | ||
|
||
// Test API | ||
public static final String QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API = "quarkus-internal.aws-lambda.test-api"; | ||
|
||
// API paths | ||
public static final String API_PROTOCOL = "http://"; | ||
public static final String API_PATH_RUNTIME = "/2018-06-01/runtime/"; | ||
public static final String API_PATH_INVOCATION = API_PATH_RUNTIME + "invocation/"; | ||
public static final String API_PATH_INVOCATION_NEXT = API_PATH_INVOCATION + "next"; | ||
public static final String API_PATH_INIT_ERROR = API_PATH_RUNTIME + "init/error"; | ||
public static final String API_PATH_ERROR = "/error"; | ||
public static final String API_PATH_RESPONSE = "/response"; | ||
|
||
static URL invocationNext() throws MalformedURLException { | ||
return new URL(API_PROTOCOL + runtimeApi() + API_PATH_INVOCATION_NEXT); | ||
} | ||
|
||
static URL invocationError(String requestId) throws MalformedURLException { | ||
return new URL(API_PROTOCOL + runtimeApi() + API_PATH_INVOCATION + requestId + API_PATH_ERROR); | ||
} | ||
|
||
static URL invocationResponse(String requestId) throws MalformedURLException { | ||
return new URL(API_PROTOCOL + runtimeApi() + API_PATH_INVOCATION + requestId + API_PATH_RESPONSE); | ||
} | ||
|
||
static URL initError() throws MalformedURLException { | ||
return new URL(API_PROTOCOL + runtimeApi() + API_PATH_INIT_ERROR); | ||
} | ||
|
||
static String logGroupName() { | ||
return System.getenv("AWS_LAMBDA_LOG_GROUP_NAME"); | ||
} | ||
|
||
static String functionMemorySize() { | ||
return System.getenv("AWS_LAMBDA_FUNCTION_MEMORY_SIZE"); | ||
} | ||
|
||
static String logStreamName() { | ||
return System.getenv("AWS_LAMBDA_LOG_STREAM_NAME"); | ||
} | ||
|
||
static String functionName() { | ||
return System.getenv("AWS_LAMBDA_FUNCTION_NAME"); | ||
} | ||
|
||
static String functionVersion() { | ||
return System.getenv("AWS_LAMBDA_FUNCTION_VERSION"); | ||
} | ||
|
||
private static String runtimeApi() { | ||
String testApi = System.getProperty(QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API); | ||
if (testApi != null) { | ||
return testApi; | ||
} | ||
return System.getenv("AWS_LAMBDA_RUNTIME_API"); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.amazon.lambda.runtime.AmazonLambdaApi; | ||
import io.quarkus.amazon.lambda.runtime.FunctionError; | ||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
import io.undertow.Undertow; | ||
|
@@ -20,8 +21,6 @@ | |
|
||
public class LambdaResourceManager implements QuarkusTestResourceLifecycleManager { | ||
|
||
protected static final String QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API = "quarkus-internal.aws-lambda.test-api"; | ||
|
||
private volatile Undertow undertow; | ||
|
||
public static final int PORT = Integer.getInteger("quarkus-internal.aws-lambda.test-port", 5387); | ||
|
@@ -30,7 +29,7 @@ public class LambdaResourceManager implements QuarkusTestResourceLifecycleManage | |
public Map<String, String> start() { | ||
|
||
RoutingHandler routingHandler = new RoutingHandler(true); | ||
routingHandler.add("GET", "/2018-06-01/runtime/invocation/next", new HttpHandler() { | ||
routingHandler.add("GET", AmazonLambdaApi.API_PATH_INVOCATION_NEXT, new HttpHandler() { | ||
@Override | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
LambdaStartedNotifier.started = true; | ||
|
@@ -41,44 +40,46 @@ public void handleRequest(HttpServerExchange exchange) throws Exception { | |
return; | ||
} | ||
} | ||
exchange.getResponseHeaders().put(new HttpString("Lambda-Runtime-Aws-Request-Id"), req.id); | ||
exchange.getResponseHeaders().put(new HttpString(AmazonLambdaApi.LAMBDA_RUNTIME_AWS_REQUEST_ID), req.id); | ||
exchange.getResponseSender().send(req.json); | ||
} | ||
}); | ||
routingHandler.add("POST", "/2018-06-01/runtime/invocation/{req}/response", new HttpHandler() { | ||
@Override | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
String id = exchange.getQueryParameters().get("req").getFirst(); | ||
exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() { | ||
routingHandler.add("POST", AmazonLambdaApi.API_PATH_INVOCATION + "{req}" + AmazonLambdaApi.API_PATH_RESPONSE, | ||
new HttpHandler() { | ||
@Override | ||
public void handle(HttpServerExchange exchange, String message) { | ||
LambdaClient.REQUESTS.get(id).complete(message); | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
String id = exchange.getQueryParameters().get("req").getFirst(); | ||
exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() { | ||
@Override | ||
public void handle(HttpServerExchange exchange, String message) { | ||
LambdaClient.REQUESTS.get(id).complete(message); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
routingHandler.add("POST", "/2018-06-01/runtime/invocation/{req}/error", new HttpHandler() { | ||
@Override | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
String id = exchange.getQueryParameters().get("req").getFirst(); | ||
exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() { | ||
routingHandler.add("POST", AmazonLambdaApi.API_PATH_INVOCATION + "{req}" + AmazonLambdaApi.API_PATH_ERROR, | ||
new HttpHandler() { | ||
@Override | ||
public void handle(HttpServerExchange exchange, String message) { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
try { | ||
FunctionError result = mapper.readerFor(FunctionError.class).readValue(message); | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
String id = exchange.getQueryParameters().get("req").getFirst(); | ||
exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() { | ||
@Override | ||
public void handle(HttpServerExchange exchange, String message) { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
try { | ||
FunctionError result = mapper.readerFor(FunctionError.class).readValue(message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should cache the reader here. It can be done in another PR though as it was already preexisting. I don't know if we use this pattern elsewhere in the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it is safe to share a reader then +1 for a new PR ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I missed it's just in the tests. We don't really care then. |
||
|
||
LambdaClient.REQUESTS.get(id).completeExceptionally( | ||
new LambdaException(result.getErrorType(), result.getErrorMessage())); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
LambdaClient.REQUESTS.get(id).completeExceptionally( | ||
new LambdaException(result.getErrorType(), result.getErrorMessage())); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
routingHandler.add("POST", "/2018-06-01/runtime/init/error", new HttpHandler() { | ||
routingHandler.add("POST", AmazonLambdaApi.API_PATH_INIT_ERROR, new HttpHandler() { | ||
@Override | ||
public void handleRequest(HttpServerExchange exchange) throws Exception { | ||
exchange.getRequestReceiver().receiveFullString(new Receiver.FullStringCallback() { | ||
|
@@ -103,7 +104,7 @@ public void handle(HttpServerExchange exchange, String message) { | |
.setHandler(new BlockingHandler(routingHandler)) | ||
.build(); | ||
undertow.start(); | ||
return Collections.singletonMap(QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, "localhost:" + PORT); | ||
return Collections.singletonMap(AmazonLambdaApi.QUARKUS_INTERNAL_AWS_LAMBDA_TEST_API, "localhost:" + PORT); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's happening here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I think it reads the data until the end of the stream is reached. I've just added a comment because an empty while loop looks weird to me ;-).