Merge commit 'e2ae8dad02a7c0046e162e12bc0c0c077fff5d14'

Conflicts:
	td
	telegram-bot-api/ClientParameters.h
This commit is contained in:
Andrea Cavalli 2020-12-02 23:34:56 +01:00
commit 8f0f343203
10 changed files with 83 additions and 70 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR) cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)
project(TelegramBotApi VERSION 5.0 LANGUAGES CXX) project(TelegramBotApi VERSION 5.0.1 LANGUAGES CXX)
add_subdirectory(td EXCLUDE_FROM_ALL) add_subdirectory(td EXCLUDE_FROM_ALL)

2
td

@ -1 +1 @@
Subproject commit 3c59d917be8918878e3768ded083f92cb9aacbbe Subproject commit b47fab11cd7dddbb2829ef1830007632332b717b

View File

@ -1544,7 +1544,7 @@ void Client::JsonMessage::store(JsonValueScope *scope) const {
<< message_->chat_id << " while storing " << source_ << " " << message_->id; << message_->chat_id << " while storing " << source_ << " " << message_->id;
} }
} }
if (message_->media_album_id > 0) { if (message_->media_album_id != 0) {
object("media_group_id", td::to_string(message_->media_album_id)); object("media_group_id", td::to_string(message_->media_album_id));
} }
switch (message_->content->get_id()) { switch (message_->content->get_id()) {
@ -3293,17 +3293,22 @@ ServerBotInfo Client::get_bot_info() const {
auto &tqueue = parameters_->shared_data_->tqueue_; auto &tqueue = parameters_->shared_data_->tqueue_;
res.head_update_id_ = tqueue->get_head(tqueue_id_).value(); res.head_update_id_ = tqueue->get_head(tqueue_id_).value();
res.tail_update_id_ = tqueue->get_tail(tqueue_id_).value(); res.tail_update_id_ = tqueue->get_tail(tqueue_id_).value();
res.pending_update_count_ = tqueue->get_size(tqueue_id_);
res.webhook_max_connections_ = webhook_max_connections_; res.webhook_max_connections_ = webhook_max_connections_;
res.start_timestamp_ = start_timestamp_; res.pending_update_count_ = tqueue->get_size(tqueue_id_);
res.start_time_ = start_time_;
return res; return res;
} }
void Client::start_up() { void Client::start_up() {
start_timestamp_ = td::Time::now(); start_time_ = td::Time::now();
next_bot_updates_warning_date_ = start_timestamp_ + 600; next_bot_updates_warning_time_ = start_time_ + 600;
schedule_next_delete_messages_lru(); schedule_next_delete_messages_lru();
webhook_set_date_ = start_timestamp_; webhook_set_time_ = start_time_;
next_allowed_set_webhook_time_ = start_time_;
next_set_webhook_logging_time_ = start_time_;
next_webhook_is_not_modified_warning_time_ = start_time_;
previous_get_updates_start_time_ = start_time_ - 100;
previous_get_updates_finish_time_ = start_time_ - 100;
sticker_set_names_[GREAT_MINDS_SET_ID] = GREAT_MINDS_SET_NAME.str(); sticker_set_names_[GREAT_MINDS_SET_ID] = GREAT_MINDS_SET_NAME.str();
@ -3932,7 +3937,7 @@ void Client::on_update_authorization_state() {
parameters->api_hash_ = parameters_->api_hash_; parameters->api_hash_ = parameters_->api_hash_;
parameters->system_language_code_ = "en"; parameters->system_language_code_ = "en";
parameters->device_model_ = "server"; parameters->device_model_ = "server";
parameters->application_version_ = "5.0"; parameters->application_version_ = "5.0.1";
parameters->enable_storage_optimizer_ = true; parameters->enable_storage_optimizer_ = true;
parameters->ignore_file_names_ = true; parameters->ignore_file_names_ = true;
@ -6012,8 +6017,8 @@ void Client::on_cmd(PromisedQueryPtr query) {
LOG(DEBUG) << "Process query " << *query; LOG(DEBUG) << "Process query " << *query;
if (!td_client_.empty()) { if (!td_client_.empty()) {
if (query->method() == "close") { if (query->method() == "close") {
auto retry_after = static_cast<int>(10 * 60 - (td::Time::now() - start_timestamp_)); auto retry_after = static_cast<int>(10 * 60 - (td::Time::now() - start_time_));
if (retry_after > 0 && start_timestamp_ > parameters_->start_timestamp_ + 10 * 60) { if (retry_after > 0 && start_time_ > parameters_->start_time_ + 10 * 60) {
return query->set_retry_after_error(retry_after); return query->set_retry_after_error(retry_after);
} }
need_close_ = true; need_close_ = true;
@ -7340,11 +7345,11 @@ td::Status Client::process_get_updates_query(PromisedQueryPtr &query) {
update_allowed_update_types(query.get()); update_allowed_update_types(query.get());
auto now = td::Time::now_cached(); auto now = td::Time::now_cached();
if (offset == previous_get_updates_offset_ && timeout < 3 && now < previous_get_updates_start_date_ + 3.0) { if (offset == previous_get_updates_offset_ && timeout < 3 && now < previous_get_updates_start_time_ + 3.0) {
timeout = 3; timeout = 3;
} }
previous_get_updates_offset_ = offset; previous_get_updates_offset_ = offset;
previous_get_updates_start_date_ = now; previous_get_updates_start_time_ = now;
do_get_updates(offset, limit, timeout, std::move(query)); do_get_updates(offset, limit, timeout, std::move(query));
return Status::OK(); return Status::OK();
} }
@ -7357,15 +7362,15 @@ td::Status Client::process_set_webhook_query(PromisedQueryPtr &query) {
auto now = td::Time::now_cached(); auto now = td::Time::now_cached();
if (!new_url.empty()) { if (!new_url.empty()) {
if (now < next_allowed_set_webhook_date_) { if (now < next_allowed_set_webhook_time_) {
query->set_retry_after_error(1); query->set_retry_after_error(1);
return Status::OK(); return Status::OK();
} }
next_allowed_set_webhook_date_ = now + 1; next_allowed_set_webhook_time_ = now + 1;
} }
// do not send warning just after webhook was deleted or set // do not send warning just after webhook was deleted or set
next_bot_updates_warning_date_ = td::max(next_bot_updates_warning_date_, now + BOT_UPDATES_WARNING_DELAY); next_bot_updates_warning_time_ = td::max(next_bot_updates_warning_time_, now + BOT_UPDATES_WARNING_DELAY);
int32 new_max_connections = new_url.empty() ? 0 : get_webhook_max_connections(query.get()); int32 new_max_connections = new_url.empty() ? 0 : get_webhook_max_connections(query.get());
Slice new_ip_address = new_url.empty() ? Slice() : query->arg("ip_address"); Slice new_ip_address = new_url.empty() ? Slice() : query->arg("ip_address");
@ -7380,8 +7385,8 @@ td::Status Client::process_set_webhook_query(PromisedQueryPtr &query) {
(!new_fix_ip_address || new_ip_address == webhook_ip_address_) && !drop_pending_updates) { (!new_fix_ip_address || new_ip_address == webhook_ip_address_) && !drop_pending_updates) {
if (update_allowed_update_types(query.get())) { if (update_allowed_update_types(query.get())) {
save_webhook(); save_webhook();
} else if (now > next_webhook_is_not_modified_warning_date_) { } else if (now > next_webhook_is_not_modified_warning_time_) {
next_webhook_is_not_modified_warning_date_ = now + 300; next_webhook_is_not_modified_warning_time_ = now + 300;
LOG(WARNING) << "Webhook is not modified: \"" << new_url << '"'; LOG(WARNING) << "Webhook is not modified: \"" << new_url << '"';
} }
answer_query(td::JsonTrue(), std::move(query), answer_query(td::JsonTrue(), std::move(query),
@ -7389,8 +7394,8 @@ td::Status Client::process_set_webhook_query(PromisedQueryPtr &query) {
return Status::OK(); return Status::OK();
} }
if (now > next_set_webhook_logging_date_ || webhook_url_ != new_url) { if (now > next_set_webhook_logging_time_ || webhook_url_ != new_url) {
next_set_webhook_logging_date_ = now + 300; next_set_webhook_logging_time_ = now + 300;
LOG(WARNING) << "Set webhook to " << new_url << ", max_connections = " << new_max_connections LOG(WARNING) << "Set webhook to " << new_url << ", max_connections = " << new_max_connections
<< ", IP address = " << new_ip_address; << ", IP address = " << new_ip_address;
} }
@ -7633,7 +7638,7 @@ void Client::save_webhook() const {
} }
void Client::webhook_success() { void Client::webhook_success() {
next_bot_updates_warning_date_ = td::Time::now() + BOT_UPDATES_WARNING_DELAY; next_bot_updates_warning_time_ = td::Time::now() + BOT_UPDATES_WARNING_DELAY;
if (was_bot_updates_warning_) { if (was_bot_updates_warning_) {
send_request(make_object<td_api::setBotUpdatesStatus>(0, ""), std::make_unique<TdOnOkCallback>()); send_request(make_object<td_api::setBotUpdatesStatus>(0, ""), std::make_unique<TdOnOkCallback>());
was_bot_updates_warning_ = false; was_bot_updates_warning_ = false;
@ -7646,11 +7651,11 @@ void Client::webhook_error(Status status) {
last_webhook_error_ = std::move(status); last_webhook_error_ = std::move(status);
auto pending_update_count = get_pending_update_count(); auto pending_update_count = get_pending_update_count();
if (pending_update_count >= MIN_PENDING_UPDATES_WARNING && td::Time::now() > next_bot_updates_warning_date_) { if (pending_update_count >= MIN_PENDING_UPDATES_WARNING && td::Time::now() > next_bot_updates_warning_time_) {
send_request(make_object<td_api::setBotUpdatesStatus>(td::narrow_cast<int32>(pending_update_count), send_request(make_object<td_api::setBotUpdatesStatus>(td::narrow_cast<int32>(pending_update_count),
"Webhook error. " + last_webhook_error_.message().str()), "Webhook error. " + last_webhook_error_.message().str()),
std::make_unique<TdOnOkCallback>()); std::make_unique<TdOnOkCallback>());
next_bot_updates_warning_date_ = td::Time::now_cached() + BOT_UPDATES_WARNING_DELAY; next_bot_updates_warning_time_ = td::Time::now_cached() + BOT_UPDATES_WARNING_DELAY;
was_bot_updates_warning_ = true; was_bot_updates_warning_ = true;
} }
} }
@ -7667,7 +7672,7 @@ void Client::webhook_closed(Status status) {
webhook_max_connections_ = 0; webhook_max_connections_ = 0;
webhook_ip_address_ = td::string(); webhook_ip_address_ = td::string();
webhook_fix_ip_address_ = false; webhook_fix_ip_address_ = false;
webhook_set_date_ = td::Time::now(); webhook_set_time_ = td::Time::now();
last_webhook_error_date_ = 0; last_webhook_error_date_ = 0;
last_webhook_error_ = Status::OK(); last_webhook_error_ = Status::OK();
parameters_->shared_data_->webhook_db_->erase(bot_token_with_dc_); parameters_->shared_data_->webhook_db_->erase(bot_token_with_dc_);
@ -7737,7 +7742,7 @@ void Client::do_set_webhook(PromisedQueryPtr query, bool was_deleted) {
has_webhook_certificate_ = true; has_webhook_certificate_ = true;
} }
webhook_url_ = new_url.str(); webhook_url_ = new_url.str();
webhook_set_date_ = td::Time::now(); webhook_set_time_ = td::Time::now();
webhook_max_connections_ = get_webhook_max_connections(query.get()); webhook_max_connections_ = get_webhook_max_connections(query.get());
webhook_ip_address_ = query->arg("ip_address").str(); webhook_ip_address_ = query->arg("ip_address").str();
webhook_fix_ip_address_ = get_webhook_fix_ip_address(query.get()); webhook_fix_ip_address_ = get_webhook_fix_ip_address(query.get());
@ -7833,9 +7838,9 @@ void Client::abort_long_poll(bool from_set_webhook) {
void Client::fail_query_conflict(Slice message, PromisedQueryPtr &&query) { void Client::fail_query_conflict(Slice message, PromisedQueryPtr &&query) {
auto now = td::Time::now_cached(); auto now = td::Time::now_cached();
if (now >= next_conflict_response_date_) { if (now >= previous_get_updates_finish_time_) {
fail_query(409, message, std::move(query)); fail_query(409, message, std::move(query));
next_conflict_response_date_ = now + 3.0; previous_get_updates_finish_time_ = now + 3.0;
} else { } else {
td::create_actor<td::SleepActor>( td::create_actor<td::SleepActor>(
"FailQueryConflictSleepActor", 3.0, "FailQueryConflictSleepActor", 3.0,
@ -7944,7 +7949,7 @@ void Client::do_get_updates(int32 offset, int32 limit, int32 timeout, PromisedQu
return; return;
} }
previous_get_updates_finish_date_ = td::Clocks::system(); // local time to output it to the log previous_get_updates_finish_date_ = td::Clocks::system(); // local time to output it to the log
next_bot_updates_warning_date_ = td::Time::now() + BOT_UPDATES_WARNING_DELAY; next_bot_updates_warning_time_ = td::Time::now() + BOT_UPDATES_WARNING_DELAY;
if (total_size == updates.size() && was_bot_updates_warning_) { if (total_size == updates.size() && was_bot_updates_warning_) {
send_request(make_object<td_api::setBotUpdatesStatus>(0, ""), std::make_unique<TdOnOkCallback>()); send_request(make_object<td_api::setBotUpdatesStatus>(0, ""), std::make_unique<TdOnOkCallback>());
was_bot_updates_warning_ = false; was_bot_updates_warning_ = false;
@ -7955,11 +7960,11 @@ void Client::do_get_updates(int32 offset, int32 limit, int32 timeout, PromisedQu
void Client::long_poll_wakeup(bool force_flag) { void Client::long_poll_wakeup(bool force_flag) {
if (!long_poll_query_) { if (!long_poll_query_) {
auto pending_update_count = get_pending_update_count(); auto pending_update_count = get_pending_update_count();
if (pending_update_count >= MIN_PENDING_UPDATES_WARNING && td::Time::now() > next_bot_updates_warning_date_) { if (pending_update_count >= MIN_PENDING_UPDATES_WARNING && td::Time::now() > next_bot_updates_warning_time_) {
send_request(make_object<td_api::setBotUpdatesStatus>(td::narrow_cast<int32>(pending_update_count), send_request(make_object<td_api::setBotUpdatesStatus>(td::narrow_cast<int32>(pending_update_count),
"The getUpdates method is not called for too long"), "The getUpdates method is not called for too long"),
std::make_unique<TdOnOkCallback>()); std::make_unique<TdOnOkCallback>());
next_bot_updates_warning_date_ = next_bot_updates_warning_time_ =
td::Time::now_cached() + BOT_UPDATES_WARNING_DELAY; // do not send warnings too often td::Time::now_cached() + BOT_UPDATES_WARNING_DELAY; // do not send warnings too often
was_bot_updates_warning_ = true; was_bot_updates_warning_ = true;
} }
@ -8900,7 +8905,7 @@ void Client::process_new_message_queue(int64 chat_id) {
int32 message_date = message->edit_date_ == 0 ? message->date_ : message->edit_date_; int32 message_date = message->edit_date_ == 0 ? message->date_ : message->edit_date_;
auto now = get_unix_time(); auto now = get_unix_time();
auto update_delay_time = now - td::max(message_date, parameters_->shared_data_->get_unix_time(webhook_set_date_)); auto update_delay_time = now - td::max(message_date, parameters_->shared_data_->get_unix_time(webhook_set_time_));
const auto UPDATE_DELAY_WARNING_TIME = 10 * 60; const auto UPDATE_DELAY_WARNING_TIME = 10 * 60;
LOG_IF(ERROR, update_delay_time > UPDATE_DELAY_WARNING_TIME) LOG_IF(ERROR, update_delay_time > UPDATE_DELAY_WARNING_TIME)
<< "Receive very old update " << get_update_type_name(update_type) << " sent at " << message_date << " to chat " << "Receive very old update " << get_update_type_name(update_type) << " sent at " << message_date << " to chat "

View File

@ -830,7 +830,7 @@ class Client : public WebhookActor::Callback {
td::string bot_token_id_; td::string bot_token_id_;
bool is_test_dc_; bool is_test_dc_;
int64 tqueue_id_; int64 tqueue_id_;
double start_timestamp_ = 0; double start_time_ = 0;
int32 my_id_ = -1; int32 my_id_ = -1;
int32 authorization_date_ = -1; int32 authorization_date_ = -1;
@ -930,7 +930,7 @@ class Client : public WebhookActor::Callback {
PromisedQueryPtr long_poll_query_; PromisedQueryPtr long_poll_query_;
static constexpr int32 BOT_UPDATES_WARNING_DELAY = 30; static constexpr int32 BOT_UPDATES_WARNING_DELAY = 30;
double next_bot_updates_warning_date_ = 0; double next_bot_updates_warning_time_ = 0;
bool was_bot_updates_warning_ = false; bool was_bot_updates_warning_ = false;
td::uint32 allowed_update_types_ = DEFAULT_ALLOWED_UPDATE_TYPES; td::uint32 allowed_update_types_ = DEFAULT_ALLOWED_UPDATE_TYPES;
@ -941,23 +941,23 @@ class Client : public WebhookActor::Callback {
td::ActorOwn<WebhookActor> webhook_id_; td::ActorOwn<WebhookActor> webhook_id_;
PromisedQueryPtr webhook_set_query_; PromisedQueryPtr webhook_set_query_;
td::string webhook_url_; td::string webhook_url_;
double webhook_set_date_ = 0; double webhook_set_time_ = 0;
int32 webhook_max_connections_ = 0; int32 webhook_max_connections_ = 0;
td::string webhook_ip_address_; td::string webhook_ip_address_;
bool webhook_fix_ip_address_ = false; bool webhook_fix_ip_address_ = false;
int32 last_webhook_error_date_ = 0; int32 last_webhook_error_date_ = 0;
Status last_webhook_error_; Status last_webhook_error_;
double next_allowed_set_webhook_date_ = 0; double next_allowed_set_webhook_time_ = 0;
double next_set_webhook_logging_date_ = 0; double next_set_webhook_logging_time_ = 0;
double next_webhook_is_not_modified_warning_date_ = 0; double next_webhook_is_not_modified_warning_time_ = 0;
std::size_t last_pending_update_count_ = MIN_PENDING_UPDATES_WARNING; std::size_t last_pending_update_count_ = MIN_PENDING_UPDATES_WARNING;
double local_unix_time_difference_ = 0; // Unix time - now() double local_unix_time_difference_ = 0; // Unix time - now()
int32 previous_get_updates_offset_ = -1; int32 previous_get_updates_offset_ = -1;
double previous_get_updates_start_date_ = 0; double previous_get_updates_start_time_ = 0;
double previous_get_updates_finish_date_ = 0; double previous_get_updates_finish_date_ = 0;
double next_conflict_response_date_ = 0; double previous_get_updates_finish_time_ = 0;
td::uint64 webhook_generation_ = 1; td::uint64 webhook_generation_ = 1;

View File

@ -190,7 +190,7 @@ void ClientManager::get_stats(td::PromiseActor<td::BufferSlice> promise,
sb << stat_.get_description() << "\n"; sb << stat_.get_description() << "\n";
if (id_filter.empty()) { if (id_filter.empty()) {
sb << "uptime\t" << now - parameters_->start_timestamp_ << "\n"; sb << "uptime\t" << now - parameters_->start_time_ << "\n";
sb << "bot_count\t" << clients_.size() << "\n"; sb << "bot_count\t" << clients_.size() << "\n";
sb << "active_bot_count\t" << active_bot_count << "\n"; sb << "active_bot_count\t" << active_bot_count << "\n";
auto r_mem_stat = td::mem_stat(); auto r_mem_stat = td::mem_stat();
@ -227,7 +227,7 @@ void ClientManager::get_stats(td::PromiseActor<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();
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_timestamp_ << "\n"; sb << "uptime\t" << now - bot_info.start_time_ << "\n";
sb << "token\t" << bot_info.token_ << "\n"; sb << "token\t" << bot_info.token_ << "\n";
sb << "username\t" << bot_info.username_ << "\n"; sb << "username\t" << bot_info.username_ << "\n";
sb << "webhook\t" << bot_info.webhook_ << "\n"; sb << "webhook\t" << bot_info.webhook_ << "\n";

View File

@ -66,7 +66,7 @@ struct ClientParameters {
td::IPAddress webhook_proxy_ip_address_; td::IPAddress webhook_proxy_ip_address_;
td::uint32 max_batch_operations = 10000; td::uint32 max_batch_operations = 10000;
double start_timestamp_ = 0; double start_time_ = 0;
td::ActorId<td::GetHostByNameActor> get_host_by_name_actor_id_; td::ActorId<td::GetHostByNameActor> get_host_by_name_actor_id_;

View File

@ -86,8 +86,7 @@ class ServerBotInfo {
td::int32 tail_update_id_ = 0; td::int32 tail_update_id_ = 0;
td::int32 webhook_max_connections_ = 0; td::int32 webhook_max_connections_ = 0;
std::size_t pending_update_count_ = 0; std::size_t pending_update_count_ = 0;
double start_timestamp_ = 0; double start_time_ = 0;
double last_query_timestamp_ = 0;
}; };
struct ServerBotStat { struct ServerBotStat {

View File

@ -86,8 +86,8 @@ void WebhookActor::resolve_ip_address() {
if (fix_ip_address_) { if (fix_ip_address_) {
return; return;
} }
if (td::Time::now() < next_ip_address_resolve_timestamp_) { if (td::Time::now() < next_ip_address_resolve_time_) {
relax_wakeup_at(next_ip_address_resolve_timestamp_, "resolve_ip_address"); relax_wakeup_at(next_ip_address_resolve_time_, "resolve_ip_address");
return; return;
} }
@ -101,9 +101,9 @@ void WebhookActor::resolve_ip_address() {
} }
if (future_ip_address_.is_ready()) { if (future_ip_address_.is_ready()) {
next_ip_address_resolve_timestamp_ = next_ip_address_resolve_time_ =
td::Time::now() + IP_ADDRESS_CACHE_TIME + td::Random::fast(0, IP_ADDRESS_CACHE_TIME / 10); td::Time::now() + IP_ADDRESS_CACHE_TIME + td::Random::fast(0, IP_ADDRESS_CACHE_TIME / 10);
relax_wakeup_at(next_ip_address_resolve_timestamp_, "resolve_ip_address"); relax_wakeup_at(next_ip_address_resolve_time_, "resolve_ip_address");
auto r_ip_address = future_ip_address_.move_as_result(); auto r_ip_address = future_ip_address_.move_as_result();
if (r_ip_address.is_error()) { if (r_ip_address.is_error()) {
@ -264,7 +264,7 @@ void WebhookActor::create_new_connections() {
auto now = td::Time::now(); auto now = td::Time::now();
td::FloodControlFast *flood; td::FloodControlFast *flood;
bool active; bool active;
if (last_success_timestamp_ + 10 < now) { if (last_success_time_ + 10 < now) {
flood = &pending_new_connection_flood_; flood = &pending_new_connection_flood_;
if (need_connections > 1) { if (need_connections > 1) {
need_connections = 1; need_connections = 1;
@ -356,20 +356,21 @@ void WebhookActor::load_updates() {
} }
VLOG(webhook) << "Trying to load new updates from offset " << tqueue_offset_; VLOG(webhook) << "Trying to load new updates from offset " << tqueue_offset_;
auto offset = tqueue_offset_;
auto limit = td::min(SharedData::TQUEUE_EVENT_BUFFER_SIZE, max_loaded_updates_ - queue_updates_.size()); auto limit = td::min(SharedData::TQUEUE_EVENT_BUFFER_SIZE, max_loaded_updates_ - queue_updates_.size());
auto updates = mutable_span(parameters_->shared_data_->event_buffer_, limit); auto updates = mutable_span(parameters_->shared_data_->event_buffer_, limit);
auto now = td::Time::now(); auto now = td::Time::now();
auto unix_time_now = parameters_->shared_data_->get_unix_time(now); auto unix_time_now = parameters_->shared_data_->get_unix_time(now);
size_t total_size = 0; size_t total_size = 0;
if (tqueue_offset_.empty()) { if (offset.empty()) {
updates.truncate(0); updates.truncate(0);
} else { } else {
auto r_size = tqueue->get(tqueue_id_, tqueue_offset_, false, unix_time_now, updates); auto r_size = tqueue->get(tqueue_id_, offset, false, unix_time_now, updates);
if (r_size.is_error()) { if (r_size.is_error()) {
VLOG(webhook) << "Failed to get new updates: " << r_size.error(); VLOG(webhook) << "Failed to get new updates: " << r_size.error();
tqueue_offset_ = tqueue->get_head(tqueue_id_); offset = tqueue_offset_ = tqueue->get_head(tqueue_id_);
r_size = tqueue->get(tqueue_id_, tqueue_offset_, false, unix_time_now, updates); r_size = tqueue->get(tqueue_id_, offset, false, unix_time_now, updates);
r_size.ensure(); r_size.ensure();
} }
total_size = r_size.ok(); total_size = r_size.ok();
@ -420,8 +421,8 @@ void WebhookActor::load_updates() {
} }
} }
if (need_warning) { if (need_warning) {
LOG(WARNING) << "Found " << updates.size() << " updates out of " << total_size - update_map_.size() << " + " LOG(WARNING) << "Loaded " << updates.size() << " updates out of " << total_size << ". Have total of "
<< update_map_.size() << " in " << queues_.size() << " queues after last error \"" << update_map_.size() << " loaded in " << queues_.size() << " queues after last error \""
<< last_error_message_ << "\" at " << last_error_date_; << last_error_message_ << "\" at " << last_error_date_;
} }
@ -430,9 +431,9 @@ void WebhookActor::load_updates() {
} }
if (!updates.empty()) { if (!updates.empty()) {
VLOG(webhook) << "Loaded " << updates.size() << " new updates from offset " << tqueue_offset_ VLOG(webhook) << "Loaded " << updates.size() << " new updates from offset " << offset << " out of requested "
<< " out of requested " << limit << ". Have total of " << update_map_.size() << " updates loaded in " << limit << ". Have total of " << update_map_.size() << " updates loaded in " << queue_updates_.size()
<< queue_updates_.size() << " queues"; << " queues";
} }
} }
@ -458,7 +459,7 @@ void WebhookActor::drop_event(td::TQueue::EventId event_id) {
void WebhookActor::on_update_ok(td::TQueue::EventId event_id) { void WebhookActor::on_update_ok(td::TQueue::EventId event_id) {
last_update_was_successful_ = true; last_update_was_successful_ = true;
last_success_timestamp_ = td::Time::now(); last_success_time_ = td::Time::now();
VLOG(webhook) << "Receive ok for update " << event_id; VLOG(webhook) << "Receive ok for update " << event_id;
drop_event(event_id); drop_event(event_id);
@ -469,19 +470,24 @@ void WebhookActor::on_update_error(td::TQueue::EventId event_id, td::Slice error
double now = td::Time::now(); double now = td::Time::now();
auto it = update_map_.find(event_id); auto it = update_map_.find(event_id);
if (parameters_->shared_data_->get_unix_time(now) > it->second.expires_at_) {
const int MAX_RETRY_AFTER = 3600;
retry_after = td::clamp(retry_after, 0, MAX_RETRY_AFTER);
int next_delay = it->second.delay_;
int next_effective_delay = retry_after;
if (retry_after == 0 && it->second.fail_count_ > 0) {
next_delay = td::min(WEBHOOK_MAX_RESEND_TIMEOUT, next_delay * 2);
next_effective_delay = next_delay;
}
if (parameters_->shared_data_->get_unix_time(now) + next_effective_delay > it->second.expires_at_) {
LOG(WARNING) << "Drop update " << event_id << ": " << error; LOG(WARNING) << "Drop update " << event_id << ": " << error;
drop_event(event_id); drop_event(event_id);
return; return;
} }
auto &update = it->second; auto &update = it->second;
update.state_ = Update::State::Begin; update.state_ = Update::State::Begin;
const int MAX_RETRY_AFTER = 3600; update.delay_ = next_delay;
retry_after = td::clamp(retry_after, 0, MAX_RETRY_AFTER); update.wakeup_at_ = now + next_effective_delay;
update.delay_ = retry_after != 0
? retry_after
: td::min(WEBHOOK_MAX_RESEND_TIMEOUT, update.delay_ * (update.fail_count_ > 0 ? 2 : 1));
update.wakeup_at_ = update.fail_count_ > 0 ? now + update.delay_ : now;
update.fail_count_++; update.fail_count_++;
queues_.emplace(update.wakeup_at_, update.queue_id_); queues_.emplace(update.wakeup_at_, update.queue_id_);
VLOG(webhook) << "Delay update " << event_id << " for " << (update.wakeup_at_ - now) << " seconds because of " VLOG(webhook) << "Delay update " << event_id << " for " << (update.wakeup_at_ - now) << " seconds because of "
@ -647,7 +653,7 @@ void WebhookActor::handle(td::unique_ptr<td::HttpQuery> response) {
void WebhookActor::start_up() { void WebhookActor::start_up() {
max_loaded_updates_ = max_connections_ * 2; max_loaded_updates_ = max_connections_ * 2;
last_success_timestamp_ = td::Time::now(); next_ip_address_resolve_time_ = last_success_time_ = td::Time::now();
active_new_connection_flood_.add_limit(1, 10 * max_connections_); active_new_connection_flood_.add_limit(1, 10 * max_connections_);
active_new_connection_flood_.add_limit(5, 20 * max_connections_); active_new_connection_flood_.add_limit(5, 20 * max_connections_);

View File

@ -137,7 +137,7 @@ class WebhookActor : public td::HttpOutboundConnection::Callback {
td::IPAddress ip_address_; td::IPAddress ip_address_;
td::int32 ip_generation_ = 0; td::int32 ip_generation_ = 0;
double next_ip_address_resolve_timestamp_ = 0; double next_ip_address_resolve_time_ = 0;
td::FutureActor<td::IPAddress> future_ip_address_; td::FutureActor<td::IPAddress> future_ip_address_;
class Connection : public td::ListNode { class Connection : public td::ListNode {
@ -168,7 +168,7 @@ class WebhookActor : public td::HttpOutboundConnection::Callback {
td::ListNode ready_connections_; td::ListNode ready_connections_;
td::FloodControlFast active_new_connection_flood_; td::FloodControlFast active_new_connection_flood_;
td::FloodControlFast pending_new_connection_flood_; td::FloodControlFast pending_new_connection_flood_;
double last_success_timestamp_ = 0; double last_success_time_ = 0;
double wakeup_at_ = 0; double wakeup_at_ = 0;
bool last_update_was_successful_ = true; bool last_update_was_successful_ = true;

View File

@ -134,7 +134,7 @@ int main(int argc, char *argv[]) {
auto shared_data = std::make_shared<SharedData>(); auto shared_data = std::make_shared<SharedData>();
auto parameters = std::make_unique<ClientParameters>(); auto parameters = std::make_unique<ClientParameters>();
parameters->shared_data_ = shared_data; parameters->shared_data_ = shared_data;
parameters->start_timestamp_ = start_time; parameters->start_time_ = start_time;
auto net_query_stats = td::create_net_query_stats(); auto net_query_stats = td::create_net_query_stats();
parameters->net_query_stats_ = net_query_stats; parameters->net_query_stats_ = net_query_stats;
@ -472,6 +472,9 @@ int main(int argc, char *argv[]) {
double now = td::Time::now(); double now = td::Time::now();
if (now >= next_cron_time) { if (now >= next_cron_time) {
if (now >= next_cron_time + 1.0) {
next_cron_time = now;
}
next_cron_time += 1.0; next_cron_time += 1.0;
ServerCpuStat::update(now); ServerCpuStat::update(now);
} }