Skip to content

Commit 8583cdc

Browse files
committed
timeout on disconnect
1 parent f55052e commit 8583cdc

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

hashbot/hashbot.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,20 @@ func (t *HashBot) ReconnectClient() error {
272272
func (t *HashBot) ConnectClient(login string, token string) error {
273273
var err error
274274

275+
ErrDisconnectTimeout := errors.New("disconnect timed out")
276+
275277
if t.TwitchClient != nil {
276-
err = t.TwitchClient.Disconnect()
277-
if !errors.Is(err, twitch.ErrConnectionIsNotOpen) {
278+
result := make(chan error, 1)
279+
go func() {
280+
result <- t.TwitchClient.Disconnect()
281+
}()
282+
select {
283+
case err = <-result:
284+
case <-time.After(time.Second * 5):
285+
err = ErrDisconnectTimeout
286+
}
287+
288+
if !errors.Is(err, twitch.ErrConnectionIsNotOpen) && !errors.Is(err, ErrDisconnectTimeout) {
278289
return err
279290
}
280291
}

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"context"
66
"encoding/json"
77
"errors"
8-
flag "github.com/spf13/pflag"
98
"hashbot/backend"
109
"hashbot/command"
1110
"hashbot/config"
@@ -17,6 +16,8 @@ import (
1716
"sort"
1817
"time"
1918

19+
flag "github.com/spf13/pflag"
20+
2021
"github.com/gempir/go-twitch-irc/v4"
2122
"github.com/rs/zerolog"
2223
"github.com/rs/zerolog/log"

0 commit comments

Comments
 (0)