Stop the 7tv heartbeat timer on shutdown request

This commit is contained in:
Rasmus Karlsson 2023-12-03 13:45:37 +01:00
parent ca97fa4d76
commit 934e518e18
3 changed files with 27 additions and 8 deletions

View file

@ -150,6 +150,15 @@ protected:
return this->started_.load(std::memory_order_acquire); 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 do their own shutdown behaviour
*/
virtual void stopImpl()
{
}
liveupdates::WebsocketClient &websocketClient_; liveupdates::WebsocketClient &websocketClient_;
private: private:
@ -164,6 +173,8 @@ private:
{ {
assert(this->isStarted()); assert(this->isStarted());
this->started_.store(false, std::memory_order_release); this->started_.store(false, std::memory_order_release);
this->stopImpl();
} }
liveupdates::WebsocketHandle handle_; liveupdates::WebsocketHandle handle_;

View file

@ -13,6 +13,8 @@ Client::Client(liveupdates::WebsocketClient &websocketClient,
: BasicPubSubClient<Subscription>(websocketClient, std::move(handle)) : BasicPubSubClient<Subscription>(websocketClient, std::move(handle))
, lastHeartbeat_(std::chrono::steady_clock::now()) , lastHeartbeat_(std::chrono::steady_clock::now())
, heartbeatInterval_(heartbeatInterval) , heartbeatInterval_(heartbeatInterval)
, heartbeatTimer_(std::make_shared<boost::asio::steady_timer>(
this->websocketClient_.get_io_service()))
{ {
} }
@ -23,6 +25,11 @@ void Client::onConnectionEstablished()
this->checkHeartbeat(); this->checkHeartbeat();
} }
void Client::stopImpl()
{
this->heartbeatTimer_->cancel();
}
void Client::setHeartbeatInterval(int intervalMs) void Client::setHeartbeatInterval(int intervalMs)
{ {
qCDebug(chatterinoSeventvEventAPI) qCDebug(chatterinoSeventvEventAPI)
@ -54,8 +61,7 @@ void Client::checkHeartbeat()
auto self = std::dynamic_pointer_cast<Client>(this->shared_from_this()); auto self = std::dynamic_pointer_cast<Client>(this->shared_from_this());
runAfter(this->websocketClient_.get_io_service(), this->heartbeatInterval_, runAfter(this->heartbeatTimer_, this->heartbeatInterval_, [self](auto) {
[self](auto) {
if (!self->isStarted()) if (!self->isStarted())
{ {
return; return;

View file

@ -24,6 +24,7 @@ public:
protected: protected:
void onConnectionEstablished() override; void onConnectionEstablished() override;
void stopImpl() override;
private: private:
void checkHeartbeat(); void checkHeartbeat();
@ -32,6 +33,7 @@ private:
lastHeartbeat_; lastHeartbeat_;
// This will be set once on the welcome message. // This will be set once on the welcome message.
std::chrono::milliseconds heartbeatInterval_; std::chrono::milliseconds heartbeatInterval_;
std::shared_ptr<boost::asio::steady_timer> heartbeatTimer_;
friend SeventvEventAPI; friend SeventvEventAPI;
}; };