Fail flood-limited queries with a delay.

This commit is contained in:
levlam 2023-02-13 15:52:15 +03:00
parent 12c56318a8
commit 09c9db306a
2 changed files with 17 additions and 12 deletions

View File

@ -4195,18 +4195,15 @@ void Client::send(PromisedQueryPtr query) {
auto update_per_minute = static_cast<int64>(stat->get_minute_update_count(td::Time::now()) * 60); auto update_per_minute = static_cast<int64>(stat->get_minute_update_count(td::Time::now()) * 60);
if (stat->get_active_request_count() > 500 + update_per_minute) { if (stat->get_active_request_count() > 500 + update_per_minute) {
LOG(INFO) << "Fail a query, because there are too many active queries: " << *query; LOG(INFO) << "Fail a query, because there are too many active queries: " << *query;
flood_limited_query_count_++; return fail_query_flood_limit_exceeded(std::move(query));
return query->set_retry_after_error(60);
} }
if (stat->get_active_file_upload_bytes() > (static_cast<int64>(1) << 32) && !query->files().empty()) { if (stat->get_active_file_upload_bytes() > (static_cast<int64>(1) << 32) && !query->files().empty()) {
LOG(INFO) << "Fail a query, because the total size of active file uploads is too big: " << *query; LOG(INFO) << "Fail a query, because the total size of active file uploads is too big: " << *query;
flood_limited_query_count_++; return fail_query_flood_limit_exceeded(std::move(query));
return query->set_retry_after_error(60);
} }
if (stat->get_active_file_upload_count() > 100 + update_per_minute / 5 && !query->files().empty()) { if (stat->get_active_file_upload_count() > 100 + update_per_minute / 5 && !query->files().empty()) {
LOG(INFO) << "Fail a query, because there are too many active file uploads: " << *query; LOG(INFO) << "Fail a query, because there are too many active file uploads: " << *query;
flood_limited_query_count_++; return fail_query_flood_limit_exceeded(std::move(query));
return query->set_retry_after_error(60);
} }
} }
} }
@ -7580,8 +7577,7 @@ td::Status Client::process_send_message_query(PromisedQueryPtr &query) {
// fast path // fast path
auto it = yet_unsent_message_count_.find(r_chat_id.ok()); auto it = yet_unsent_message_count_.find(r_chat_id.ok());
if (it != yet_unsent_message_count_.end() && it->second >= MAX_CONCURRENTLY_SENT_CHAT_MESSAGES) { if (it != yet_unsent_message_count_.end() && it->second >= MAX_CONCURRENTLY_SENT_CHAT_MESSAGES) {
flood_limited_query_count_++; fail_query_flood_limit_exceeded(std::move(query));
query->set_retry_after_error(60);
return Status::OK(); return Status::OK();
} }
} }
@ -7889,8 +7885,7 @@ td::Status Client::process_send_media_group_query(PromisedQueryPtr &query) {
PromisedQueryPtr query) mutable { PromisedQueryPtr query) mutable {
auto &count = yet_unsent_message_count_[chat_id]; auto &count = yet_unsent_message_count_[chat_id];
if (count >= MAX_CONCURRENTLY_SENT_CHAT_MESSAGES) { if (count >= MAX_CONCURRENTLY_SENT_CHAT_MESSAGES) {
flood_limited_query_count_++; return fail_query_flood_limit_exceeded(std::move(query));
return query->set_retry_after_error(60);
} }
auto message_count = input_message_contents.size(); auto message_count = input_message_contents.size();
count += static_cast<int32>(message_count); count += static_cast<int32>(message_count);
@ -9527,8 +9522,7 @@ void Client::do_send_message(object_ptr<td_api::InputMessageContent> input_messa
int64 reply_to_message_id, PromisedQueryPtr query) mutable { int64 reply_to_message_id, PromisedQueryPtr query) mutable {
auto &count = yet_unsent_message_count_[chat_id]; auto &count = yet_unsent_message_count_[chat_id];
if (count >= MAX_CONCURRENTLY_SENT_CHAT_MESSAGES) { if (count >= MAX_CONCURRENTLY_SENT_CHAT_MESSAGES) {
flood_limited_query_count_++; return fail_query_flood_limit_exceeded(std::move(query));
return query->set_retry_after_error(60);
} }
count++; count++;
@ -9616,6 +9610,15 @@ void Client::fail_query_closing(PromisedQueryPtr &&query) {
} }
} }
void Client::fail_query_flood_limit_exceeded(PromisedQueryPtr &&query) {
flood_limited_query_count_++;
td::create_actor<td::SleepActor>(
"FailQueryFloodLimitExceededActor", 3.0,
td::PromiseCreator::lambda(
[query = std::move(query)](td::Result<td::Unit> result) mutable { query->set_retry_after_error(60); }))
.release();
}
Client::ClosingError Client::get_closing_error() { Client::ClosingError Client::get_closing_error() {
ClosingError result; ClosingError result;
result.retry_after = 0; result.retry_after = 0;

View File

@ -618,6 +618,8 @@ class Client final : public WebhookActor::Callback {
void fail_query_closing(PromisedQueryPtr &&query); void fail_query_closing(PromisedQueryPtr &&query);
void fail_query_flood_limit_exceeded(PromisedQueryPtr &&query);
void fail_query_conflict(Slice message, PromisedQueryPtr &&query); void fail_query_conflict(Slice message, PromisedQueryPtr &&query);
struct ClosingError { struct ClosingError {