Fix Twitch event sub reconnect handling

If the connection was aborted by the local machine, the active
subsciptions were not cleared causing the new connection not attempt to
register any new subsciptions.

An event sub connection without any active subsciptions will be dropped
by Twitch after a certain amount of time.

Additionally the reconnection logic was triggering to frequently causing
unecessary load.
This commit is contained in:
WarmUpTill 2026-01-31 17:40:00 +01:00
parent ad1f1effeb
commit 6ba01dca39
2 changed files with 20 additions and 3 deletions

View File

@ -89,12 +89,24 @@ void EventSub::ConnectThread()
void EventSub::WaitAndReconnect()
{
if (_reconnecting) {
return;
}
_reconnecting = true;
auto thread = std::thread([this]() {
std::unique_lock<std::mutex> lock(_waitMtx);
blog(LOG_INFO,
"Twitch EventSub trying to reconnect to in %d seconds.",
reconnectDelay);
_cv.wait_for(lock, std::chrono::seconds(reconnectDelay));
_reconnecting = false;
if (_disconnect) {
return;
}
Connect();
});
thread.detach();
@ -102,6 +114,10 @@ void EventSub::WaitAndReconnect()
void EventSub::Connect()
{
if (_reconnecting) {
return;
}
std::lock_guard<std::mutex> lock(_connectMtx);
if (_connected) {
vblog(LOG_INFO, "Twitch EventSub connect already in progress");
@ -465,8 +481,8 @@ void EventSub::StartServerMigrationClient(const std::string &url)
const auto &data = msg->payload;
if (type == "session_welcome") {
vblog(LOG_INFO,
"Twitch EventSub migration successful - switching to new connection");
blog(LOG_INFO,
"Twitch EventSub migration successful - switching to new connection");
OBSDataAutoRelease session =
obs_data_get_obj(data, "session");
_migrationSessionID =
@ -565,7 +581,7 @@ void EventSub::OnClose(connection_hdl hdl)
blog(LOG_INFO, "Twitch EventSub connection closed: %s / %s (%d)",
msg.c_str(), reason.c_str(), code);
if (_migrating) {
if (!_migrating) {
ClearActiveSubscriptions();
}
_connected = false;

View File

@ -110,6 +110,7 @@ private:
std::condition_variable _cv;
std::atomic_bool _connected{false};
std::atomic_bool _disconnect{false};
std::atomic_bool _reconnecting{false};
std::string _url;
std::string _sessionID;