Skip to content

Commit

Permalink
AccountState: Add a 1-5min reconnection delay #5872
Browse files Browse the repository at this point in the history
This only applies when the server was explicitly in maintenance mode or
when it was 503-unavailable.
  • Loading branch information
ckamm authored and guruz committed Jul 4, 2017
1 parent b1aaf05 commit 9493e8f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/gui/accountstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "configfile.h"

#include <QSettings>
#include <QTimer>
#include <qfontmetrics.h>

namespace OCC {
Expand All @@ -32,6 +33,7 @@ AccountState::AccountState(AccountPtr account)
, _state(AccountState::Disconnected)
, _connectionStatus(ConnectionValidator::Undefined)
, _waitingForNewCredentials(false)
, _maintenanceToConnectedDelay(60000 + (qrand() % (4 * 60000))) // 1-5min delay
{
qRegisterMetaType<AccountState *>("AccountState*");

Expand Down Expand Up @@ -228,6 +230,23 @@ void AccountState::slotConnectionValidatorResult(ConnectionValidator::Status sta
return;
}

// Come online gradually from 503 or maintenance mode
if (status == ConnectionValidator::Connected
&& (_connectionStatus == ConnectionValidator::ServiceUnavailable
|| _connectionStatus == ConnectionValidator::MaintenanceMode)) {
if (!_timeSinceMaintenanceOver.isValid()) {
qCInfo(lcAccountState) << "AccountState reconnection: delaying for"
<< _maintenanceToConnectedDelay << "ms";
_timeSinceMaintenanceOver.start();
QTimer::singleShot(_maintenanceToConnectedDelay + 100, this, SLOT(checkConnectivity()));
return;
} else if (_timeSinceMaintenanceOver.elapsed() < _maintenanceToConnectedDelay) {
qCInfo(lcAccountState) << "AccountState reconnection: only"
<< _timeSinceMaintenanceOver.elapsed() << "ms have passed";
return;
}
}

if (_connectionStatus != status) {
qCInfo(lcAccountState) << "AccountState connection status change: "
<< connectionStatusString(_connectionStatus) << "->"
Expand Down Expand Up @@ -263,9 +282,11 @@ void AccountState::slotConnectionValidatorResult(ConnectionValidator::Status sta
setState(SignedOut);
break;
case ConnectionValidator::ServiceUnavailable:
_timeSinceMaintenanceOver.invalidate();
setState(ServiceUnavailable);
break;
case ConnectionValidator::MaintenanceMode:
_timeSinceMaintenanceOver.invalidate();
setState(MaintenanceMode);
break;
case ConnectionValidator::Timeout:
Expand Down
21 changes: 17 additions & 4 deletions src/gui/accountstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ class AccountState : public QObject, public QSharedData

bool isConnected() const;

/// Triggers a ping to the server to update state and
/// connection status and errors.
void checkConnectivity();

/** Returns a new settings object for this account, already in the right groups. */
std::unique_ptr<QSettings> settings();

Expand All @@ -127,6 +123,11 @@ class AccountState : public QObject, public QSharedData
*/
void tagLastSuccessfullETagRequest();

public slots:
/// Triggers a ping to the server to update state and
/// connection status and errors.
void checkConnectivity();

private:
void setState(State state);

Expand All @@ -148,6 +149,18 @@ protected Q_SLOTS:
bool _waitingForNewCredentials;
QElapsedTimer _timeSinceLastETagCheck;
QPointer<ConnectionValidator> _connectionValidator;

/**
* Starts counting when the server starts being back up after 503 or
* maintenance mode. The account will only become connected once this
* timer exceeds the _maintenanceToConnectedDelay value.
*/
QElapsedTimer _timeSinceMaintenanceOver;

/**
* Milliseconds for which to delay reconnection after 503/maintenance.
*/
int _maintenanceToConnectedDelay;
};
}

Expand Down

0 comments on commit 9493e8f

Please sign in to comment.