Azure cosmosdb 使用node.js客户端时网络超时约5秒

Azure cosmosdb 使用node.js客户端时网络超时约5秒,azure-cosmosdb,Azure Cosmosdb,请注意,这个问题以前非常不同。这才是真正的问题。这是 当使用调用executeStoredProcedure()时,我得到一个408代码,RequestTimeout,并且我没有从存储过程的“主体”返回任何数据。这似乎发生在大约5秒的时间,但当我从存储过程本身内部执行有时间限制的操作时,任何超过700毫秒的值都会导致网络超时(尽管直到大约5秒过去我才看到) 注意,我可以使用读取操作运行更长时间的存储过程。这似乎只有在我有很多createDocument()操作时才会发生,所以我认为它不在客户端。

请注意,这个问题以前非常不同。这才是真正的问题。这是

当使用调用executeStoredProcedure()时,我得到一个408代码,RequestTimeout,并且我没有从存储过程的“主体”返回任何数据。这似乎发生在大约5秒的时间,但当我从存储过程本身内部执行有时间限制的操作时,任何超过700毫秒的值都会导致网络超时(尽管直到大约5秒过去我才看到)

注意,我可以使用读取操作运行更长时间的存储过程。这似乎只有在我有很多createDocument()操作时才会发生,所以我认为它不在客户端。我认为服务器端正在发生一些事情

仍然有可能我最初的想法是正确的,并且我没有从createDocument()调用中得到错误的反馈,这会导致存储过程持续运行超过其超时时间,这就是导致408错误的原因

以下是“创建文档”存储过程的时间限制版本

generateData = function(memo) {
  var collection, collectionLink, nowTime, row, startTime, timeout;
  if ((memo != null ? memo.remaining : void 0) == null) {
    throw new Error('generateData must be called with an object containing a `remaining` field.');
  }
  if (memo.totalCount == null) {
    memo.totalCount = 0;
  }
  memo.countForThisRun = 0;
  timeout = memo.timeout || 600; // Works at 600. Fails at 800.
  startTime = new Date();
  memo.stillTime = true;
  collection = getContext().getCollection();
  collectionLink = collection.getSelfLink();
  memo.stillQueueing = true;
  while (memo.remaining > 0 && memo.stillQueueing && memo.stillTime) {
    row = {
      a: 1,
      b: 2
    };
    getContext().getResponse().setBody(memo);
    memo.stillQueueing = collection.createDocument(collectionLink, row);
    if (memo.stillQueueing) {
      memo.remaining--;
      memo.countForThisRun++;
      memo.totalCount++;
    }
    nowTime = new Date();
    memo.nowTime = nowTime;
    memo.startTime = startTime;
    memo.stillTime = (nowTime - startTime) < timeout;
    if (memo.stillTime) {
      memo.continuation = null;
    } else {
      memo.continuation = 'Value does not matter';
    }
  }
  getContext().getResponse().setBody(memo);
  return memo;
};
generateData=函数(备忘录){
var集合、collectionLink、nowTime、row、startTime、超时;
if((memo!=null?memo.remaining:void 0)==null){
抛出新错误(“必须使用包含“剩余”字段的对象调用generateData”);
}
如果(memo.totalCount==null){
memo.totalCount=0;
}
memo.countForThisRun=0;
timeout=memo.timeout | | 600;//在600时工作。在800时失败。
开始时间=新日期();
memo.stillTime=true;
collection=getContext().getCollection();
collectionLink=collection.getSelfLink();
memo.stillQueueing=true;
while(memo.remaining>0&&memo.stillqueue&&memo.stillTime){
行={
答:1,,
b:2
};
getContext().getResponse().setBody(备忘录);
memo.stillQueueing=collection.createDocument(collectionLink,行);
如果(备注:仍在排队){
备忘录.剩余--;
memo.countForThisRun++;
memo.totalCount++;
}
nowTime=新日期();
memo.nowTime=nowTime;
memo.startTime=startTime;
memo.stillTime=(nowTime-startTime)<超时;
如果(备忘录时间){
memo.continuation=null;
}否则{
memo.continuation='值不重要';
}
}
getContext().getResponse().setBody(备忘录);
返回备忘录;
};

上面的存储过程将在while循环中创建文档,直到API返回
false

请记住,
createDocument()
是一种异步方法。返回的布尔值表示此时是否结束执行。返回值不够“智能”,无法估计和说明异步调用将花费多少时间;因此,它不能用于在while()循环中将一组调用排队

因此,当布尔值返回
false
时,上面的存储过程不会正常终止,因为它有一组createDocument()调用仍在运行。最终的结果是超时(这最终导致重复尝试被列入黑名单)

简言之,避免这种模式:

while (stillQueueing) {
  stillQueueing = collection.createDocument(collectionLink, row);
}
相反,您应该对控制流使用回调。以下是重构后的代码:

function(memo) {
  var collection = getContext().getCollection();
  var collectionLink = collection.getSelfLink();
  var row = {
    a: 1,
    b: 2
  };

  if ((memo != null ? memo.remaining : void 0) == null) {
    throw new Error('generateData must be called with an object containing a `remaining` field.');
  }
  if (memo.totalCount == null) {
    memo.totalCount = 0;
  }
  memo.countForThisRun = 0;

  createMemo();

  function createMemo() {
    var isAccepted = collection.createDocument(collectionLink, row, function(err, createdDoc) {
      if (err) throw err;

      memo.remaining--;
      memo.countForThisRun++;
      memo.totalCount++;

      if (memo.remaining > 0) {
        createMemo();
      } else {
        getContext().getResponse().setBody(memo);
      }
    });

    if (!isAccepted) {
      getContext().getResponse().setBody(memo);
    }
  }
};

介意分享编译后的JavaScript吗?我想报告我添加了编译好的JavaScription事实上,我现在不确定我是否得到了一个false,但看看代码,如果我得到了false,存储过程将立即终止,它仍然被列入黑名单。我没有看到任何红旗。。。你能为备忘录分享你的输入样本吗?我想分析存储过程的执行情况。好的,现在我想问题是,我在大约5秒后得到一个网络超时。我没有改变默认值,在查看代码时,默认值似乎是60秒。node.js是否提供了另一个需要覆盖的超时?