mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-08 08:10:08 +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
18
irc.go
18
irc.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
@ -45,9 +46,11 @@ func registerRawMessageHandler(fn plugins.RawMessageHandlerFunc) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
type ircHandler struct {
|
type ircHandler struct {
|
||||||
conn *tls.Conn
|
c *irc.Client
|
||||||
c *irc.Client
|
conn *tls.Conn
|
||||||
user string
|
ctx context.Context
|
||||||
|
ctxCancelFn func()
|
||||||
|
user string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newIRCHandler() (*ircHandler, error) {
|
func newIRCHandler() (*ircHandler, error) {
|
||||||
|
@ -58,6 +61,8 @@ func newIRCHandler() (*ircHandler, error) {
|
||||||
return nil, errors.Wrap(err, "fetching username")
|
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)
|
conn, err := tls.Dial("tcp", "irc.chat.twitch.tv:6697", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "connect to IRC server")
|
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) 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) {
|
func (i ircHandler) ExecuteJoins(channels []string) {
|
||||||
for _, ch := range channels {
|
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) }
|
func (i ircHandler) SendMessage(m *irc.Message) error { return i.c.WriteMessage(m) }
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue