From 3a579dd5cb591cabc2f15bbf797c6d717a872b3d Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 20 Sep 2022 17:46:10 +0300 Subject: [PATCH] Improve warning on long RawConnection::flush. --- td/mtproto/RawConnection.cpp | 16 ++++++++++++---- td/mtproto/RawConnection.h | 4 ++-- td/mtproto/SessionConnection.cpp | 21 ++++++++++++++++----- td/mtproto/SessionConnection.h | 3 +++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/td/mtproto/RawConnection.cpp b/td/mtproto/RawConnection.cpp index c7b196eaa..d69211e7b 100644 --- a/td/mtproto/RawConnection.cpp +++ b/td/mtproto/RawConnection.cpp @@ -49,11 +49,13 @@ class RawConnectionDefault final : public RawConnection { bool can_send() const final { return transport_->can_write(); } + TransportType get_transport_type() const final { return transport_->get_type(); } - void send_crypto(const Storer &storer, int64 session_id, int64 salt, const AuthKey &auth_key, - uint64 quick_ack_token) final { + + size_t send_crypto(const Storer &storer, int64 session_id, int64 salt, const AuthKey &auth_key, + uint64 quick_ack_token) final { PacketInfo info; info.version = 2; info.no_crypto_flag = false; @@ -76,7 +78,9 @@ class RawConnectionDefault final : public RawConnection { } } + auto packet_size = packet.size(); transport_->write(std::move(packet), use_quick_ack); + return packet_size; } uint64 send_no_crypto(const Storer &storer) final { @@ -280,11 +284,13 @@ class RawConnectionHttp final : public RawConnection { bool can_send() const final { return mode_ == Send; } + TransportType get_transport_type() const final { return mtproto::TransportType{mtproto::TransportType::Http, 0, mtproto::ProxySecret()}; } - void send_crypto(const Storer &storer, int64 session_id, int64 salt, const AuthKey &auth_key, - uint64 quick_ack_token) final { + + size_t send_crypto(const Storer &storer, int64 session_id, int64 salt, const AuthKey &auth_key, + uint64 quick_ack_token) final { PacketInfo info; info.version = 2; info.no_crypto_flag = false; @@ -295,7 +301,9 @@ class RawConnectionHttp final : public RawConnection { auto packet = BufferWriter{Transport::write(storer, auth_key, &info), 0, 0}; Transport::write(storer, auth_key, &info, packet.as_slice()); + auto packet_size = packet.size(); send_packet(packet.as_buffer_slice()); + return packet_size; } uint64 send_no_crypto(const Storer &storer) final { diff --git a/td/mtproto/RawConnection.h b/td/mtproto/RawConnection.h index be63533dd..6582d46e7 100644 --- a/td/mtproto/RawConnection.h +++ b/td/mtproto/RawConnection.h @@ -48,8 +48,8 @@ class RawConnection { virtual bool can_send() const = 0; virtual TransportType get_transport_type() const = 0; - virtual void send_crypto(const Storer &storer, int64 session_id, int64 salt, const AuthKey &auth_key, - uint64 quick_ack_token) = 0; + virtual size_t send_crypto(const Storer &storer, int64 session_id, int64 salt, const AuthKey &auth_key, + uint64 quick_ack_token) = 0; virtual uint64 send_no_crypto(const Storer &storer) = 0; virtual PollableFdInfo &get_poll_info() = 0; diff --git a/td/mtproto/SessionConnection.cpp b/td/mtproto/SessionConnection.cpp index 63e785b37..bb7b6670c 100644 --- a/td/mtproto/SessionConnection.cpp +++ b/td/mtproto/SessionConnection.cpp @@ -26,7 +26,6 @@ #include "td/utils/ScopeGuard.h" #include "td/utils/SliceBuilder.h" #include "td/utils/Time.h" -#include "td/utils/Timer.h" #include "td/utils/tl_parsers.h" #include "td/utils/TlDowncastHelper.h" @@ -718,6 +717,7 @@ Status SessionConnection::on_quick_ack(uint64 quick_ack_token) { void SessionConnection::on_read(size_t size) { last_read_at_ = Time::now_cached(); + last_read_size_ += size; } SessionConnection::SessionConnection(Mode mode, unique_ptr raw_connection, AuthData *auth_data) @@ -768,8 +768,9 @@ void SessionConnection::do_close(Status status) { void SessionConnection::send_crypto(const Storer &storer, uint64 quick_ack_token) { CHECK(state_ != Closed); - raw_connection_->send_crypto(storer, auth_data_->get_session_id(), auth_data_->get_server_salt(Time::now_cached()), - auth_data_->get_auth_key(), quick_ack_token); + last_write_size_ += raw_connection_->send_crypto(storer, auth_data_->get_session_id(), + auth_data_->get_server_salt(Time::now_cached()), + auth_data_->get_auth_key(), quick_ack_token); } Result SessionConnection::send_query(BufferSlice buffer, bool gzip_flag, int64 message_id, @@ -1028,7 +1029,6 @@ Status SessionConnection::do_flush() { << connected_flag_ << ' ' << is_main_ << ' ' << need_destroy_auth_key_ << ' ' << sent_destroy_auth_key_ << ' ' << callback_ << ' ' << (Time::now() - created_at_) << ' ' << (Time::now() - last_read_at_); - PerfWarningTimer timer("SessionConnection::do_flush", 0.01); CHECK(state_ != Closed); if (state_ == Init) { TRY_STATUS(init()); @@ -1037,7 +1037,18 @@ Status SessionConnection::do_flush() { return Status::Error("No auth key"); } - TRY_STATUS(raw_connection_->flush(auth_data_->get_auth_key(), *this)); + last_read_size_ = 0; + last_write_size_ = 0; + auto start_time = Time::now(); + auto result = raw_connection_->flush(auth_data_->get_auth_key(), *this); + auto elapsed_time = Time::now() - start_time; + if (elapsed_time >= 0.01) { + LOG(ERROR) << "RawConnection::flush took " << elapsed_time << " seconds, written " << last_write_size_ + << " bytes, read " << last_read_size_ << " bytes and returned " << result; + } + if (result.is_error()) { + return result; + } if (last_pong_at_ + ping_disconnect_delay() < Time::now_cached()) { auto stats_callback = raw_connection_->stats_callback(); diff --git a/td/mtproto/SessionConnection.h b/td/mtproto/SessionConnection.h index 1ba2d3ba6..ead20e1b1 100644 --- a/td/mtproto/SessionConnection.h +++ b/td/mtproto/SessionConnection.h @@ -189,6 +189,9 @@ class SessionConnection final uint64 last_ping_message_id_ = 0; uint64 last_ping_container_id_ = 0; + uint64 last_read_size_ = 0; + uint64 last_write_size_ = 0; + bool need_destroy_auth_key_ = false; bool sent_destroy_auth_key_ = false;