DialogFlow 完成 http 请求时响应缓慢

Slow time response on DialogFlow fullfilment http requests

我正在为 DialogFlow 上的 google 助手开发一个应用程序。 出于某种目的,我有一个必须执行 http 请求的 fullfilment。

代码是这样的:

const syncrequest = require('sync-request');

console.log('Request start');

var res = syncrequest('GET', urlRequest, {
  json: {},
});

console.log('Request end');

测试我正在使用的 url 大约需要 0.103 秒来响应。 但是看firebase log,是这样的:

3:01:58.555 下午 dialogflowFirebaseFulfillment 请求结束

3:01:56.585 下午 dialogflowFirebaseFulfillment 请求开始

即使我的服务器在 0.103 秒内响应,请求也需要 2 秒才能处理。 有时它需要超过 4 秒并使我的应用程序崩溃。 有谁知道为什么要花这么长时间?我可以做些什么来更快地完成请求吗?

提前致谢

我没有仔细查看 sync-request package,但我确实在 npm 页面上看到了这个大警告:

You should not be using this in a production application. In a node.js application you will find that you are completely unable to scale your server. In a client application you will find that sync-request causes the app to hang/freeze. Synchronous web requests are the number one cause of browser crashes. For production apps, you should use then-request, which is exactly the same except that it is asynchronous.

基于此以及页面上的其他一些信息,听起来这个包的性能很差,处理同步操作的效率可能非常低。

您可能希望切换到 then-request package, as it suggests, however the most common way to handle HTTP calls is using request-promise-native,在那里您可以执行如下操作:

const rp = require('request-promise-native');

return rp.get(url)
  .then( body => {
    // Set the Dialogflow response here
    // You didn't really show this in your code.
  });

如果您正在执行异步任务 - 您必须 return 来自意图处理程序的承诺。