Use errgroup for a cleaner and no-deadlock implementation of AddNodes

This commit is contained in:
Tyler 2021-01-06 21:00:14 -05:00
parent d99e202995
commit 90365cd9e4
5 changed files with 31 additions and 30 deletions

3
go.mod
View File

@ -1,10 +1,11 @@
module meow.tf/astra/gavalink module meow.tf/astra/gavalink
require ( require (
github.com/bwmarrin/discordgo v0.20.3 // indirect github.com/bwmarrin/discordgo v0.20.3
github.com/foxbot/gavalink v0.0.0-20181105223750-6252b1245300 github.com/foxbot/gavalink v0.0.0-20181105223750-6252b1245300
github.com/gorilla/websocket v1.4.0 github.com/gorilla/websocket v1.4.0
github.com/valyala/fastjson v1.4.1 github.com/valyala/fastjson v1.4.1
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
) )
go 1.13 go 1.13

2
go.sum
View File

@ -8,3 +8,5 @@ github.com/valyala/fastjson v1.4.1 h1:hrltpHpIpkaxll8QltMU8c3QZ5+qIiCL8yKqPFJI/y
github.com/valyala/fastjson v1.4.1/go.mod h1:nV6MsjxL2IMJQUoHDIrjEI7oLyeqK6aBD7EFWPsvP8o= github.com/valyala/fastjson v1.4.1/go.mod h1:nV6MsjxL2IMJQUoHDIrjEI7oLyeqK6aBD7EFWPsvP8o=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

View File

@ -1,7 +1,9 @@
package gavalink package gavalink
import ( import (
"context"
"errors" "errors"
"golang.org/x/sync/errgroup"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -65,41 +67,29 @@ func (l *Lavalink) AddNodes(nodeConfigs ...NodeConfig) error {
Timeout: 60 * time.Second, Timeout: 60 * time.Second,
} }
wg := &sync.WaitGroup{} eg, ctx := errgroup.WithContext(context.Background())
errCh := make(chan error)
for _, c := range nodeConfigs { for _, c := range nodeConfigs {
n := &Node{ eg.Go(func() error {
config: c, n := &Node{
manager: l, config: c,
client: client, manager: l,
} client: client,
}
wg.Add(1) err := n.open(ctx)
go func(n *Node, wg *sync.WaitGroup, errCh chan error) {
defer wg.Done()
err := n.open()
if err != nil { if err != nil {
errCh <- err return err
return
} }
l.nodes = append(l.nodes, n) l.nodes = append(l.nodes, n)
}(n, wg, errCh)
return nil
})
} }
wg.Wait() return eg.Wait()
select {
case err := <-errCh:
return err
default:
return nil
}
} }
// RemoveNode removes a node from the manager // RemoveNode removes a node from the manager

12
node.go
View File

@ -1,6 +1,7 @@
package gavalink package gavalink
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/valyala/fastjson" "github.com/valyala/fastjson"
@ -15,16 +16,21 @@ import (
// NodeConfig configures a Lavalink Node // NodeConfig configures a Lavalink Node
type NodeConfig struct { type NodeConfig struct {
// Node identifier (uuid, hostname, etc)
Identifier string
// REST is the host where Lavalink's REST server runs // REST is the host where Lavalink's REST server runs
// //
// This value is expected without a trailing slash, e.g. like // This value is expected without a trailing slash, e.g. like
// `http://localhost:2333` // `http://localhost:2333`
REST string REST string
// WebSocket is the host where Lavalink's WebSocket server runs // WebSocket is the host where Lavalink's WebSocket server runs
// //
// This value is expected without a trailing slash, e.g. like // This value is expected without a trailing slash, e.g. like
// `http://localhost:8012` // `http://localhost:8012`
WebSocket string WebSocket string
// Password is the expected Authorization header for the Node // Password is the expected Authorization header for the Node
Password string Password string
} }
@ -68,7 +74,7 @@ type FrameStats struct {
} }
// Opens the connection to the Lavalink server // Opens the connection to the Lavalink server
func (node *Node) open() error { func (node *Node) open(ctx context.Context) error {
header := http.Header{} header := http.Header{}
header.Set("User-Agent", gavalinkUserAgent()) header.Set("User-Agent", gavalinkUserAgent())
@ -92,7 +98,7 @@ func (node *Node) open() error {
header.Set("Capabilities", strings.Join(v, ";")) header.Set("Capabilities", strings.Join(v, ";"))
} }
ws, resp, err := websocket.DefaultDialer.Dial(node.config.WebSocket, header) ws, resp, err := websocket.DefaultDialer.DialContext(ctx, node.config.WebSocket, header)
if err != nil { if err != nil {
return err return err
@ -136,7 +142,7 @@ func (node *Node) listen() {
if err != nil { if err != nil {
Log.Println(err) Log.Println(err)
// try to reconnect // try to reconnect
oerr := node.open() oerr := node.open(context.Background())
if oerr != nil { if oerr != nil {
Log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr) Log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr)

View File

@ -184,7 +184,9 @@ func (player *Player) Forward(sessionID string, event VoiceServerUpdate) error {
func (player *Player) ChangeNode(node *Node) error { func (player *Player) ChangeNode(node *Node) error {
player.node = node player.node = node
player.Forward(player.sessionID, player.lastVoiceServerUpdate) if err := player.Forward(player.sessionID, player.lastVoiceServerUpdate); err != nil {
return err
}
return player.PlayAt(player.track, player.position, 0) return player.PlayAt(player.track, player.position, 0)
} }