perf: query fewer historical messages on reconnects (#5001)

Co-authored-by: Ruben Anders <ruben.anders@robotty.de>
Co-authored-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
iProdigy
2023-12-09 10:46:30 -08:00
committed by GitHub
parent 401e097d62
commit 13dc306506
10 changed files with 171 additions and 77 deletions
@@ -1136,6 +1136,7 @@ void IrcMessageHandler::handleJoinMessage(Communi::IrcMessage *message)
getApp()->accounts->twitch.getCurrent()->getUserName())
{
twitchChannel->addMessage(makeSystemMessage("joined channel"));
twitchChannel->joined.invoke();
}
else if (getSettings()->showJoins.getValue())
{
+42 -11
View File
@@ -103,17 +103,12 @@ 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->connected.connect([this]() {
if (this->roomId().isEmpty())
std::ignore = this->joined.connect([this]() {
if (this->disconnectedAt_.has_value())
{
// If we get a reconnected event when the room id is not set, we
// just connected for the first time. After receiving the first
// message from a channel, setRoomId is called and further
// invocations of this event will load recent messages.
return;
this->loadRecentMessagesReconnect();
this->disconnectedAt_ = std::nullopt;
}
this->loadRecentMessagesReconnect();
});
// timers
@@ -1111,6 +1106,24 @@ bool TwitchChannel::setLive(bool newLiveStatus)
return true;
}
void TwitchChannel::markDisconnectedNow()
{
if (this->roomId().isEmpty())
{
// we were never joined in the first place
return;
}
if (this->disconnectedAt_.has_value())
{
// don't overwrite prior timestamp since
// a reconnection hasn't happened yet
return;
}
this->disconnectedAt_ = std::chrono::system_clock::now();
}
void TwitchChannel::loadRecentMessages()
{
if (!getSettings()->loadTwitchMessageHistoryOnConnect)
@@ -1163,7 +1176,9 @@ void TwitchChannel::loadRecentMessages()
return;
tc->loadingRecentMessages_.clear();
});
},
getSettings()->twitchMessageHistoryLimit.getValue(), std::nullopt,
std::nullopt, false);
}
void TwitchChannel::loadRecentMessagesReconnect()
@@ -1178,6 +1193,21 @@ void TwitchChannel::loadRecentMessagesReconnect()
return; // already loading
}
const auto now = std::chrono::system_clock::now();
int limit = getSettings()->twitchMessageHistoryLimit.getValue();
if (this->disconnectedAt_.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())
.count();
limit =
std::min(static_cast<int>(secondsSinceDisconnect + 1) * 10, limit);
}
auto weak = weakOf<Channel>(this);
recentmessages::load(
this->getName(), weak,
@@ -1203,7 +1233,8 @@ void TwitchChannel::loadRecentMessagesReconnect()
return;
tc->loadingRecentMessages_.clear();
});
},
limit, this->disconnectedAt_, now, true);
}
void TwitchChannel::refreshPubSub()
+13
View File
@@ -136,6 +136,12 @@ public:
SharedAccessGuard<const RoomModes> accessRoomModes() const;
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
*/
void markDisconnectedNow();
// Emotes
std::optional<EmotePtr> bttvEmote(const EmoteName &name) const;
std::optional<EmotePtr> ffzEmote(const EmoteName &name) const;
@@ -200,6 +206,11 @@ public:
*/
std::shared_ptr<MessageThread> getOrCreateThread(const MessagePtr &message);
/**
* This signal fires when the local user has joined the channel
**/
pajlada::Signals::NoArgSignal joined;
// Only TwitchChannel may invoke this signal
pajlada::Signals::NoArgSignal userStateChanged;
@@ -353,6 +364,8 @@ private:
int chatterCount_{};
UniqueAccess<StreamStatus> streamStatus_;
UniqueAccess<RoomModes> roomModes_;
std::optional<std::chrono::time_point<std::chrono::system_clock>>
disconnectedAt_{};
std::atomic_flag loadingRecentMessages_ = ATOMIC_FLAG_INIT;
std::unordered_map<QString, std::weak_ptr<MessageThread>> threads_;