Skip to content

Commit

Permalink
Use Qt built-in timeout handling
Browse files Browse the repository at this point in the history
Instead of spinning our own QTimer (which was overall quite
brittle, often when I resumed from suspend my system monitor
was just broken and didn't receive any data anymore) set
QNetworkRequest::transferTimeout.
  • Loading branch information
kbroulik committed Dec 17, 2023
1 parent 5f5dd29 commit a6f8b3d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
19 changes: 2 additions & 17 deletions src/lib/apirequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QScopeGuard>
#include <QTimer>
#include <QUrlQuery>

namespace QAlphaCloud
Expand Down Expand Up @@ -92,18 +91,6 @@ bool ApiRequest::send()
return false;
}

if (configuration->requestTimeout() > 0) {
m_timeoutTimer = new QTimer(this);
m_timeoutTimer->setSingleShot(true);
connect(m_timeoutTimer, &QTimer::timeout, this, [this] {
qCWarning(QALPHACLOUD_LOG) << "Request timed out after" << m_timeoutTimer->interval() << "ms";
// TODO set error to QNetworkReply::TimeoutError
abort();
});
// TODO Qt6 QNetworkReply::requestSent
m_timeoutTimer->start(configuration->requestTimeout());
}

// Calculate Header fields (appId, timeStamp, sign).
const QByteArray timeStampStr = QByteArray::number(QDateTime::currentSecsSinceEpoch(), 'f', 0);

Expand Down Expand Up @@ -133,6 +120,8 @@ bool ApiRequest::send()
url.setQuery(query);

QNetworkRequest request(url);
request.setTransferTimeout(configuration->requestTimeout());

// request.setAttribute(QNetworkRequest::Http2AllowedAttribute, false);

// TODO allow spoofing stuff like User Agent.
Expand All @@ -151,10 +140,6 @@ bool ApiRequest::send()

auto *reply = m_connector->networkAccessManager()->get(request);
connect(reply, &QNetworkReply::finished, this, [this, reply] {
if (m_timeoutTimer) {
m_timeoutTimer->stop();
}

if (reply->error() != QNetworkReply::NoError) {
if (reply->error() == QNetworkReply::OperationCanceledError) {
qCDebug(QALPHACLOUD_LOG) << "API request for endpoint" << reply->url() << "was canceled";
Expand Down
4 changes: 0 additions & 4 deletions src/lib/apirequest_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

#include "qalphacloud.h"

class QTimer;

namespace QAlphaCloud
{

Expand Down Expand Up @@ -91,8 +89,6 @@ class ApiRequest : public QObject
QAlphaCloud::ErrorCode m_error = QAlphaCloud::ErrorCode::NoError;
QString m_errorString;
QJsonValue m_data;

QTimer *m_timeoutTimer = nullptr;
};

} // namespace QAlphaCloud

0 comments on commit a6f8b3d

Please sign in to comment.