Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Nicer API for network timeouts
  • Loading branch information
nyalldawson committed Feb 1, 2019
1 parent dcb6104 commit f1040ef
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
18 changes: 18 additions & 0 deletions python/core/auto_generated/qgsnetworkaccessmanager.sip.in
Expand Up @@ -234,6 +234,24 @@ for the constructor of this class.
bool useSystemProxy() const;
%Docstring
Returns whether the system proxy should be used.
%End

static int timeout();
%Docstring
Returns the network timeout length, in milliseconds.

.. seealso:: :py:func:`setTimeout`

.. versionadded:: 3.6
%End

static void setTimeout( int time );
%Docstring
Sets the maximum timeout ``time`` for network requests, in milliseconds.

.. seealso:: :py:func:`timeout`

.. versionadded:: 3.6
%End

signals:
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsoptions.cpp
Expand Up @@ -321,7 +321,7 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WindowFlags fl, const QList<QgsOpti
connect( mBtnMoveHelpDown, &QAbstractButton::clicked, this, &QgsOptions::moveHelpPathDown );

//Network timeout
mNetworkTimeoutSpinBox->setValue( mSettings->value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt() );
mNetworkTimeoutSpinBox->setValue( QgsNetworkAccessManager::timeout() );
leUserAgent->setText( mSettings->value( QStringLiteral( "/qgis/networkAndProxy/userAgent" ), "Mozilla/5.0" ).toString() );

// WMS capabilities expiry time
Expand Down Expand Up @@ -1357,7 +1357,7 @@ void QgsOptions::saveOptions()
mSettings->setValue( QStringLiteral( "help/helpSearchPath" ), helpPaths );

//Network timeout
mSettings->setValue( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), mNetworkTimeoutSpinBox->value() );
QgsNetworkAccessManager::setTimeout( mNetworkTimeoutSpinBox->value() );
mSettings->setValue( QStringLiteral( "/qgis/networkAndProxy/userAgent" ), leUserAgent->text() );

// WMS capabiltiies expiry time
Expand Down
15 changes: 12 additions & 3 deletions src/core/qgsnetworkaccessmanager.cpp
Expand Up @@ -255,8 +255,7 @@ QNetworkReply *QgsNetworkAccessManager::createRequest( QNetworkAccessManager::Op
timer->setObjectName( QStringLiteral( "timeoutTimer" ) );
connect( timer, &QTimer::timeout, this, &QgsNetworkAccessManager::abortRequest );
timer->setSingleShot( true );
const int timeout = s.value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt();
timer->start( timeout );
timer->start( timeout() );

connect( reply, &QNetworkReply::downloadProgress, timer, [timer] { timer->start(); } );
connect( reply, &QNetworkReply::uploadProgress, timer, [timer] { timer->start(); } );
Expand Down Expand Up @@ -384,7 +383,7 @@ void QgsNetworkAccessManager::restartTimeout( QNetworkReply *reply )
Q_ASSERT( !timer->isActive() );
QgsDebugMsg( QStringLiteral( "Restarting network reply timeout" ) );
timer->setSingleShot( true );
timer->start( QgsSettings().value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt() );
timer->start( timeout() );
}
}

Expand Down Expand Up @@ -609,6 +608,16 @@ void QgsNetworkAccessManager::setupDefaultProxyAndCache( Qt::ConnectionType conn
setCache( newcache );
}

int QgsNetworkAccessManager::timeout()
{
return QgsSettings().value( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), 60000 ).toInt();
}

void QgsNetworkAccessManager::setTimeout( const int time )
{
QgsSettings().setValue( QStringLiteral( "/qgis/networkAndProxy/networkTimeout" ), time );
}


//
// QgsNetworkRequestParameters
Expand Down
16 changes: 16 additions & 0 deletions src/core/qgsnetworkaccessmanager.h
Expand Up @@ -398,6 +398,22 @@ class CORE_EXPORT QgsNetworkAccessManager : public QNetworkAccessManager
*/
bool useSystemProxy() const { return mUseSystemProxy; }

/**
* Returns the network timeout length, in milliseconds.
*
* \see setTimeout()
* \since QGIS 3.6
*/
static int timeout();

/**
* Sets the maximum timeout \a time for network requests, in milliseconds.
*
* \see timeout()
* \since QGIS 3.6
*/
static void setTimeout( int time );

signals:

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsnetworkaccessmanager.cpp
Expand Up @@ -634,6 +634,10 @@ void TestQgsNetworkAccessManager::fetchTimeout()
if ( QgsTest::isTravis() )
QSKIP( "This test is disabled on Travis CI environment" );

QgsNetworkAccessManager::setTimeout( 2000 );
QCOMPARE( QgsNetworkAccessManager::timeout(), 2000 );
QgsNetworkAccessManager::setTimeout( 1000 );

QObject context;
//test fetching from a blank url
bool gotRequestAboutToBeCreatedSignal = false;
Expand Down

0 comments on commit f1040ef

Please sign in to comment.