From cb68b029ec0232484baba4798006a5877b470328 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 5 Nov 2023 11:50:56 +0100 Subject: [PATCH] [core] Add timeout to eventsub connection dialer Signed-off-by: Knut Ahlers --- pkg/twitch/eventsubWebsocketClient.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/twitch/eventsubWebsocketClient.go b/pkg/twitch/eventsubWebsocketClient.go index 2347154..838b70c 100644 --- a/pkg/twitch/eventsubWebsocketClient.go +++ b/pkg/twitch/eventsubWebsocketClient.go @@ -18,6 +18,7 @@ import ( const ( eventsubLiveSocketDest = "wss://eventsub.wss.twitch.tv/ws" + socketConnectTimeout = 15 * time.Second socketInitialTimeout = 30 * time.Second socketTimeoutGraceMultiplier = 1.5 ) @@ -242,7 +243,10 @@ func (e *EventSubSocketClient) Run() error { func (e *EventSubSocketClient) connect(url string, msgC chan eventSubSocketMessage, errC chan error, reason string) error { e.logger.WithField("reason", reason).Debug("(re-)connecting websocket") - conn, _, err := websocket.DefaultDialer.Dial(url, nil) //nolint:bodyclose // Close is implemented at other place + ctx, cancel := context.WithTimeout(context.Background(), socketConnectTimeout) + defer cancel() + + conn, _, err := websocket.DefaultDialer.DialContext(ctx, url, nil) //nolint:bodyclose // Close is implemented at other place if err != nil { return errors.Wrap(err, "dialing websocket") } @@ -403,7 +407,7 @@ func (e *EventSubSocketClient) subscribe() error { return errors.Wrapf(err, "subscribing to %s/%s", st.Event, st.Version) } - e.logger.WithField("topic", strings.Join([]string{st.Event, st.Version}, "/")).Debug("subscripted to topic") + e.logger.WithField("topic", strings.Join([]string{st.Event, st.Version}, "/")).Debug("subscribed to topic") } return nil