From 090e1e98e94cfb2bfcb09bc1762f135d00fa6865 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Sep 2025 19:40:30 +0300 Subject: [PATCH] Add global flood limit. --- telegram-bot-api/ClientManager.cpp | 17 ++++++++++++++++- telegram-bot-api/ClientManager.h | 2 ++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index 963f1d2..3df07e5 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -116,11 +116,20 @@ void ClientManager::send(PromisedQueryPtr query) { auto now = td::Time::now(); auto wakeup_at = flood_control.get_wakeup_at(); if (wakeup_at > now) { - LOG(INFO) << "Failed to create Client from IP address " << ip_address; + LOG(INFO) << "Failed to create Client from IP address " << ip_address << " with token"; return query->set_retry_after_error(static_cast(wakeup_at - now) + 1); } flood_control.add_event(now); } + if (is_global_flood_control_enabled_) { + auto now = td::Time::now(); + auto wakeup_at = global_flood_control_.get_wakeup_at(); + if (wakeup_at > now) { + LOG(WARNING) << "Failed to create Client with token " << token; + return query->set_retry_after_error(static_cast(wakeup_at - now) + 1); + } + global_flood_control_.add_event(now); + } auto tqueue_id = get_tqueue_id(user_id, query->is_test_dc()); if (active_client_count_.count(tqueue_id) != 0) { // return query->set_retry_after_error(1); @@ -552,6 +561,12 @@ void ClientManager::timeout_expired() { last_tqueue_deleted_events_ = tqueue_deleted_events_; } } + + if (!is_global_flood_control_enabled_ && !parameters_->local_mode_) { + is_global_flood_control_enabled_ = true; + global_flood_control_.add_limit(60, 1000); // 1000 in a minute + global_flood_control_.add_limit(60 * 60, 10000); // 10000 in an hour + } } void ClientManager::hangup_shared() { diff --git a/telegram-bot-api/ClientManager.h b/telegram-bot-api/ClientManager.h index d6e7d04..e853b48 100644 --- a/telegram-bot-api/ClientManager.h +++ b/telegram-bot-api/ClientManager.h @@ -66,6 +66,8 @@ class ClientManager final : public td::Actor { td::FlatHashMap token_to_id_; td::FlatHashMap flood_controls_; + td::FloodControlFast global_flood_control_; + bool is_global_flood_control_enabled_ = false; td::FlatHashMap active_client_count_; bool close_flag_ = false;