fix: cleanly exit on shutdown (#5537)

Co-authored-by: Mm2PL <mm2pl+gh@kotmisia.pl>
Co-authored-by: Nerixyz <nerixdev@outlook.de>
This commit is contained in:
pajlada
2024-08-10 14:24:25 +02:00
committed by GitHub
parent daff83dde8
commit 74d65a345d
23 changed files with 134 additions and 65 deletions
+5 -1
View File
@@ -1,13 +1,17 @@
#include "providers/bttv/BttvLiveUpdates.hpp"
#include "common/Literals.hpp"
#include <QJsonDocument>
#include <utility>
namespace chatterino {
using namespace chatterino::literals;
BttvLiveUpdates::BttvLiveUpdates(QString host)
: BasicPubSubManager(std::move(host))
: BasicPubSubManager(std::move(host), u"BTTV"_s)
{
}
@@ -150,6 +150,15 @@ protected:
return this->started_.load(std::memory_order_acquire);
}
/**
* @brief Will be called when the clients has been requested to stop
*
* Derived classes can override this to implement their own shutdown behaviour
*/
virtual void stopImpl()
{
}
liveupdates::WebsocketClient &websocketClient_;
private:
@@ -164,6 +173,8 @@ private:
{
assert(this->isStarted());
this->started_.store(false, std::memory_order_release);
this->stopImpl();
}
liveupdates::WebsocketHandle handle_;
@@ -8,10 +8,12 @@
#include "providers/twitch/PubSubHelpers.hpp"
#include "util/DebugCount.hpp"
#include "util/ExponentialBackoff.hpp"
#include "util/RenameThread.hpp"
#include <pajlada/signals/signal.hpp>
#include <QJsonObject>
#include <QString>
#include <QStringBuilder>
#include <websocketpp/client.hpp>
#include <algorithm>
@@ -59,8 +61,9 @@ template <typename Subscription>
class BasicPubSubManager
{
public:
BasicPubSubManager(QString host)
BasicPubSubManager(QString host, QString shortName)
: host_(std::move(host))
, shortName_(std::move(shortName))
{
this->websocketClient_.set_access_channels(
websocketpp::log::alevel::all);
@@ -94,7 +97,10 @@ public:
.toStdString());
}
virtual ~BasicPubSubManager() = default;
virtual ~BasicPubSubManager()
{
this->stop();
}
BasicPubSubManager(const BasicPubSubManager &) = delete;
BasicPubSubManager(const BasicPubSubManager &&) = delete;
@@ -115,6 +121,8 @@ public:
this->mainThread_.reset(new std::thread([this] {
runThread();
}));
renameThread(*this->mainThread_.get(), "BPSM-" % this->shortName_);
}
void stop()
@@ -373,6 +381,9 @@ private:
const QString host_;
/// Short name of the service (e.g. "7TV" or "BTTV")
const QString shortName_;
bool stopping_{false};
};
+3 -1
View File
@@ -1,6 +1,7 @@
#include "providers/seventv/SeventvEventAPI.hpp"
#include "Application.hpp"
#include "common/Literals.hpp"
#include "providers/seventv/eventapi/Client.hpp"
#include "providers/seventv/eventapi/Dispatch.hpp"
#include "providers/seventv/eventapi/Message.hpp"
@@ -16,10 +17,11 @@ namespace chatterino {
using namespace seventv;
using namespace seventv::eventapi;
using namespace chatterino::literals;
SeventvEventAPI::SeventvEventAPI(
QString host, std::chrono::milliseconds defaultHeartbeatInterval)
: BasicPubSubManager(std::move(host))
: BasicPubSubManager(std::move(host), u"7TV"_s)
, heartbeatInterval_(defaultHeartbeatInterval)
{
}
+14 -8
View File
@@ -13,9 +13,16 @@ Client::Client(liveupdates::WebsocketClient &websocketClient,
: BasicPubSubClient<Subscription>(websocketClient, std::move(handle))
, lastHeartbeat_(std::chrono::steady_clock::now())
, heartbeatInterval_(heartbeatInterval)
, heartbeatTimer_(std::make_shared<boost::asio::steady_timer>(
this->websocketClient_.get_io_service()))
{
}
void Client::stopImpl()
{
this->heartbeatTimer_->cancel();
}
void Client::onConnectionEstablished()
{
this->lastHeartbeat_.store(std::chrono::steady_clock::now(),
@@ -54,14 +61,13 @@ void Client::checkHeartbeat()
auto self = std::dynamic_pointer_cast<Client>(this->shared_from_this());
runAfter(this->websocketClient_.get_io_service(), this->heartbeatInterval_,
[self](auto) {
if (!self->isStarted())
{
return;
}
self->checkHeartbeat();
});
runAfter(this->heartbeatTimer_, this->heartbeatInterval_, [self](auto) {
if (!self->isStarted())
{
return;
}
self->checkHeartbeat();
});
}
} // namespace chatterino::seventv::eventapi
@@ -19,6 +19,8 @@ public:
liveupdates::WebsocketHandle handle,
std::chrono::milliseconds heartbeatInterval);
void stopImpl() override;
void setHeartbeatInterval(int intervalMs);
void handleHeartbeat();
@@ -32,6 +34,7 @@ private:
lastHeartbeat_;
// This will be set once on the welcome message.
std::chrono::milliseconds heartbeatInterval_;
std::shared_ptr<boost::asio::steady_timer> heartbeatTimer_;
friend SeventvEventAPI;
};