diff --git a/disgolink/_example/example.go b/disgolink/_example/example.go index 54759e1..3f2cfbe 100644 --- a/disgolink/_example/example.go +++ b/disgolink/_example/example.go @@ -38,7 +38,7 @@ func main() { var err error client, err = disgo.New(token, bot.WithGatewayConfigOpts(gateway.WithGatewayIntents(discord.GatewayIntentGuilds|discord.GatewayIntentGuildVoiceStates)), - bot.WithCacheConfigOpts(cache.WithCacheFlags(cache.FlagsDefault)), + bot.WithCacheConfigOpts(cache.WithCacheFlags(cache.FlagVoiceStates)), bot.WithEventListeners(&events.ListenerAdapter{ OnApplicationCommandInteraction: onApplicationCommand, }), diff --git a/disgolink/go.mod b/disgolink/go.mod index c1c4c12..be8e55a 100644 --- a/disgolink/go.mod +++ b/disgolink/go.mod @@ -3,7 +3,7 @@ module github.com/disgoorg/disgolink/disgolink go 1.18 require ( - github.com/disgoorg/disgo v0.10.2 + github.com/disgoorg/disgo v0.11.5 github.com/disgoorg/disgolink/lavalink v1.7.0 ) @@ -12,4 +12,5 @@ require ( github.com/disgoorg/snowflake/v2 v2.0.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect + golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 // indirect ) diff --git a/disgolink/go.sum b/disgolink/go.sum index ecd3550..03905a3 100644 --- a/disgolink/go.sum +++ b/disgolink/go.sum @@ -1,6 +1,6 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/disgoorg/disgo v0.10.2 h1:LUJjll/fqdBGJglIt8OSzxvJhn6xByTXVGPQQdtHk98= -github.com/disgoorg/disgo v0.10.2/go.mod h1:Cyip4bCYHD3rHgDhBPT9cLo81e9AMbDe8ocM50UNRM4= +github.com/disgoorg/disgo v0.11.5 h1:cIFtwlT8M7RRFZC92yG5wLAdNDWUdD483Qm3ardq/fM= +github.com/disgoorg/disgo v0.11.5/go.mod h1:Cyip4bCYHD3rHgDhBPT9cLo81e9AMbDe8ocM50UNRM4= github.com/disgoorg/disgolink/lavalink v1.7.0 h1:TdFniCEsOTzza6vg2FklAuUVJhfzjCj1aV1p52pRwfo= github.com/disgoorg/disgolink/lavalink v1.7.0/go.mod h1:e/oo6afxnHvjkgGeJYrDazVIJEsQuWVSFahsek+K1mo= github.com/disgoorg/log v1.2.0 h1:sqlXnu/ZKAlIlHV9IO+dbMto7/hCQ474vlIdMWk8QKo= @@ -13,4 +13,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b h1:qYTY2tN72LhgDj2rtWG+LI6TXFl2ygFQQ4YezfVaGQE= github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8 h1:Xt4/LzbTwfocTk9ZLEu4onjeFucl88iW+v4j4PWbQuE= +golang.org/x/exp v0.0.0-20220325121720-054d8573a5d8/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= diff --git a/lavalink/node.go b/lavalink/node.go index 0337e30..0afdada 100644 --- a/lavalink/node.go +++ b/lavalink/node.go @@ -3,7 +3,9 @@ package lavalink import ( "context" "encoding/json" + "errors" "fmt" + "net" "net/http" "sync" "time" @@ -120,37 +122,50 @@ func (n *nodeImpl) Stats() *Stats { return n.stats } -func (n *nodeImpl) reconnect() error { +func (n *nodeImpl) reconnect(ctx context.Context) { + if err := n.reconnectTry(ctx, 0, time.Second); err != nil { + n.lavalink.Logger().Error("failed to reconnect to node: ", err) + } +} + +func (n *nodeImpl) reconnectTry(ctx context.Context, try int, delay time.Duration) error { n.statusMu.Lock() defer n.statusMu.Unlock() - n.status = Reconnecting - if err := n.open(context.TODO(), 0); err != nil { + + timer := time.NewTimer(time.Duration(try) * delay) + defer timer.Stop() + select { + case <-ctx.Done(): + timer.Stop() + return ctx.Err() + case <-timer.C: + } + + n.lavalink.Logger().Debug("reconnecting gateway...") + if err := n.open(ctx); err != nil { + n.lavalink.Logger().Error("failed to reconnect node. error: ", err) n.status = Disconnected - return err + return n.reconnectTry(ctx, try+1, delay) } n.status = Connected return nil } func (n *nodeImpl) listen() { - defer func() { - n.lavalink.Logger().Info("shut down listen goroutine") - }() + defer n.lavalink.Logger().Debug("shutting down listen goroutine") +loop: for { if n.conn == nil { return } _, data, err := n.conn.ReadMessage() if err != nil { - if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { - n.lavalink.Logger().Error("error while reading from lavalink websocket. error: ", err) - n.Close() - if err = n.reconnect(); err != nil { - n.lavalink.Logger().Error("error while reconnecting to lavalink websocket. error: ", err) - } + n.Close() + if !errors.Is(err, net.ErrClosed) { + go n.reconnect(context.TODO()) } - return + break loop } n.lavalink.Logger().Trace("received: ", string(data)) @@ -287,7 +302,7 @@ func (n *nodeImpl) onStatsEvent(stats StatsOp) { n.stats = &stats.Stats } -func (n *nodeImpl) open(ctx context.Context, delay time.Duration) error { +func (n *nodeImpl) open(ctx context.Context) error { select { case <-ctx.Done(): return ctx.Err() @@ -312,21 +327,7 @@ func (n *nodeImpl) open(ctx context.Context, delay time.Duration) error { ) n.conn, rs, err = websocket.DefaultDialer.DialContext(ctx, fmt.Sprintf("%s://%s:%s", scheme, n.config.Host, n.config.Port), header) if err != nil { - n.lavalink.Logger().Warnf("error while connecting to lavalink websocket, retrying in %f seconds: %s", delay.Seconds(), err) - if delay > 0 { - select { - case <-ctx.Done(): - return ctx.Err() - case <-time.After(delay): - } - - } else { - delay = 1 * time.Second - } - if delay < 30*time.Second { - delay *= 2 - } - return n.open(ctx, delay) + return err } if n.config.ResumingKey != "" { if rs.Header.Get("Session-Resumed") == "true" { @@ -352,7 +353,7 @@ func (n *nodeImpl) Open(ctx context.Context) error { defer n.statusMu.Unlock() n.status = Connecting - if err := n.open(ctx, 0); err != nil { + if err := n.open(ctx); err != nil { n.status = Disconnected return err }