Skip to content

Commit

Permalink
Merge pull request #42 from disgoorg/fix/reconnecting
Browse files Browse the repository at this point in the history
fix reconnecting logic not reconnecting on 1006
  • Loading branch information
topi314 authored May 21, 2022
2 parents 2ddd6b2 + b8ab343 commit b2c1d52
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 35 deletions.
2 changes: 1 addition & 1 deletion disgolink/_example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
Expand Down
3 changes: 2 additions & 1 deletion disgolink/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
)
6 changes: 4 additions & 2 deletions disgolink/go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=
63 changes: 32 additions & 31 deletions lavalink/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package lavalink
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"sync"
"time"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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()
Expand All @@ -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" {
Expand All @@ -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
}
Expand Down

0 comments on commit b2c1d52

Please sign in to comment.