feat: add old subscription cleanup routine

This commit is contained in:
Ash Keel 2024-04-20 16:07:54 +02:00
parent 7f6e14cd48
commit fe02999663
No known key found for this signature in database
GPG Key ID: 53A9E9A6035DD109
2 changed files with 47 additions and 0 deletions

View File

@ -22,10 +22,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The Twitch chat integration has been rewritten from the ground up to not use an IRC bot and rely on EventSub. This means that you will need to reconfigure your twitch account, especially if you used a different account as the "bot" account. Because of this rewrite, the terminology around chat functionalities have been renamed from "Bot" to "Chat" (e.g. "Bot commands" are now "Chat commands").
- The (i) icon next to "Recent events" in the dashboard now uses a custom tooltip that shows up more consistently.
- The "strimertul is already running" message now pops up from the currently running instance.
- Setting up a secondary user for chat interactions is now much simpler through an auth flow much like the one for setting up the main user.
### Fixed
- Updated Kilovolt to a version that fixes an annoying crash when managing subscriptions.
- A new cleanup routine will remove old eventsub subscriptions that are no longer used. This should remove most of the errors when trying to set new API keys about "too many subscriptions".
### Removed

View File

@ -51,6 +51,9 @@ func Setup(ctx context.Context, twitchAPI *helix.Client, user helix.User, db dat
}
func (c *Client) eventSubLoop() {
// Cleanup subscriptions for dead sessions so we don't get hit by the sub limit
go c.cleanupSubscriptions()
endpoint := websocketEndpoint
var err error
var connection *websocket.Conn
@ -231,6 +234,48 @@ func (c *Client) addSubscriptionsForSession(session string) error {
return nil
}
func (c *Client) cleanupSubscriptions() {
// Clear all subscriptions for dead/broken sessions
c.cleanupSubscriptionForStatus("websocket_disconnected")
c.cleanupSubscriptionForStatus("websocket_failed_ping_pong")
c.cleanupSubscriptionForStatus("websocket_received_inbound_traffic")
c.cleanupSubscriptionForStatus("websocket_connection_unused")
c.cleanupSubscriptionForStatus("websocket_internal_error")
c.cleanupSubscriptionForStatus("websocket_network_timeout")
c.cleanupSubscriptionForStatus("websocket_network_error")
c.cleanupSubscriptionForStatus("websocket_failed_to_reconnect")
}
func (c *Client) cleanupSubscriptionForStatus(status string) {
var cursor string
for {
subscriptions, err := c.twitchAPI.GetEventSubSubscriptions(&helix.EventSubSubscriptionsParams{
Status: status,
After: cursor,
})
if err != nil {
c.logger.Warn("Could not get subscriptions for status", slog.String("status", status), log.Error(err))
}
// Unsubscribe from all subscriptions for dead sessions
for _, sub := range subscriptions.Data.EventSubSubscriptions {
res, err := c.twitchAPI.RemoveEventSubSubscription(sub.ID)
if err != nil {
c.logger.Warn("Could not unsubscribe from dead session", slog.String("session-id", sub.Transport.SessionID), slog.String("subscription-id", sub.ID), log.Error(err))
}
if res.ErrorMessage != "" {
c.logger.Warn("Could not unsubscribe from dead session", slog.String("session-id", sub.Transport.SessionID), slog.String("subscription-id", sub.ID), slog.String("message", res.ErrorMessage), slog.Int("messageCode", res.ErrorStatus))
}
}
// Check for cursor
if subscriptions.Data.Pagination.Cursor == "" {
break
}
cursor = subscriptions.Data.Pagination.Cursor
}
}
func topicCondition(topic string, id string) helix.EventSubCondition {
switch topic {
case helix.EventSubTypeChannelRaid: