我正在使用一个专用的浏览器扩展程序,该扩展程序从网页中提取一些信息并将其通过Webhook发布到Discord频道中。
浏览器扩展确实评估x-ratelimit-...
响应标头以遵守速率限制限制。
在进行“垃圾邮件测试”时,似乎正确地遵守了速率限制限制,到目前为止一切正常。但是,即使ratelimit-remaining
大于0
,在发送一堆消息(15+)之后,我仍然时不时地受到速率的限制。
为了解决这个问题,我已经在ratelimit-remaining
为1
时停止了,并且还为ratelimit-reset
时间戳添加了额外的秒数。但这似乎无济于事。
let rateLimitRemaining = 5;
let rateLimitReset = 0;
function sendContent()
{
if ( contentQueue.length > 0 )
{
console.log( "Messages in content queue: " + contentQueue.length );
let content = contentQueue[ 0 ];
let dateTimestamp = getCurrentUTCTimestamp();
// Don't send if remaining rate limit is <= 1 and current UTC time is less than reset timestamp
if ( rateLimitRemaining <= 1 && dateTimestamp <= rateLimitReset )
return;
contentQueue.shift();
let url = "...";
sendMessage( content, url );
}
}
function sendMessage( content, url )
{
let payload = JSON.stringify( { "content": content } );
$.ajax(
{
contentType: 'application/json',
method: "POST",
url: url,
data: payload,
dataType: 'json'
} ).done( function( response, status, jqXHR )
{
rateLimitRemaining = parseInt( jqXHR.getResponseHeader( 'x-ratelimit-remaining' ) );
// Add an additional second to the reset timestamp
rateLimitReset = parseInt( jqXHR.getResponseHeader( 'x-ratelimit-reset' ) ) + 1;
let timeToResetRemaining = rateLimitReset - getCurrentUTCTimestamp();
console.log( '[' + getCurrentDateTime() + '] Content sent to webhook. Remaining until rate limit: ' + rateLimitRemaining + ' / Reset @ ' + rateLimitReset + ' (' + getCurrentUTCTimestamp() + ') (' + timeToResetRemaining + ')' );
} ).fail( function( jqXHR, status, error )
{
let response = jqXHR.responseJSON;
// If we got rate limited, respect the retry_after delay
if ( response.hasOwnProperty( 'message' ) && response.message.indexOf( 'rate limited' ) !== 0 )
{
rateLimitRemaining = 0;
rateLimitReset = getCurrentUTCTimestamp() + Math.ceil( response.retry_after / 1000 ) + 1;
}
console.log( '[' + getCurrentDateTime() + '] Error sending request to webhook.' );
console.log( response );
} );
}
奇怪的是,触发速率限制的同一请求的x-ratelimit-remaining
响应标头为> 0。
我在这里想念什么?我的错误在哪里?我还需要考虑x-ratelimit-bucket
和x-ratelimit-reset-after
吗?