mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-12-30 00:21:16 +00:00
[core] Utilize clean shutdown in go-irc v4
in order to maybe less often throw errors when re-using closed connections Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
bcc5b4eba7
commit
4a15a7bf35
1 changed files with 13 additions and 5 deletions
14
irc.go
14
irc.go
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"math"
|
||||
|
@ -45,8 +46,10 @@ func registerRawMessageHandler(fn plugins.RawMessageHandlerFunc) error {
|
|||
}
|
||||
|
||||
type ircHandler struct {
|
||||
conn *tls.Conn
|
||||
c *irc.Client
|
||||
conn *tls.Conn
|
||||
ctx context.Context
|
||||
ctxCancelFn func()
|
||||
user string
|
||||
}
|
||||
|
||||
|
@ -58,6 +61,8 @@ func newIRCHandler() (*ircHandler, error) {
|
|||
return nil, errors.Wrap(err, "fetching username")
|
||||
}
|
||||
|
||||
h.ctx, h.ctxCancelFn = context.WithCancel(context.Background())
|
||||
|
||||
conn, err := tls.Dial("tcp", "irc.chat.twitch.tv:6697", nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "connect to IRC server")
|
||||
|
@ -86,7 +91,10 @@ func newIRCHandler() (*ircHandler, error) {
|
|||
|
||||
func (i ircHandler) Client() *irc.Client { return i.c }
|
||||
|
||||
func (i ircHandler) Close() error { return i.conn.Close() }
|
||||
func (i ircHandler) Close() error {
|
||||
i.ctxCancelFn()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (i ircHandler) ExecuteJoins(channels []string) {
|
||||
for _, ch := range channels {
|
||||
|
@ -197,7 +205,7 @@ func (i ircHandler) Handle(c *irc.Client, m *irc.Message) {
|
|||
}
|
||||
}
|
||||
|
||||
func (i ircHandler) Run() error { return errors.Wrap(i.c.Run(), "running IRC client") }
|
||||
func (i ircHandler) Run() error { return errors.Wrap(i.c.RunContext(i.ctx), "running IRC client") }
|
||||
|
||||
func (i ircHandler) SendMessage(m *irc.Message) error { return i.c.WriteMessage(m) }
|
||||
|
||||
|
|
Loading…
Reference in a new issue