Keep last time when a file was uploaded.

This commit is contained in:
levlam 2023-07-06 14:47:31 +03:00
parent 84e512c2e4
commit a9a0140476
3 changed files with 74 additions and 4 deletions

View File

@ -10028,7 +10028,64 @@ void Client::finish_set_webhook(PromisedQueryPtr query) {
webhook_set_query_ = std::move(query);
}
void Client::do_send_message(object_ptr<td_api::InputMessageContent> input_message_content, PromisedQueryPtr query) {
void Client::delete_last_send_message_time(td::int64 file_size, double max_delay) {
auto last_send_message_time = last_send_message_time_.get(file_size);
if (last_send_message_time == 0.0) {
return;
}
if (last_send_message_time < td::Time::now() - max_delay) {
LOG(DEBUG) << "Clear last send message time for size " << file_size;
last_send_message_time_.erase(file_size);
}
}
void Client::do_send_message(object_ptr<td_api::InputMessageContent> input_message_content, PromisedQueryPtr query,
bool force) {
if (!parameters_->local_mode_) {
if (!force) {
auto file_size = query->files_size();
if (file_size > 100000) {
auto &last_send_message_time = last_send_message_time_[file_size];
auto now = td::Time::now();
auto min_delay = td::clamp(static_cast<double>(file_size) * 1e-7, 0.1, 0.5);
auto max_bucket_volume = 1.0;
if (last_send_message_time > now + 5.0) {
return fail_query_flood_limit_exceeded(std::move(query));
}
last_send_message_time = td::max(last_send_message_time + min_delay, now - max_bucket_volume);
LOG(DEBUG) << "Query with files of size " << file_size << " can be processed in "
<< last_send_message_time - now << " seconds";
td::create_actor<td::SleepActor>(
"DeleteLastSendMessageTimeSleepActor", last_send_message_time + min_delay - (now - max_bucket_volume),
td::PromiseCreator::lambda([actor_id = actor_id(this), file_size,
max_delay = max_bucket_volume + min_delay](td::Result<td::Unit>) mutable {
send_closure(actor_id, &Client::delete_last_send_message_time, file_size, max_delay);
}))
.release();
if (last_send_message_time > now) {
td::create_actor<td::SleepActor>(
"DoSendMessageSleepActor", last_send_message_time - now,
td::PromiseCreator::lambda([actor_id = actor_id(this),
input_message_content = std::move(input_message_content),
query = std::move(query)](td::Result<td::Unit>) mutable {
send_closure(actor_id, &Client::do_send_message, std::move(input_message_content), std::move(query),
true);
}))
.release();
return;
}
}
} else {
if (logging_out_ || closing_) {
return fail_query_closing(std::move(query));
}
CHECK(was_authorized_);
}
}
auto chat_id = query->arg("chat_id");
auto message_thread_id = get_message_id(query.get(), "message_thread_id");
auto reply_to_message_id = get_message_id(query.get(), "reply_to_message_id");

View File

@ -639,7 +639,10 @@ class Client final : public WebhookActor::Callback {
void on_webhook_closed(td::Status status);
void do_send_message(object_ptr<td_api::InputMessageContent> input_message_content, PromisedQueryPtr query);
void delete_last_send_message_time(td::int64 file_size, double max_delay);
void do_send_message(object_ptr<td_api::InputMessageContent> input_message_content, PromisedQueryPtr query,
bool force = false);
int64 get_send_message_query_id(PromisedQueryPtr query, bool is_multisend);
@ -1082,6 +1085,8 @@ class Client final : public WebhookActor::Callback {
td::WaitFreeHashMap<int64, td::string> sticker_set_names_;
td::WaitFreeHashMap<int64, double> last_send_message_time_;
struct BotUserIds {
int64 default_bot_user_id_ = 0;
int64 cur_temp_bot_user_id_ = 1;

View File

@ -38,38 +38,48 @@ class Query final : public td::ListNode {
td::Slice token() const {
return token_;
}
bool is_test_dc() const {
return is_test_dc_;
}
td::Slice method() const {
return method_;
}
bool has_arg(td::Slice key) const {
auto it = std::find_if(args_.begin(), args_.end(),
[&key](const std::pair<td::MutableSlice, td::MutableSlice> &s) { return s.first == key; });
return it != args_.end();
}
td::MutableSlice arg(td::Slice key) const {
auto it = std::find_if(args_.begin(), args_.end(),
[&key](const std::pair<td::MutableSlice, td::MutableSlice> &s) { return s.first == key; });
return it == args_.end() ? td::MutableSlice() : it->second;
}
const td::vector<std::pair<td::MutableSlice, td::MutableSlice>> &args() const {
return args_;
}
td::Slice get_header(td::Slice key) const {
auto it = std::find_if(headers_.begin(), headers_.end(),
[&key](const std::pair<td::MutableSlice, td::MutableSlice> &s) { return s.first == key; });
return it == headers_.end() ? td::Slice() : it->second;
}
const td::HttpFile *file(td::Slice key) const {
auto it = std::find_if(files_.begin(), files_.end(), [&key](const td::HttpFile &f) { return f.field_name == key; });
return it == files_.end() ? nullptr : &*it;
}
const td::vector<td::HttpFile> &files() const {
return files_;
}
td::int64 files_size() const;
td::string get_peer_ip_address() const;
td::BufferSlice &answer() {
@ -152,8 +162,6 @@ class Query final : public td::ListNode {
td::int64 query_size() const;
td::int64 files_size() const;
td::int64 files_max_size() const;
void send_request_stat() const;