Move check for duplicate /sync responses to the same thread

This commit is contained in:
Nicolas Werner 2021-03-16 21:01:14 +01:00
parent 0dc40e50f8
commit cc3d32c65e
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
2 changed files with 15 additions and 14 deletions

View file

@ -805,8 +805,17 @@ ChatPage::startInitialSync()
} }
void void
ChatPage::handleSyncResponse(const mtx::responses::Sync &res) ChatPage::handleSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token)
{ {
try {
if (prev_batch_token != cache::nextBatchToken()) {
nhlog::net()->warn("Duplicate sync, dropping");
return;
}
} catch (const lmdb::error &e) {
nhlog::db()->warn("Logged out in the mean time, dropping sync");
}
nhlog::net()->debug("sync completed: {}", res.next_batch); nhlog::net()->debug("sync completed: {}", res.next_batch);
// Ensure that we have enough one-time keys available. // Ensure that we have enough one-time keys available.
@ -861,15 +870,6 @@ ChatPage::trySync()
http::client()->sync( http::client()->sync(
opts, opts,
[this, since = opts.since](const mtx::responses::Sync &res, mtx::http::RequestErr err) { [this, since = opts.since](const mtx::responses::Sync &res, mtx::http::RequestErr err) {
try {
if (since != cache::nextBatchToken()) {
nhlog::net()->warn("Duplicate sync, dropping");
return;
}
} catch (const lmdb::error &e) {
nhlog::db()->warn("Logged out in the mean time, dropping sync");
}
if (err) { if (err) {
const auto error = QString::fromStdString(err->matrix_error.error); const auto error = QString::fromStdString(err->matrix_error.error);
const auto msg = tr("Please try to login again: %1").arg(error); const auto msg = tr("Please try to login again: %1").arg(error);
@ -895,7 +895,7 @@ ChatPage::trySync()
return; return;
} }
emit newSyncResponse(res); emit newSyncResponse(res, since);
}); });
} }

View file

@ -141,7 +141,7 @@ signals:
void trySyncCb(); void trySyncCb();
void tryDelayedSyncCb(); void tryDelayedSyncCb();
void tryInitialSyncCb(); void tryInitialSyncCb();
void newSyncResponse(const mtx::responses::Sync &res); void newSyncResponse(const mtx::responses::Sync &res, const std::string &prev_batch_token);
void leftRoom(const QString &room_id); void leftRoom(const QString &room_id);
void newRoom(const QString &room_id); void newRoom(const QString &room_id);
@ -194,7 +194,8 @@ private slots:
void changeRoom(const QString &room_id); void changeRoom(const QString &room_id);
void dropToLoginPage(const QString &msg); void dropToLoginPage(const QString &msg);
void handleSyncResponse(const mtx::responses::Sync &res); void handleSyncResponse(const mtx::responses::Sync &res,
const std::string &prev_batch_token);
private: private:
static ChatPage *instance_; static ChatPage *instance_;