node.js - Watson Assistant 日志错误“超出速率限制”
问题描述
我在获取 Watson Assistant 日志方面需要您的帮助。我收到错误“超出速率限制”。根据 API 文档,我们可以在参数中指定光标。当指定光标时,我们可以每分钟发出 120 个请求。如果没有指定游标,那么我们只能每 30 分钟发出 40 个请求。当它第一次启动时,我已将“param”对象中的光标属性作为空字符串传递,然后我将光标值更新为由 Watson 返回的 next_cursor 值(作为标记的分页属性)。但是,它仍然发出 40 个请求并且连接关闭 30 分钟。你能告诉我我做错了什么吗?我能够在文本文件中检索 40 条日志。我正在使用节点 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"
}
}
解决方案
我认为这可能与递归有关,并且 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();
推荐阅读
- karel - 如何让卡雷尔到处放蜂鸣器?
- algorithm - 找出有效的序列
- python - 运行SQL,进不去
- ios - 如何按数字顺序对一对多关系中的项目进行排序?
- javascript - 由于另一个函数,我无法执行函数
- python - 提取 Instagram 评论时,NoneType 对象不可下标
- javascript - 为什么我的按钮功能只提供一次输出?
- jmeter - jmeter中的Beanshell预处理器出现错误
- node.js - 如何将我的本地 node.js 应用程序连接到我的 Heroku Postgres 数据库?
- firebase - 如何在颤动中从 Firestore 中检索每个文档的文档 ID