Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore late pings #5457

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Bugfix: If a network request errors with 200 OK, Qt's error code is now reported instead of the HTTP status. (#5378)
- Bugfix: Fixed restricted users usernames not being clickable. (#5405)
- Bugfix: Fixed a crash that could occur when logging was enabled in IRC servers that were removed. (#5419)
- Bugfix: Fixed message history occasionally not loading after a sleep. (#5457)
- Dev: Update Windows build from Qt 6.5.0 to Qt 6.7.1. (#5420)
- Dev: Update vcpkg build Qt from 6.5.0 to 6.7.0, boost from 1.83.0 to 1.85.0, openssl from 3.1.3 to 3.3.0. (#5422)
- Dev: Use Qt's high DPI scaling. (#4868, #5400)
Expand Down
25 changes: 24 additions & 1 deletion src/providers/irc/IrcConnection2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "common/QLogging.hpp"
#include "common/Version.hpp"

#include <chrono>

using namespace std::chrono_literals;

namespace chatterino {

namespace {
Expand Down Expand Up @@ -56,6 +60,7 @@ IrcConnection::IrcConnection(QObject *parent)
// Send ping every x seconds
this->pingTimer_.setInterval(5000);
this->pingTimer_.start();
this->lastPing_ = std::chrono::system_clock::now();
QObject::connect(&this->pingTimer_, &QTimer::timeout, [this] {
if (this->isConnected())
{
Expand All @@ -64,7 +69,25 @@ IrcConnection::IrcConnection(QObject *parent)
// If we're still receiving messages, all is well
this->recentlyReceivedMessage_ = false;
this->waitingForPong_ = false;
this->heartbeat.invoke();

// Check if we got invoked too late (e.g. due to a sleep)
auto now = std::chrono::system_clock::now();
auto elapsed = now - this->lastPing_;
Nerixyz marked this conversation as resolved.
Show resolved Hide resolved
if (elapsed < 3 * 5000ms)
{
this->heartbeat.invoke();
}
else
{
qCDebug(chatterinoIrc).nospace()
<< "Got late ping (skipping heartbeat): "
<< std::chrono::duration_cast<
std::chrono::milliseconds>(elapsed)
.count()
<< "ms";
}
this->lastPing_ = now;

return;
}

Expand Down
3 changes: 3 additions & 0 deletions src/providers/irc/IrcConnection2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <pajlada/signals/signal.hpp>
#include <QTimer>

#include <chrono>

namespace chatterino {

class IrcConnection : public Communi::IrcConnection
Expand Down Expand Up @@ -33,6 +35,7 @@ class IrcConnection : public Communi::IrcConnection
QTimer pingTimer_;
QTimer reconnectTimer_;
std::atomic<bool> recentlyReceivedMessage_{true};
std::chrono::time_point<std::chrono::system_clock> lastPing_;

// Reconnect with a base delay of 1 second and max out at 1 second * (2^(5-1)) (i.e. 16 seconds)
ExponentialBackoff<5> reconnectBackoff_{std::chrono::milliseconds{1000}};
Expand Down
Loading