Use "time" for monotonic time variables.

This commit is contained in:
levlam 2020-11-21 17:38:11 +03:00
parent f419509029
commit 79134758a8
9 changed files with 50 additions and 51 deletions

2
td

@ -1 +1 @@
Subproject commit 0b751f36ba3cc3b636cfc828c62ba2052d2d59a1
Subproject commit 5cbf90e4a081567593247bd96adc4eec2e9156d8

View File

@ -3187,17 +3187,17 @@ ServerBotInfo Client::get_bot_info() const {
auto &tqueue = parameters_->shared_data_->tqueue_;
res.head_update_id_ = tqueue->get_head(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.start_timestamp_ = start_timestamp_;
res.pending_update_count_ = tqueue->get_size(tqueue_id_);
res.start_time_ = start_time_;
return res;
}
void Client::start_up() {
start_timestamp_ = td::Time::now();
next_bot_updates_warning_date_ = start_timestamp_ + 600;
start_time_ = td::Time::now();
next_bot_updates_warning_time_ = start_time_ + 600;
schedule_next_delete_messages_lru();
webhook_set_date_ = start_timestamp_;
webhook_set_time_ = start_time_;
sticker_set_names_[GREAT_MINDS_SET_ID] = GREAT_MINDS_SET_NAME.str();
@ -5875,8 +5875,8 @@ void Client::on_cmd(PromisedQueryPtr query) {
LOG(DEBUG) << "Process query " << *query;
if (!td_client_.empty()) {
if (query->method() == "close") {
auto retry_after = static_cast<int>(10 * 60 - (td::Time::now() - start_timestamp_));
if (retry_after > 0 && start_timestamp_ > parameters_->start_timestamp_ + 10 * 60) {
auto retry_after = static_cast<int>(10 * 60 - (td::Time::now() - start_time_));
if (retry_after > 0 && start_time_ > parameters_->start_time_ + 10 * 60) {
return query->set_retry_after_error(retry_after);
}
need_close_ = true;
@ -7178,11 +7178,11 @@ td::Status Client::process_get_updates_query(PromisedQueryPtr &query) {
update_allowed_update_types(query.get());
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;
}
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));
return Status::OK();
}
@ -7195,15 +7195,15 @@ td::Status Client::process_set_webhook_query(PromisedQueryPtr &query) {
auto now = td::Time::now_cached();
if (!new_url.empty()) {
if (now < next_allowed_set_webhook_date_) {
if (now < next_allowed_set_webhook_time_) {
query->set_retry_after_error(1);
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
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());
Slice new_ip_address = new_url.empty() ? Slice() : query->arg("ip_address");
@ -7218,8 +7218,8 @@ td::Status Client::process_set_webhook_query(PromisedQueryPtr &query) {
(!new_fix_ip_address || new_ip_address == webhook_ip_address_) && !drop_pending_updates) {
if (update_allowed_update_types(query.get())) {
save_webhook();
} else if (now > next_webhook_is_not_modified_warning_date_) {
next_webhook_is_not_modified_warning_date_ = now + 300;
} else if (now > next_webhook_is_not_modified_warning_time_) {
next_webhook_is_not_modified_warning_time_ = now + 300;
LOG(WARNING) << "Webhook is not modified: \"" << new_url << '"';
}
answer_query(td::JsonTrue(), std::move(query),
@ -7227,8 +7227,8 @@ td::Status Client::process_set_webhook_query(PromisedQueryPtr &query) {
return Status::OK();
}
if (now > next_set_webhook_logging_date_ || webhook_url_ != new_url) {
next_set_webhook_logging_date_ = now + 300;
if (now > next_set_webhook_logging_time_ || webhook_url_ != new_url) {
next_set_webhook_logging_time_ = now + 300;
LOG(WARNING) << "Set webhook to " << new_url << ", max_connections = " << new_max_connections
<< ", IP address = " << new_ip_address;
}
@ -7346,7 +7346,7 @@ void Client::save_webhook() const {
}
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_) {
send_request(make_object<td_api::setBotUpdatesStatus>(0, ""), std::make_unique<TdOnOkCallback>());
was_bot_updates_warning_ = false;
@ -7359,11 +7359,11 @@ void Client::webhook_error(Status status) {
last_webhook_error_ = std::move(status);
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),
"Webhook error. " + last_webhook_error_.message().str()),
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;
}
}
@ -7380,7 +7380,7 @@ void Client::webhook_closed(Status status) {
webhook_max_connections_ = 0;
webhook_ip_address_ = td::string();
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_ = Status::OK();
parameters_->shared_data_->webhook_db_->erase(bot_token_with_dc_);
@ -7450,7 +7450,7 @@ void Client::do_set_webhook(PromisedQueryPtr query, bool was_deleted) {
has_webhook_certificate_ = true;
}
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_ip_address_ = query->arg("ip_address").str();
webhook_fix_ip_address_ = get_webhook_fix_ip_address(query.get());
@ -7546,9 +7546,9 @@ void Client::abort_long_poll(bool from_set_webhook) {
void Client::fail_query_conflict(Slice message, PromisedQueryPtr &&query) {
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));
next_conflict_response_date_ = now + 3.0;
previous_get_updates_finish_time_ = now + 3.0;
} else {
td::create_actor<td::SleepActor>(
"FailQueryConflictSleepActor", 3.0,
@ -7657,7 +7657,7 @@ void Client::do_get_updates(int32 offset, int32 limit, int32 timeout, PromisedQu
return;
}
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_) {
send_request(make_object<td_api::setBotUpdatesStatus>(0, ""), std::make_unique<TdOnOkCallback>());
was_bot_updates_warning_ = false;
@ -7668,11 +7668,11 @@ void Client::do_get_updates(int32 offset, int32 limit, int32 timeout, PromisedQu
void Client::long_poll_wakeup(bool force_flag) {
if (!long_poll_query_) {
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),
"The getUpdates method is not called for too long"),
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
was_bot_updates_warning_ = true;
}
@ -8609,7 +8609,7 @@ void Client::process_new_message_queue(int64 chat_id) {
int32 message_date = message->edit_date_ == 0 ? message->date_ : message->edit_date_;
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;
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 "

View File

@ -796,7 +796,7 @@ class Client : public WebhookActor::Callback {
td::string bot_token_id_;
bool is_test_dc_;
int64 tqueue_id_;
double start_timestamp_ = 0;
double start_time_ = 0;
int32 my_id_ = -1;
int32 authorization_date_ = -1;
@ -896,7 +896,7 @@ class Client : public WebhookActor::Callback {
PromisedQueryPtr long_poll_query_;
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;
td::uint32 allowed_update_types_ = DEFAULT_ALLOWED_UPDATE_TYPES;
@ -907,23 +907,23 @@ class Client : public WebhookActor::Callback {
td::ActorOwn<WebhookActor> webhook_id_;
PromisedQueryPtr webhook_set_query_;
td::string webhook_url_;
double webhook_set_date_ = 0;
double webhook_set_time_ = 0;
int32 webhook_max_connections_ = 0;
td::string webhook_ip_address_;
bool webhook_fix_ip_address_ = false;
int32 last_webhook_error_date_ = 0;
Status last_webhook_error_;
double next_allowed_set_webhook_date_ = 0;
double next_set_webhook_logging_date_ = 0;
double next_webhook_is_not_modified_warning_date_ = 0;
double next_allowed_set_webhook_time_ = 0;
double next_set_webhook_logging_time_ = 0;
double next_webhook_is_not_modified_warning_time_ = 0;
std::size_t last_pending_update_count_ = MIN_PENDING_UPDATES_WARNING;
double local_unix_time_difference_ = 0; // Unix time - now()
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 next_conflict_response_date_ = 0;
double previous_get_updates_finish_time_ = 0;
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";
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 << "active_bot_count\t" << active_bot_count << "\n";
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();
sb << "\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 << "username\t" << bot_info.username_ << "\n";
sb << "webhook\t" << bot_info.webhook_ << "\n";

View File

@ -62,7 +62,7 @@ struct ClientParameters {
td::int32 default_max_webhook_connections_ = 0;
td::IPAddress webhook_proxy_ip_address_;
double start_timestamp_ = 0;
double start_time_ = 0;
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 webhook_max_connections_ = 0;
std::size_t pending_update_count_ = 0;
double start_timestamp_ = 0;
double last_query_timestamp_ = 0;
double start_time_ = 0;
};
struct ServerBotStat {

View File

@ -86,8 +86,8 @@ void WebhookActor::resolve_ip_address() {
if (fix_ip_address_) {
return;
}
if (td::Time::now() < next_ip_address_resolve_timestamp_) {
relax_wakeup_at(next_ip_address_resolve_timestamp_, "resolve_ip_address");
if (td::Time::now() < next_ip_address_resolve_time_) {
relax_wakeup_at(next_ip_address_resolve_time_, "resolve_ip_address");
return;
}
@ -101,9 +101,9 @@ void WebhookActor::resolve_ip_address() {
}
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);
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();
if (r_ip_address.is_error()) {
@ -264,7 +264,7 @@ void WebhookActor::create_new_connections() {
auto now = td::Time::now();
td::FloodControlFast *flood;
bool active;
if (last_success_timestamp_ + 10 < now) {
if (last_success_time_ + 10 < now) {
flood = &pending_new_connection_flood_;
if (need_connections > 1) {
need_connections = 1;
@ -458,7 +458,7 @@ void WebhookActor::drop_event(td::TQueue::EventId event_id) {
void WebhookActor::on_update_ok(td::TQueue::EventId event_id) {
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;
drop_event(event_id);
@ -647,7 +647,7 @@ void WebhookActor::handle(td::unique_ptr<td::HttpQuery> response) {
void WebhookActor::start_up() {
max_loaded_updates_ = max_connections_ * 2;
last_success_timestamp_ = td::Time::now();
last_success_time_ = td::Time::now();
active_new_connection_flood_.add_limit(1, 10 * 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::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_;
class Connection : public td::ListNode {
@ -168,7 +168,7 @@ class WebhookActor : public td::HttpOutboundConnection::Callback {
td::ListNode ready_connections_;
td::FloodControlFast active_new_connection_flood_;
td::FloodControlFast pending_new_connection_flood_;
double last_success_timestamp_ = 0;
double last_success_time_ = 0;
double wakeup_at_ = 0;
bool last_update_was_successful_ = true;

View File

@ -133,7 +133,7 @@ int main(int argc, char *argv[]) {
auto shared_data = std::make_shared<SharedData>();
auto parameters = std::make_unique<ClientParameters>();
parameters->shared_data_ = shared_data;
parameters->start_timestamp_ = start_time;
parameters->start_time_ = start_time;
auto net_query_stats = td::create_net_query_stats();
parameters->net_query_stats_ = net_query_stats;