From 3b703d726286f25a15a290c9966f7ce120e56e93 Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 2 Dec 2019 13:49:33 +0300 Subject: [PATCH] Keep in sent container only important msgIds. --- Telegram/SourceFiles/mtproto/connection.cpp | 40 ++++++++++++--------- Telegram/SourceFiles/mtproto/connection.h | 1 - 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Telegram/SourceFiles/mtproto/connection.cpp b/Telegram/SourceFiles/mtproto/connection.cpp index 7a800c5245..d180ebba66 100644 --- a/Telegram/SourceFiles/mtproto/connection.cpp +++ b/Telegram/SourceFiles/mtproto/connection.cpp @@ -249,20 +249,31 @@ void Connection::checkSentRequests() { } void Connection::clearOldContainers() { + auto resent = false; const auto now = crl::now(); for (auto i = _sentContainers.begin(); i != _sentContainers.end();) { if (now > i->second.sent + kSentContainerLives) { - DEBUG_LOG(("MTP Info: Removing old container %1, " + DEBUG_LOG(("MTP Info: Removing old container with resending %1, " "sent: %2, now: %3, current unixtime: %4" ).arg(i->first ).arg(i->second.sent ).arg(now ).arg(base::unixtime::now())); + + const auto ids = std::move(i->second.messages); i = _sentContainers.erase(i); + + resent = resent || !ids.empty(); + for (const auto innerMsgId : ids) { + resend(innerMsgId, -1, true); + } } else { ++i; } } + if (resent) { + _sessionData->queueNeedToResumeAndSend(); + } } void Connection::destroyAllConnections() { @@ -464,7 +475,6 @@ mtpMsgId Connection::replaceMsgId(SerializedRequest &request, mtpMsgId newId) { } mtpMsgId Connection::placeToContainer( - SentContainer &sentIdsWrap, SerializedRequest &toSendRequest, mtpMsgId &bigMsgId, bool forceNewMsgId, @@ -473,7 +483,6 @@ mtpMsgId Connection::placeToContainer( if (msgId >= bigMsgId) { bigMsgId = base::unixtime::mtproto_msg_id(); } - sentIdsWrap.messages.push_back(msgId); uint32 from = toSendRequest->size(), len = req.messageSize(); toSendRequest->resize(from + len); @@ -554,6 +563,8 @@ void Connection::tryToSend() { resendRequest = SerializedRequest::Serialize(MTPMsgResendReq( MTP_msg_resend_req(MTP_vector( base::take(_resendRequestData))))); + // Add to haveSent / _ackedIds, but don't add to requestMap. + resendRequest->requestId = GetNextRequestId(); } if (!_stateRequestData.empty()) { auto ids = QVector(); @@ -768,7 +779,6 @@ void Connection::tryToSend() { if (bindDcKeyRequest) { _bindMsgId = placeToContainer( - sentIdsWrap, toSendRequest, bigMsgId, false, @@ -777,22 +787,17 @@ void Connection::tryToSend() { } if (pingRequest) { _pingMsgId = placeToContainer( - sentIdsWrap, toSendRequest, bigMsgId, forceNewMsgId, pingRequest); needAnyResponse = true; } - if (resendRequest || stateRequest) { - needAnyResponse = true; - } for (auto &[requestId, request] : toSend) { const auto msgId = prepareToSend( request, bigMsgId, forceNewMsgId); - sentIdsWrap.messages.push_back(msgId); if (msgId >= bigMsgId) { bigMsgId = base::unixtime::mtproto_msg_id(); } @@ -816,9 +821,10 @@ void Connection::tryToSend() { *(toSendRequest->data() + reqNeedsLayer + 3) += initSize; added = true; } + Assert(!haveSent.contains(msgId)); haveSent.emplace(msgId, request); - + sentIdsWrap.messages.push_back(msgId); needAnyResponse = true; } else { _ackedIds.emplace(msgId, request->requestId); @@ -832,25 +838,28 @@ void Connection::tryToSend() { } if (stateRequest) { const auto msgId = placeToContainer( - sentIdsWrap, toSendRequest, bigMsgId, forceNewMsgId, stateRequest); Assert(!haveSent.contains(msgId)); haveSent.emplace(msgId, stateRequest); + sentIdsWrap.messages.push_back(msgId); + needAnyResponse = true; } if (resendRequest) { - placeToContainer( - sentIdsWrap, + const auto msgId = placeToContainer( toSendRequest, bigMsgId, forceNewMsgId, resendRequest); + Assert(!haveSent.contains(msgId)); + haveSent.emplace(msgId, resendRequest); + sentIdsWrap.messages.push_back(msgId); + needAnyResponse = true; } if (ackRequest) { placeToContainer( - sentIdsWrap, toSendRequest, bigMsgId, forceNewMsgId, @@ -858,7 +867,6 @@ void Connection::tryToSend() { } if (httpWaitRequest) { placeToContainer( - sentIdsWrap, toSendRequest, bigMsgId, forceNewMsgId, @@ -1959,7 +1967,6 @@ void Connection::requestsAcked(const QVector &ids, bool byResponse) { DEBUG_LOG(("Message Info: requests acked, ids %1").arg(LogIdsVector(ids))); - auto clearedBecauseTooOld = std::vector(); QVector toAckMore; { QWriteLocker locker2(_sessionData->haveSentMutex()); @@ -2023,7 +2030,6 @@ void Connection::requestsAcked(const QVector &ids, bool byResponse) { auto ackedCount = _ackedIds.size(); if (ackedCount > kIdsBufferSize) { DEBUG_LOG(("Message Info: removing some old acked sent msgIds %1").arg(ackedCount - kIdsBufferSize)); - clearedBecauseTooOld.reserve(ackedCount - kIdsBufferSize); while (ackedCount-- > kIdsBufferSize) { _ackedIds.erase(_ackedIds.begin()); } diff --git a/Telegram/SourceFiles/mtproto/connection.h b/Telegram/SourceFiles/mtproto/connection.h index 036a90cfd4..706919b4ba 100644 --- a/Telegram/SourceFiles/mtproto/connection.h +++ b/Telegram/SourceFiles/mtproto/connection.h @@ -106,7 +106,6 @@ private: void clearOldContainers(); mtpMsgId placeToContainer( - SentContainer &sentIdsWrap, details::SerializedRequest &toSendRequest, mtpMsgId &bigMsgId, bool forceNewMsgId,