diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 43621db..cf35284 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -4067,6 +4067,10 @@ void Client::send(PromisedQueryPtr 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 the total size of active file uploads is too big: " << *query; + return query->set_retry_after_error(60); + } + if (stat->get_active_file_upload_count() > 500 && !query->files().empty()) { LOG(INFO) << "Fail a query, because there are too many active file uploads: " << *query; return query->set_retry_after_error(60); } diff --git a/telegram-bot-api/ClientManager.cpp b/telegram-bot-api/ClientManager.cpp index 2c44e6a..01273d3 100644 --- a/telegram-bot-api/ClientManager.cpp +++ b/telegram-bot-api/ClientManager.cpp @@ -252,6 +252,7 @@ void ClientManager::get_stats(td::Promise promise, auto bot_info = client_info->client_.get_actor_unsafe()->get_bot_info(); auto active_request_count = client_info->stat_.get_active_request_count(); auto active_file_upload_bytes = client_info->stat_.get_active_file_upload_bytes(); + auto active_file_upload_count = client_info->stat_.get_active_file_upload_count(); sb << '\n'; sb << "id\t" << bot_info.id_ << '\n'; sb << "uptime\t" << now - bot_info.start_time_ << '\n'; @@ -263,6 +264,9 @@ void ClientManager::get_stats(td::Promise promise, if (active_file_upload_bytes != 0) { sb << "active_file_upload_bytes\t" << active_file_upload_bytes << '\n'; } + if (active_file_upload_count != 0) { + sb << "active_file_upload_count\t" << active_file_upload_count << '\n'; + } if (!bot_info.webhook_.empty()) { sb << "webhook\t" << bot_info.webhook_ << '\n'; if (bot_info.has_webhook_certificate_) { diff --git a/telegram-bot-api/Query.cpp b/telegram-bot-api/Query.cpp index e917ba3..1d2d7e9 100644 --- a/telegram-bot-api/Query.cpp +++ b/telegram-bot-api/Query.cpp @@ -135,7 +135,7 @@ void Query::send_response_stat() const { return; } send_closure(stat_actor_, &BotStatActor::add_event, - ServerBotStat::Response{state_ == State::OK, answer_.size(), files_size()}, now); + ServerBotStat::Response{state_ == State::OK, answer_.size(), file_count(), files_size()}, now); } } // namespace telegram_bot_api diff --git a/telegram-bot-api/Stats.cpp b/telegram-bot-api/Stats.cpp index 54a6236..b7e3c38 100644 --- a/telegram-bot-api/Stats.cpp +++ b/telegram-bot-api/Stats.cpp @@ -168,6 +168,10 @@ td::int64 BotStatActor::get_active_file_upload_bytes() const { return active_file_upload_bytes_; } +td::int64 BotStatActor::get_active_file_upload_count() const { + return active_file_upload_count_; +} + bool BotStatActor::is_active(double now) const { return last_activity_timestamp_ > now - 86400; } diff --git a/telegram-bot-api/Stats.h b/telegram-bot-api/Stats.h index a4d7a07..c4f2487 100644 --- a/telegram-bot-api/Stats.h +++ b/telegram-bot-api/Stats.h @@ -115,6 +115,7 @@ struct ServerBotStat { struct Response { bool ok_; size_t size_; + td::int64 file_count_; td::int64 files_size_; }; void on_event(const Response &response) { @@ -189,6 +190,8 @@ class BotStatActor final : public td::Actor { td::int64 get_active_file_upload_bytes() const; + td::int64 get_active_file_upload_count() const; + bool is_active(double now) const; private: @@ -201,12 +204,14 @@ class BotStatActor final : public td::Actor { double last_activity_timestamp_ = -1e9; td::int64 active_request_count_ = 0; td::int64 active_file_upload_bytes_ = 0; + td::int64 active_file_upload_count_ = 0; void on_event(const ServerBotStat::Update &update) { } void on_event(const ServerBotStat::Response &response) { active_request_count_--; + active_file_upload_count_ -= response.file_count_; active_file_upload_bytes_ -= response.files_size_; CHECK(active_request_count_ >= 0); CHECK(active_file_upload_bytes_ >= 0); @@ -214,6 +219,7 @@ class BotStatActor final : public td::Actor { void on_event(const ServerBotStat::Request &request) { active_request_count_++; + active_file_upload_count_ += request.file_count_; active_file_upload_bytes_ += request.files_size_; } };