From 49f8b1e14cd297165661f17c643fbfa295edd416 Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 28 Aug 2022 00:49:56 +0300 Subject: [PATCH] Randomize ping delay for different connections. --- td/mtproto/SessionConnection.cpp | 19 +++++++++++-------- td/mtproto/SessionConnection.h | 26 +++++++++++++------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/td/mtproto/SessionConnection.cpp b/td/mtproto/SessionConnection.cpp index d6722f264..eb6b35966 100644 --- a/td/mtproto/SessionConnection.cpp +++ b/td/mtproto/SessionConnection.cpp @@ -720,11 +720,13 @@ void SessionConnection::on_read(size_t size) { } SessionConnection::SessionConnection(Mode mode, unique_ptr raw_connection, AuthData *auth_data) - : raw_connection_(std::move(raw_connection)), auth_data_(auth_data) { + : random_delay_(Random::fast(0, 5000000) * 1e-6) + , state_(Init) + , mode_(mode) + , created_at_(Time::now()) + , raw_connection_(std::move(raw_connection)) + , auth_data_(auth_data) { CHECK(raw_connection_); - state_ = Init; - mode_ = mode; - created_at_ = Time::now(); } PollableFdInfo &SessionConnection::get_poll_info() { @@ -969,10 +971,11 @@ void SessionConnection::flush_packet() { { // LOG(ERROR) << (auth_data_->get_header().empty() ? '-' : '+'); uint64 parent_message_id = 0; - auto storer = PacketStorer( - queries, auth_data_->get_header(), std::move(to_ack), ping_id, ping_disconnect_delay() + 2, max_delay, - max_after, max_wait, future_salt_n, to_get_state_info, to_resend_answer, to_cancel_answer, destroy_auth_key, - auth_data_, &container_id, &get_state_info_id, &resend_answer_id, &ping_message_id, &parent_message_id); + auto storer = PacketStorer(queries, auth_data_->get_header(), std::move(to_ack), ping_id, + static_cast(ping_disconnect_delay() + 2.0), max_delay, max_after, + max_wait, future_salt_n, to_get_state_info, to_resend_answer, + to_cancel_answer, destroy_auth_key, auth_data_, &container_id, + &get_state_info_id, &resend_answer_id, &ping_message_id, &parent_message_id); auto quick_ack_token = use_quick_ack ? parent_message_id : 0; send_crypto(storer, quick_ack_token); diff --git a/td/mtproto/SessionConnection.h b/td/mtproto/SessionConnection.h index 0b2e75555..1ba2d3ba6 100644 --- a/td/mtproto/SessionConnection.h +++ b/td/mtproto/SessionConnection.h @@ -139,32 +139,31 @@ class SessionConnection final bool is_main_ = false; bool was_moved_ = false; - int rtt() const { - return max(2, static_cast(raw_connection_->extra().rtt * 1.5 + 1)); + double rtt() const { + return max(2.0, raw_connection_->extra().rtt * 1.5 + 1); } - int32 read_disconnect_delay() const { - return online_flag_ ? rtt() * 7 / 2 : 135; + double read_disconnect_delay() const { + return online_flag_ ? rtt() * 3.5 : 135 + random_delay_; } - int32 ping_disconnect_delay() const { - return (online_flag_ && is_main_) ? rtt() * 5 / 2 : 135; + double ping_disconnect_delay() const { + return online_flag_ && is_main_ ? rtt() * 2.5 : 135 + random_delay_; } - int32 ping_may_delay() const { - return online_flag_ ? rtt() / 2 : 30; + double ping_may_delay() const { + return online_flag_ ? rtt() * 0.5 : 30 + random_delay_; } - int32 ping_must_delay() const { - return online_flag_ ? rtt() : 60; + double ping_must_delay() const { + return online_flag_ ? rtt() : 60 + random_delay_; } double http_max_wait() const { return 25.0; // 25s. Longer could be closed by proxy } - static constexpr int HTTP_MAX_AFTER = 10; // 0.01s - static constexpr int HTTP_MAX_DELAY = 30; // 0.03s - static constexpr int TEMP_KEY_TIMEOUT = 60 * 60 * 24; // one day + static constexpr int HTTP_MAX_AFTER = 10; // 0.01s + static constexpr int HTTP_MAX_DELAY = 30; // 0.03s vector to_send_; vector to_ack_; @@ -182,6 +181,7 @@ class SessionConnection final // nobody cleans up this map. But it should be really small. FlatHashMap> container_to_service_msg_; + double random_delay_ = 0; double last_read_at_ = 0; double last_ping_at_ = 0; double last_pong_at_ = 0;