Watson Assistant日志错误“超出了速率限制”

时间:2019-05-28 16:04:19

标签: node.js ibm-watson watson-assistant

在获取Watson Assistant日志时,我需要您的帮助。我收到一个错误“超出速率限制”。根据API文档,我们可以在param中指定游标。当指定了游标时,我们可以每分钟120个请求。如果未指定游标,那么我们只能发出40个请求/ 30分钟。首次启动时,我已将游标属性以空字符串形式传递给“参数”对象,然后将游标值更新为Watson返回的next_cursor值(作为标记的分页属性)。但是,它仍然发出40个请求,并且连接关闭了30分钟。你能告诉我我在做什么错吗?我能够在文本文件中检索40条日志。我正在使用Node JS。我期待着您的回音。

下面是我创建的小样本函数:

const getLogs = () => {
           const params = {
             workspace_id: '50f6598a-a369-45df-9cfb-20bdfe617066',
             cursor: ""
           }
           service.listLogs(params)
           .then(res => {
             if(res.pagination.next_cursor) {
               fs.writeFile("temp.txt", JSON.stringify(res, null, 2), err => {
                 if (err) console.log(err);
                 console.log("Successfully Written to File.");
               });
               params.cursor = res.pagination.next_cursor;
               getLogs()
             } else {
               console.log('no more logs')
             }
           })
           .catch(err => console.log(JSON.stringify(err, null, 2)));
         }
         getLogs();

{
  "name": "Too Many Requests",
  "code": 429,
  "message": "Rate limit exceeded",
  "body": "{\"error\":\"Rate limit exceeded\",\"code\":429}",
  "headers": {
    "x-backside-transport": "FAIL FAIL",
    "content-type": "application/json; charset=utf-8",
    "access-control-allow-origin": "*",
    "access-control-allow-methods": "GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS",
    "access-control-allow-headers": "Content-Type, Content-Length, Authorization, X-Watson-Authorization-Token, X-WDC-PL-OPT-OUT, X-Watson-UserInfo, X-Watson-Learning-Opt-Out, X-Watson-Metadata",
    "access-control-max-age": "3600",
    "content-security-policy": "default-src 'none'",
    "x-dns-prefetch-control": "off",
    "x-frame-options": "SAMEORIGIN",
    "strict-transport-security": "max-age=31536000;",
    "x-download-options": "noopen",
    "x-content-type-options": "nosniff",
    "x-xss-protection": "1; mode=block",
    "x-ratelimit-limit": "40",
    "x-ratelimit-reset": "1559060571",
    "x-ratelimit-remaining": "0",
    "retry-after": "1143.36",
    "x-global-transaction-id": "7ecac92c5ced5be3440b2991",
    "x-dp-watson-tran-id": "gateway01-1141582225",
    "x-dp-transit-id": "gateway01-1141582225",
    "content-length": "42",
    "x-edgeconnect-midmile-rtt": "256",
    "x-edgeconnect-origin-mex-latency": "210",
    "date": "Tue, 28 May 2019 16:03:47 GMT",
    "connection": "close"
  }
}

1 个答案:

答案 0 :(得分:0)

我认为这可能与递归相关,并且在每次递归开始时将params.cursor设置为“”。您需要做的是将递归与闭包结合起来。因此您的代码将类似于:

 const getLogs = () => {
   const params = {
        workspace_id: '50f6598a-a369-45df-9cfb-20bdfe617066',
        cursor: ""
    }
   function doGetLogs() {
     service.listLogs(params)
     .then(res => {
        if(res.pagination.next_cursor) {
                ...
                params.cursor = res.pagination.next_cursor;
                doGetLogs()
              } 
              ...
            })
           ...
   }
   return doGetLogs();
 }

 getLogs();