Javascript 为什么我的程序超过了gmail api拉配额?
我已经设置了gmail api,这样我可以每10秒从收件箱中提取最新的5条消息。这是我的密码:Javascript 为什么我的程序超过了gmail api拉配额?,javascript,node.js,google-api,Javascript,Node.js,Google Api,我已经设置了gmail api,这样我可以每10秒从收件箱中提取最新的5条消息。这是我的密码: gmail.users.messages.list( { auth: auth, userId: 'me', maxResults: 5 }, function(err, response) { if (err) { console.log('The API returned an error: ' + err) return }
gmail.users.messages.list(
{ auth: auth, userId: 'me', maxResults: 5 },
function(err, response) {
if (err) {
console.log('The API returned an error: ' + err)
return
}
console.log('+ 5, Listing Messages')
response.data.messages.forEach(message => {
console.log('Got message id: ' + message['id'])
const id = message['id']
gmail.users.messages.get({ auth: auth, userId: 'me', id: id }, function(
err,
response
) {
if (err) {
console.log('The API returned an error: ' + err)
return
}
console.log('+ 5 Getting message')
const fromHeader = response['data'].payload.headers.filter(
header => header.name === 'From'
)[0]
if (fromHeader.value.includes('amarciniak@bcit.ca')) {
response.data.payload.parts.forEach(part => {
if (
part.filename !== '' &&
part.mimeType ===
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
) {
console.log('For Each Part')
gmail.users.messages.attachments.get(
{
userId: 'me',
messageId: id,
id: part.body.attachmentId,
},
(err, { data }) => {
if (err) {
return console.log('Error getting attachment: ' + err)
}
console.log('+ 5 Getting attachment')
const hash = crypto
.createHash('md5')
.update(data['data'])
.digest('hex')
const newFileName = `${fileFolderPath}/${hash}_${part.filename}`
const filesInFolder = fs.readdirSync(fileFolderPath)
if (!filesInFolder.includes(`${hash}_${part.filename}`)) {
fs.writeFile(
newFileName,
data['data'],
{ encoding: 'base64' },
err => {
if (err) {
console.log(err)
} else {
console.log(`Wrote File: ${newFileName}`)
}
}
)
} else {
console.log(
`No changes detected. File: ${newFileName} is already present in folder. `
)
}
}
)
}
})
}
})
})
}
)
}
我从gmail中得到一个邮件列表,然后对于所有5条邮件,我得到单独的邮件,然后对于每一条邮件,我得到有效负载部分,对于每一部分,我得到附件,并用它做一些事情。我的每个get请求使用5个“配额”点。根据gmail api文档,限制为250/秒。在这个代码片段中,我看不出我是如何每秒使用超过250个配额点的。最多我用的是5*5*5左右。大概125分。是不是因为这些请求一个接一个地发生得如此之快,而不是每100毫秒125点,这实际上等于1250点/秒,所以限制了我的速率?这就是它的工作原理吗?还是我的代码运行的频率比我想象的要高?每10秒轮询一次?你真的应该考虑一些类似的事情,这可能值得一看,这说明并发请求的数量是有限制的。您实际上同时发送了5个
get
请求,另外还发送了更多用于找到任何附件的请求。您可能应该一个接一个地执行get
:确保在启动下一个请求之前完成一个请求(可能使用异步管理库?),这是一个好主意。我现在推,现在还可以。我还将考虑到你所说的等待每个请求完成后再返回。每10秒轮询一次?你真的应该考虑一些类似的事情,这可能值得一看,这说明并发请求的数量是有限制的。您实际上同时发送了5个get
请求,另外还发送了更多用于找到任何附件的请求。您可能应该一个接一个地执行get
:确保在启动下一个请求之前完成一个请求(可能使用异步管理库?),这是一个好主意。我现在推,现在还可以。我还将考虑到你所说的等待每个请求完成后再回来。