Limit the number of simultaneously uploaded files.

This commit is contained in:
levlam 2022-11-14 15:35:43 +03:00
parent c833612414
commit ce6ddc74d7
5 changed files with 19 additions and 1 deletions

View File

@ -4067,6 +4067,10 @@ void Client::send(PromisedQueryPtr query) {
return query->set_retry_after_error(60); return query->set_retry_after_error(60);
} }
if (stat->get_active_file_upload_bytes() > (static_cast<int64>(1) << 33) && !query->files().empty()) { if (stat->get_active_file_upload_bytes() > (static_cast<int64>(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; LOG(INFO) << "Fail a query, because there are too many active file uploads: " << *query;
return query->set_retry_after_error(60); return query->set_retry_after_error(60);
} }

View File

@ -252,6 +252,7 @@ void ClientManager::get_stats(td::Promise<td::BufferSlice> promise,
auto bot_info = client_info->client_.get_actor_unsafe()->get_bot_info(); 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_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_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 << '\n';
sb << "id\t" << bot_info.id_ << '\n'; sb << "id\t" << bot_info.id_ << '\n';
sb << "uptime\t" << now - bot_info.start_time_ << '\n'; sb << "uptime\t" << now - bot_info.start_time_ << '\n';
@ -263,6 +264,9 @@ void ClientManager::get_stats(td::Promise<td::BufferSlice> promise,
if (active_file_upload_bytes != 0) { if (active_file_upload_bytes != 0) {
sb << "active_file_upload_bytes\t" << active_file_upload_bytes << '\n'; 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()) { if (!bot_info.webhook_.empty()) {
sb << "webhook\t" << bot_info.webhook_ << '\n'; sb << "webhook\t" << bot_info.webhook_ << '\n';
if (bot_info.has_webhook_certificate_) { if (bot_info.has_webhook_certificate_) {

View File

@ -135,7 +135,7 @@ void Query::send_response_stat() const {
return; return;
} }
send_closure(stat_actor_, &BotStatActor::add_event<ServerBotStat::Response>, send_closure(stat_actor_, &BotStatActor::add_event<ServerBotStat::Response>,
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 } // namespace telegram_bot_api

View File

@ -168,6 +168,10 @@ td::int64 BotStatActor::get_active_file_upload_bytes() const {
return active_file_upload_bytes_; 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 { bool BotStatActor::is_active(double now) const {
return last_activity_timestamp_ > now - 86400; return last_activity_timestamp_ > now - 86400;
} }

View File

@ -115,6 +115,7 @@ struct ServerBotStat {
struct Response { struct Response {
bool ok_; bool ok_;
size_t size_; size_t size_;
td::int64 file_count_;
td::int64 files_size_; td::int64 files_size_;
}; };
void on_event(const Response &response) { 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_bytes() const;
td::int64 get_active_file_upload_count() const;
bool is_active(double now) const; bool is_active(double now) const;
private: private:
@ -201,12 +204,14 @@ class BotStatActor final : public td::Actor {
double last_activity_timestamp_ = -1e9; double last_activity_timestamp_ = -1e9;
td::int64 active_request_count_ = 0; td::int64 active_request_count_ = 0;
td::int64 active_file_upload_bytes_ = 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::Update &update) {
} }
void on_event(const ServerBotStat::Response &response) { void on_event(const ServerBotStat::Response &response) {
active_request_count_--; active_request_count_--;
active_file_upload_count_ -= response.file_count_;
active_file_upload_bytes_ -= response.files_size_; active_file_upload_bytes_ -= response.files_size_;
CHECK(active_request_count_ >= 0); CHECK(active_request_count_ >= 0);
CHECK(active_file_upload_bytes_ >= 0); CHECK(active_file_upload_bytes_ >= 0);
@ -214,6 +219,7 @@ class BotStatActor final : public td::Actor {
void on_event(const ServerBotStat::Request &request) { void on_event(const ServerBotStat::Request &request) {
active_request_count_++; active_request_count_++;
active_file_upload_count_ += request.file_count_;
active_file_upload_bytes_ += request.files_size_; active_file_upload_bytes_ += request.files_size_;
} }
}; };