summaryrefslogtreecommitdiffstats
path: root/src/network/access
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2021-08-27 12:52:20 +0200
committerMårten Nordheim <marten.nordheim@qt.io>2021-09-02 16:57:42 +0200
commita253bec500a837496d19438f76cfa06b704f2a57 (patch)
treedbb296eec43540cc3f00596e6685bd4c06336cd2 /src/network/access
parent3a7d6767e9e0d51acdab63b192be6d4557557479 (diff)
QNetworkAccessCache: Port to QDeadlineTimer
QDeadlineTimer is semantically more correct than comparing timestamps to 'now'. Change-Id: I15d9654af2886499392e6409d22f802203aca18d Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/network/access')
-rw-r--r--src/network/access/qnetworkaccesscache.cpp24
1 files changed, 11 insertions, 13 deletions
diff --git a/src/network/access/qnetworkaccesscache.cpp b/src/network/access/qnetworkaccesscache.cpp
index 66006084d0..9e5d10ddd7 100644
--- a/src/network/access/qnetworkaccesscache.cpp
+++ b/src/network/access/qnetworkaccesscache.cpp
@@ -39,7 +39,7 @@
#include "qnetworkaccesscache_p.h"
#include "QtCore/qpointer.h"
-#include "QtCore/qdatetime.h"
+#include "QtCore/qdeadlinetimer.h"
#include "qnetworkaccessmanager_p.h"
#include "qnetworkreply_p.h"
#include "qnetworkrequest.h"
@@ -65,7 +65,7 @@ namespace {
// idea copied from qcache.h
struct QNetworkAccessCache::Node
{
- QDateTime timestamp;
+ QDeadlineTimer timer;
QByteArray key;
Node *previous; // "previous" nodes expire "previous"ly (before us)
@@ -146,22 +146,23 @@ void QNetworkAccessCache::linkEntry(const QByteArray &key)
Q_ASSERT(node->useCount == 0);
- node->timestamp = QDateTime::currentDateTimeUtc().addSecs(node->object->expiryTimeoutSeconds);
+ node->timer.setPreciseRemainingTime(node->object->expiryTimeoutSeconds);
#ifdef DEBUG_ACCESSCACHE
qDebug() << "QNetworkAccessCache case trying to insert=" << QString::fromUtf8(key)
- << node->timestamp;
+ << node->timer.remainingTime() << "milliseconds";
Node *current = lastExpiringNode;
while (current) {
qDebug() << "QNetworkAccessCache item=" << QString::fromUtf8(current->key)
- << current->timestamp << (current == lastExpiringNode ? "[last to expire]" : "")
- << (current == firstExpiringNode ? "[next to expire]" : "");
+ << current->timer.remainingTime() << "milliseconds"
+ << (current == lastExpiringNode ? "[last to expire]" : "")
+ << (current == firstExpiringNode ? "[first to expire]" : "");
current = current->previous;
}
#endif
if (lastExpiringNode) {
Q_ASSERT(lastExpiringNode->next == nullptr);
- if (lastExpiringNode->timestamp < node->timestamp) {
+ if (lastExpiringNode->timer < node->timer) {
// Insert as new last-to-expire node.
node->previous = lastExpiringNode;
lastExpiringNode->next = node;
@@ -170,7 +171,7 @@ void QNetworkAccessCache::linkEntry(const QByteArray &key)
} else {
// Insert in a sorted way, as different nodes might have had different expiryTimeoutSeconds set.
Node *current = lastExpiringNode;
- while (current->previous != nullptr && current->previous->timestamp >= node->timestamp) {
+ while (current->previous != nullptr && current->previous->timer >= node->timer) {
current = current->previous;
}
node->previous = current->previous;
@@ -226,7 +227,7 @@ void QNetworkAccessCache::updateTimer()
if (!firstExpiringNode)
return;
- qint64 interval = QDateTime::currentDateTimeUtc().msecsTo(firstExpiringNode->timestamp);
+ qint64 interval = firstExpiringNode->timer.remainingTime();
if (interval <= 0) {
interval = 0;
}
@@ -252,10 +253,7 @@ bool QNetworkAccessCache::emitEntryReady(Node *node, QObject *target, const char
void QNetworkAccessCache::timerEvent(QTimerEvent *)
{
- // expire old items
- const QDateTime now = QDateTime::currentDateTimeUtc();
-
- while (firstExpiringNode && firstExpiringNode->timestamp < now) {
+ while (firstExpiringNode && firstExpiringNode->timer.hasExpired()) {
Node *next = firstExpiringNode->next;
firstExpiringNode->object->dispose();
hash.remove(firstExpiringNode->key); // `firstExpiringNode` gets deleted