0

我正在向我的 OpenWhisk/IBM Cloud 功能添加 CORS 支持。但是在对函数进行修改(-a web-custom-options true)之后,我注意到性能下降了。为了隔离这个问题,我创建了一个简单的函数,见下文:

public static JsonObject main(JsonObject args) throws IOException {


        String method = args.get("__ow_method").getAsString();
        System.out.println(method+" handle");
        if (method.equalsIgnoreCase("OPTIONS")) {
            JsonObject responseJSON = new JsonObject();
            //add CORS headers

            JsonObject headers = new JsonObject();
            headers.addProperty("Access-Control-Allow-Headers", "*");
            headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
            headers.addProperty("Access-Control-Allow-Credentials", "true");

            responseJSON.add("headers", headers);
            responseJSON.addProperty("statusCode", 200);

            return responseJSON;
        } else {

            JsonObject responseJSON = new JsonObject();
            JsonObject headers = new JsonObject();      
            headers.addProperty("Access-Control-Allow-Headers", "*");
            headers.addProperty("Access-Control-Allow-Origin", "https://mjonker.github.io");
            headers.addProperty("Access-Control-Allow-Credentials", "true");
            headers.addProperty("Content-Type", "application/json");
            responseJSON.add("headers", headers);
            Date now = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
            JsonObject answerJSON=new JsonObject();
            JsonArray timeArray = new JsonArray();
            timeArray.add( "It is "+sdf.format(now));
            answerJSON.add("text",timeArray);
            responseJSON.add("body",answerJSON );
            responseJSON.addProperty("statusCode", 200);
            return responseJSON;

        }
    }

有两种情况 1. .http 端点和 web-custom-options true 2. .json 端点和 web-custom-options false

正如您从屏幕截图中看到的,OPTIONS 的差异很大,但 POST 回复的差异也很明显。我可以做些什么来获得 CORS 的一些性能支持吗?我在 JAVA 代码中做错了吗?

在此处输入图像描述 在此处输入图像描述

4

2 回答 2

1

当您使用自定义选项响应创建 Web 操作时,相应的函数将执行并生成 OPTIONS 响应。但是,如果您允许默认 OPTIONS 响应生效,则不会执行任何函数,并且默认响应由 API 主机提供。

默认响应显示在这里:https ://github.com/apache/incubator-openwhisk/blob/master/docs/webactions.md#options-requests

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: OPTIONS, GET, DELETE, POST, PUT, HEAD, PATCH
Access-Control-Allow-Headers: Authorization, Content-Type

由于您正在执行 Java 操作,因此启动时间可以解释您所看到的性能。我看到 313 毫秒的初始化时间和 342 毫秒的持续时间只是执行冷启动(当然,这绝不是代表)。

于 2018-02-26T18:39:36.317 回答
0

移至 eu-gb,不再出现性能问题

于 2018-02-27T10:20:09.600 回答