I'm working on a private browser extension which extracts some information from a web page and posts it into a Discord channel via a webhook.
(我正在开发一个专用的浏览器扩展程序,该扩展程序从网页中提取一些信息并将其通过Webhook发布到Discord频道中。)
The browser extension does evaluate the x-ratelimit-...
response headers to observe the rate limit restrictions.
(浏览器扩展确实评估x-ratelimit-...
响应标头以遵守速率限制限制。)
While doing a "spam test" it seems that the rate limit restrictions are respected correctly and everything is working so far.
(在进行“垃圾邮件测试”时,似乎正确地遵守了速率限制限制,到目前为止一切正常。)
However, every now and then I'm still getting rate limited after sending a stack of messages (15+) even though ratelimit-remaining
is > 0
.(但是,即使ratelimit-remaining
> 0
我仍然时不时地发送一堆消息(15+)后仍然受到速率的限制。)
To counter this I already stop when ratelimit-remaining
is 1
and also add an additional second to the ratelimit-reset
timestamp.
(为了解决这个问题,当ratelimit-remaining
为1
时,我已经停止了,并且还向ratelimit-reset
时间戳添加了额外的ratelimit-reset
。)
But this doesn't seem to help.(但这似乎无济于事。)
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 );
} );
}
It is also strange that the same request, which triggers the rate limit, has its x-ratelimit-remaining
response header at > 0.
(同样奇怪的是,触发速率限制的同一请求的x-ratelimit-remaining
响应头为> 0。)
0" referrerpolicy="no-referrer">
What do I miss here?
(我在这里想念什么?)
Where is my mistake?(我的错误在哪里?)
Do I need to take x-ratelimit-bucket
and x-ratelimit-reset-after
into account aswell?(我还需要考虑x-ratelimit-bucket
和x-ratelimit-reset-after
吗?)
ask by Mario Werner translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…