Skip to content

Commit

Permalink
change AsynFunction to OpenFunction
Browse files Browse the repository at this point in the history
Signed-off-by: wanjunlei <wanjunlei@kubesphere.io>
  • Loading branch information
wanjunlei committed Jun 23, 2022
1 parent e3703de commit 8a0f54a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
package dev.openfunction.functions;

@FunctionalInterface
public interface AsyncFunction {
public interface OpenFunction {

/**
* Called to service an incoming event. This interface is implemented by user code to provide the
* action for a given asynchronous function.
* action for a given function.
*
* @param context context
* @param payload incoming event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,27 @@ public final class AsynchronousRuntime implements Runtime {

private RuntimeContext runtimeContext;

private final AsyncFunction function;
private final OpenFunction function;

private final Service service;

private AsynchronousRuntime(AsyncFunction function) {
private AsynchronousRuntime(OpenFunction function) {
this.function = function;
service = new Service();
}

public static AsynchronousRuntime forClass(Class<?> functionClass) {
if (!AsyncFunction.class.isAssignableFrom(functionClass)) {
if (!OpenFunction.class.isAssignableFrom(functionClass)) {
throw new RuntimeException(
"Class "
+ functionClass.getName()
+ " does not implement "
+ HttpFunction.class.getName());
+ OpenFunction.class.getName());
}

try {
Class<? extends AsyncFunction> asyncHttpFunctionClass = functionClass.asSubclass(AsyncFunction.class);
return new AsynchronousRuntime(asyncHttpFunctionClass.getConstructor().newInstance());
Class<? extends OpenFunction> openFunctionClass = functionClass.asSubclass(OpenFunction.class);
return new AsynchronousRuntime(openFunctionClass.getConstructor().newInstance());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(
"Could not construct an instance of " + functionClass.getName(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

package dev.openfunction.invoker.runtime;

import dev.openfunction.functions.AsyncFunction;
import dev.openfunction.functions.CloudEventFunction;
import dev.openfunction.functions.HttpFunction;
import dev.openfunction.functions.OpenFunction;
import dev.openfunction.functions.Out;
import dev.openfunction.invoker.context.RuntimeContext;
import dev.openfunction.invoker.context.UserContext;
Expand Down Expand Up @@ -54,7 +54,7 @@ public class SynchronizeRuntime extends HttpServlet implements Runtime {

private CloudEventFunction cloudEventFunction;

private AsyncFunction asyncFunction;
private OpenFunction openFunction;

private RuntimeContext runtimeContext;

Expand All @@ -69,17 +69,14 @@ private SynchronizeRuntime(CloudEventFunction cloudEventFunction) {
EventFormatProvider.getInstance().registerFormat(new JsonEventFormat());
}

private SynchronizeRuntime(AsyncFunction asyncFunction) {
this.asyncFunction = asyncFunction;
daprClient = new DaprClientBuilder().build();
daprClient.waitForSidecar(WaitDaprSidecarTimeout);
private SynchronizeRuntime(OpenFunction openFunction) {
this.openFunction = openFunction;
}

/**
* Makes a {@link SynchronizeRuntime} for the given class.
*
* @param functionClass function class
*
* @return {@link SynchronizeRuntime}
*/
public static SynchronizeRuntime forClass(Class<?> functionClass) {
Expand All @@ -93,10 +90,10 @@ public static SynchronizeRuntime forClass(Class<?> functionClass) {
Class<? extends CloudEventFunction> cloudEventFunctionClass = functionClass.asSubclass(CloudEventFunction.class);
CloudEventFunction cloudEventFunction = cloudEventFunctionClass.getConstructor().newInstance();
return new SynchronizeRuntime(cloudEventFunction);
} else if (AsyncFunction.class.isAssignableFrom(functionClass)) {
Class<? extends AsyncFunction> asyncFunctionClass = functionClass.asSubclass(AsyncFunction.class);
AsyncFunction asyncFunction = asyncFunctionClass.getConstructor().newInstance();
return new SynchronizeRuntime(asyncFunction);
} else if (OpenFunction.class.isAssignableFrom(functionClass)) {
Class<? extends OpenFunction> asyncFunctionClass = functionClass.asSubclass(OpenFunction.class);
OpenFunction openFunction = asyncFunctionClass.getConstructor().newInstance();
return new SynchronizeRuntime(openFunction);
}
} catch (ReflectiveOperationException e) {
throw new Error("Could not construct an instance of " + functionClass.getName(), e);
Expand Down Expand Up @@ -151,9 +148,9 @@ public void service(HttpServletRequest req, HttpServletResponse res) {
}

respImpl.getOutputStream().write(userContext.getOut().getData().array());
} else if (asyncFunction != null) {
} else if (openFunction != null) {
userContext.executePrePlugins();
Out out = asyncFunction.accept(userContext, Arrays.toString(reqImpl.getInputStream().readAllBytes()));
Out out = openFunction.accept(userContext, Arrays.toString(reqImpl.getInputStream().readAllBytes()));
userContext.setOut(out);
userContext.executePostPlugins();

Expand Down Expand Up @@ -196,6 +193,12 @@ public void service(HttpServletRequest req, HttpServletResponse res) {
public void start(RuntimeContext ctx) throws Exception {

runtimeContext = ctx;
// create dapr client when dapr sidecar enabled.
if (System.getenv("DAPR_GRPC_PORT") != null || System.getenv("DAPR_HTTP_PORT") != null) {
daprClient = new DaprClientBuilder().build();
daprClient.waitForSidecar(Runtime.WaitDaprSidecarTimeout);
}

Server server = new Server(ctx.getPort());

ServletContextHandler servletContextHandler = new ServletContextHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

package dev.openfunction.samples;

import dev.openfunction.functions.AsyncFunction;
import dev.openfunction.functions.OpenFunction;
import dev.openfunction.functions.Context;
import dev.openfunction.functions.Out;

public class AsyncFunctionImpl implements AsyncFunction {
public class OpenFunctionImpl implements OpenFunction {

@Override
public Out accept(Context context, String payload) throws Exception {
Expand Down
2 changes: 1 addition & 1 deletion samples/src/main/resources/function/binding-function.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
build:
builder: openfunctiondev/builder-java:v2-11
env:
FUNC_NAME: "dev.openfunction.samples.AsyncFunctionImpl"
FUNC_NAME: "dev.openfunction.samples.OpenFunctionImpl"
FUNC_CLEAR_SOURCE: "true"
srcRepo:
url: "https://github.com/wanjunlei/functions-framework-java.git"
Expand Down
2 changes: 1 addition & 1 deletion samples/src/main/resources/function/http-with-output.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ spec:
build:
builder: openfunctiondev/builder-java:v2-17
env:
FUNC_NAME: "dev.openfunction.samples.HttpFunctionImpl"
FUNC_NAME: "dev.openfunction.samples.OpenFunctionImpl"
FUNC_CLEAR_SOURCE: "true"
srcRepo:
url: "https://github.com/wanjunlei/functions-framework-java.git"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
build:
builder: openfunctiondev/builder-java:v2-16
env:
FUNC_NAME: "dev.openfunction.samples.AsyncFunctionImpl"
FUNC_NAME: "dev.openfunction.samples.OpenFunctionImpl"
FUNC_CLEAR_SOURCE: "true"
# Use FUNC_GOPROXY to set the goproxy
# FUNC_GOPROXY: "https://goproxy.cn"
Expand Down

0 comments on commit 8a0f54a

Please sign in to comment.