Requesting config if can't connect.

This commit is contained in:
John Preston 2018-04-24 11:21:19 +04:00
parent 4a9db99082
commit 909acb25fd
4 changed files with 38 additions and 2 deletions

View File

@ -30,6 +30,9 @@ constexpr auto kRecreateKeyId = AuthKey::KeyId(0xFFFFFFFFFFFFFFFFULL);
constexpr auto kIntSize = static_cast<int>(sizeof(mtpPrime));
constexpr auto kMaxModExpSize = 256;
// If we can't connect for this time we will ask _instance to update config.
constexpr auto kRequestConfigTimeout = TimeMs(8000);
// Don't try to handle messages larger than this size.
constexpr auto kMaxMessageLength = 16 * 1024 * 1024;
@ -356,6 +359,7 @@ ConnectionPrivate::ConnectionPrivate(Instance *instance, QThread *thread, Connec
, _state(DisconnectedState)
, _shiftedDcId(shiftedDcId)
, _owner(owner)
, _configWasFineAt(getms(true))
, _waitForReceived(MTPMinReceiveDelay)
, _waitForConnected(MTPMinConnectDelay)
//, sessionDataMutex(QReadWriteLock::Recursive)
@ -1031,11 +1035,21 @@ void ConnectionPrivate::connectToServer(bool afterConfig) {
DEBUG_LOG(("MTP Info: DC %1 options for IPv4 over HTTP not found, waiting for config").arg(_shiftedDcId));
if (Global::TryIPv6() && noIPv6) DEBUG_LOG(("MTP Info: DC %1 options for IPv6 over HTTP not found, waiting for config").arg(_shiftedDcId));
connect(_instance, SIGNAL(configLoaded()), this, SLOT(onConfigLoaded()), Qt::UniqueConnection);
InvokeQueued(_instance, [instance = _instance] { instance->requestConfig(); });
InvokeQueued(_instance, [instance = _instance] {
instance->requestConfig();
});
return;
}
if (afterConfig && (_conn4 || _conn6)) return;
if (afterConfig) {
if (_conn4 || _conn6) {
return;
}
} else if (getms(true) - _configWasFineAt > kRequestConfigTimeout) {
InvokeQueued(_instance, [instance = _instance] {
instance->requestConfigIfOld();
});
}
createConn(!noIPv4, !noIPv6);
retryTimer.stop();
@ -2340,6 +2354,8 @@ void ConnectionPrivate::updateAuthKey() {
QReadLocker lockFinished(&sessionDataMutex);
if (!sessionData || !_conn) return;
_configWasFineAt = getms(true);
DEBUG_LOG(("AuthKey Info: Connection updating key from Session, dc %1").arg(_shiftedDcId));
uint64 newKeyId = 0;
{

View File

@ -197,6 +197,7 @@ private:
AbstractConnection *_conn = nullptr;
AbstractConnection *_conn4 = nullptr;
AbstractConnection *_conn6 = nullptr;
TimeMs _configWasFineAt = 0;
SingleTimer retryTimer; // exp retry timer
int retryTimeout = 1;

View File

@ -23,6 +23,11 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/timer.h"
namespace MTP {
namespace {
constexpr auto kConfigBecomesOldIn = 2 * 60 * TimeMs(1000);
} // namespace
class Instance::Private : private Sender {
public:
@ -41,6 +46,7 @@ public:
not_null<DcOptions*> dcOptions();
void requestConfig();
void requestConfigIfOld();
void requestCDNConfig();
void restart();
@ -149,6 +155,7 @@ private:
std::unique_ptr<internal::ConfigLoader> _configLoader;
mtpRequestId _cdnConfigLoadRequestId = 0;
TimeMs _lastConfigLoadedTime = 0;
std::map<DcId, AuthKeyPtr> _keysForWrite;
mutable QReadWriteLock _keysForWriteLock;
@ -284,6 +291,12 @@ void Instance::Private::requestConfig() {
_configLoader->load();
}
void Instance::Private::requestConfigIfOld() {
if (getms(true) - _lastConfigLoadedTime >= kConfigBecomesOldIn) {
requestConfig();
}
}
void Instance::Private::requestCDNConfig() {
if (_cdnConfigLoadRequestId || _mainDcId == Config::kNoneMainDc) {
return;
@ -580,6 +593,7 @@ void Instance::Private::configLoadDone(const MTPConfig &result) {
Expects(result.type() == mtpc_config);
_configLoader.reset();
_lastConfigLoadedTime = getms(true);
auto &data = result.c_config();
DEBUG_LOG(("MTP Info: got config, chat_size_max: %1, date: %2, test_mode: %3, this_dc: %4, dc_options.length: %5").arg(data.vchat_size_max.v).arg(data.vdate.v).arg(mtpIsTrue(data.vtest_mode)).arg(data.vthis_dc.v).arg(data.vdc_options.v.size()));
@ -1310,6 +1324,10 @@ void Instance::requestConfig() {
_private->requestConfig();
}
void Instance::requestConfigIfOld() {
_private->requestConfigIfOld();
}
void Instance::requestCDNConfig() {
_private->requestCDNConfig();
}

View File

@ -135,6 +135,7 @@ public:
void scheduleKeyDestroy(ShiftedDcId shiftedDcId);
void requestConfig();
void requestConfigIfOld();
void requestCDNConfig();
~Instance();