Skip to content

Commit

Permalink
fix: request more historical messages on wake (#5018)
Browse files Browse the repository at this point in the history
  • Loading branch information
iProdigy authored Dec 16, 2023
1 parent 5f8c4c6 commit e75ce5d
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
- Dev: Move `clang-tidy` checker to its own CI job. (#4996)
- Dev: Refactored the Image Uploader feature. (#4971)
- Dev: Fixed deadlock and use-after-free in tests. (#4981)
- Dev: Load less message history upon reconnects. (#5001, #5018)
- Dev: Load less message history upon reconnects. (#5001)
- Dev: BREAKING: Replace custom `import()` with normal Lua `require()`. (#5014)
- Dev: Fixed most compiler warnings. (#5028)
Expand Down
15 changes: 14 additions & 1 deletion src/providers/irc/AbstractIrcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ AbstractIrcServer::AbstractIrcServer()
}
this->readConnection_->smartReconnect();
});
this->connections_.managedConnect(this->readConnection_->heartbeat, [this] {
this->markChannelsConnected();
});
}

void AbstractIrcServer::initializeIrc()
Expand Down Expand Up @@ -358,11 +361,21 @@ void AbstractIrcServer::onDisconnected()

if (auto *channel = dynamic_cast<TwitchChannel *>(chan.get()))
{
channel->markDisconnectedNow();
channel->markDisconnected();
}
}
}

void AbstractIrcServer::markChannelsConnected()
{
this->forEachChannel([](const ChannelPtr &chan) {
if (auto *channel = dynamic_cast<TwitchChannel *>(chan.get()))
{
channel->markConnected();
}
});
}

std::shared_ptr<Channel> AbstractIrcServer::getCustomChannel(
const QString &channelName)
{
Expand Down
1 change: 1 addition & 0 deletions src/providers/irc/AbstractIrcServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class AbstractIrcServer : public QObject
virtual void onReadConnected(IrcConnection *connection);
virtual void onWriteConnected(IrcConnection *connection);
virtual void onDisconnected();
void markChannelsConnected();

virtual std::shared_ptr<Channel> getCustomChannel(
const QString &channelName);
Expand Down
1 change: 1 addition & 0 deletions src/providers/irc/IrcConnection2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ IrcConnection::IrcConnection(QObject *parent)
// If we're still receiving messages, all is well
this->recentlyReceivedMessage_ = false;
this->waitingForPong_ = false;
this->heartbeat.invoke();
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 @@ -19,6 +19,9 @@ class IrcConnection : public Communi::IrcConnection
// receiver to trigger a reconnect, if desired
pajlada::Signals::Signal<bool> connectionLost;

// Signal to indicate the connection is still healthy
pajlada::Signals::NoArgSignal heartbeat;

// Request a reconnect with a minimum interval between attempts.
// This won't violate RECONNECT_MIN_INTERVAL
void smartReconnect();
Expand Down
30 changes: 17 additions & 13 deletions src/providers/twitch/TwitchChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ TwitchChannel::TwitchChannel(const QString &name)
// We can safely ignore this signal connection this has no external dependencies - once the signal
// is destroyed, it will no longer be able to fire
std::ignore = this->joined.connect([this]() {
if (this->disconnectedAt_.has_value())
if (this->disconnected_)
{
this->loadRecentMessagesReconnect();
this->disconnectedAt_ = std::nullopt;
this->lastConnectedAt_ = std::chrono::system_clock::now();
this->disconnected_ = false;
}
});

Expand Down Expand Up @@ -731,6 +732,8 @@ void TwitchChannel::setRoomId(const QString &id)
*this->roomID_.access() = id;
this->roomIdChanged();
this->loadRecentMessages();
this->disconnected_ = false;
this->lastConnectedAt_ = std::chrono::system_clock::now();
}
}

Expand Down Expand Up @@ -1105,22 +1108,23 @@ bool TwitchChannel::setLive(bool newLiveStatus)
return true;
}

void TwitchChannel::markDisconnectedNow()
void TwitchChannel::markConnected()
{
if (this->roomId().isEmpty())
if (this->lastConnectedAt_.has_value() && !this->disconnected_)
{
// we were never joined in the first place
return;
this->lastConnectedAt_ = std::chrono::system_clock::now();
}
}

if (this->disconnectedAt_.has_value())
void TwitchChannel::markDisconnected()
{
if (this->roomId().isEmpty())
{
// don't overwrite prior timestamp since
// a reconnection hasn't happened yet
// we were never joined in the first place
return;
}

this->disconnectedAt_ = std::chrono::system_clock::now();
this->disconnected_ = true;
}

void TwitchChannel::loadRecentMessages()
Expand Down Expand Up @@ -1194,14 +1198,14 @@ void TwitchChannel::loadRecentMessagesReconnect()

const auto now = std::chrono::system_clock::now();
int limit = getSettings()->twitchMessageHistoryLimit.getValue();
if (this->disconnectedAt_.has_value())
if (this->lastConnectedAt_.has_value())
{
// calculate how many messages could have occured
// while we were not connected to the channel
// assuming a maximum of 10 messages per second
const auto secondsSinceDisconnect =
std::chrono::duration_cast<std::chrono::seconds>(
now - this->disconnectedAt_.value())
now - this->lastConnectedAt_.value())
.count();
limit =
std::min(static_cast<int>(secondsSinceDisconnect + 1) * 10, limit);
Expand Down Expand Up @@ -1233,7 +1237,7 @@ void TwitchChannel::loadRecentMessagesReconnect()

tc->loadingRecentMessages_.clear();
},
limit, this->disconnectedAt_, now, true);
limit, this->lastConnectedAt_, now, true);
}

void TwitchChannel::refreshPubSub()
Expand Down
13 changes: 9 additions & 4 deletions src/providers/twitch/TwitchChannel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,14 @@ class TwitchChannel final : public Channel, public ChannelChatters
SharedAccessGuard<const StreamStatus> accessStreamStatus() const;

/**
* Records the current timestamp the channel was disconnected.
* This can be used to calculate the time spent disconnected after a successful reconnect
* Records that the channel is no longer joined.
*/
void markDisconnectedNow();
void markDisconnected();

/**
* Records that the channel's read connection is healthy.
*/
void markConnected();

// Emotes
std::optional<EmotePtr> bttvEmote(const EmoteName &name) const;
Expand Down Expand Up @@ -364,8 +368,9 @@ class TwitchChannel final : public Channel, public ChannelChatters
int chatterCount_{};
UniqueAccess<StreamStatus> streamStatus_;
UniqueAccess<RoomModes> roomModes_;
bool disconnected_{};
std::optional<std::chrono::time_point<std::chrono::system_clock>>
disconnectedAt_{};
lastConnectedAt_{};
std::atomic_flag loadingRecentMessages_ = ATOMIC_FLAG_INIT;
std::unordered_map<QString, std::weak_ptr<MessageThread>> threads_;

Expand Down
1 change: 1 addition & 0 deletions src/providers/twitch/TwitchIrcServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ void TwitchIrcServer::readConnectionMessageReceived(
{
this->addGlobalSystemMessage(
"Twitch Servers requested us to reconnect, reconnecting");
this->markChannelsConnected();
this->connect();
}
else if (command == "GLOBALUSERSTATE")
Expand Down

0 comments on commit e75ce5d

Please sign in to comment.