From 834caf09bffd3c780628696ca517b96455cda732 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 18 Sep 2022 10:19:58 +0300 Subject: [PATCH] Fail queries immediately if there are too many active queries already. --- telegram-bot-api/Client.cpp | 18 +++++++++++++++++- telegram-bot-api/Client.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 00608f4..bfb52a4 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -287,6 +287,11 @@ bool Client::init_methods() { return true; } +bool Client::is_local_method(Slice method) { + return method == "close" || method == "logout" || method == "getme" || method == "getupdates" || + method == "setwebhook" || method == "deletewebhook" || method == "getwebhookinfo"; +} + class Client::JsonFile final : public Jsonable { public: JsonFile(const td_api::file *file, const Client *client, bool with_path) @@ -3895,6 +3900,17 @@ void Client::start_up() { void Client::send(PromisedQueryPtr query) { if (!query->is_internal()) { query->set_stat_actor(stat_actor_); + if (!parameters_->local_mode_ && !is_local_method(query->method())) { + const BotStatActor *stat = stat_actor_.get_actor_unsafe(); + if (stat->get_active_request_count() > 10000) { + LOG(INFO) << "Fail a query, because there are too many active queries: " << *query; + return query->set_retry_after_error(60); + } + if (stat->get_active_file_upload_bytes() > (static_cast(1) << 33) && !query->files().empty()) { + LOG(INFO) << "Fail a query, because there are too many active file uploads: " << *query; + return query->set_retry_after_error(60); + } + } } cmd_queue_.emplace(std::move(query)); loop(); @@ -10457,7 +10473,7 @@ td::int32 Client::as_client_message_id(int64 message_id) { } td::int64 Client::get_supergroup_chat_id(int64 supergroup_id) { - return static_cast(-1000000000000ll) - supergroup_id; + return static_cast(-1000000000000ll) - supergroup_id; } td::int64 Client::get_basic_group_chat_id(int64 basic_group_id) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index 6d0fc54..4a3fe09 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -471,6 +471,8 @@ class Client final : public WebhookActor::Callback { static bool init_methods(); + static bool is_local_method(Slice method); + void on_cmd(PromisedQueryPtr query); Status process_get_me_query(PromisedQueryPtr &query);