From d99e2029953e5d7ec609c523c69a1278245f5c4d Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 21 Jun 2020 22:53:33 -0400 Subject: [PATCH] Handle node removal better to ensure players aren't used if there's no node --- go.mod | 1 + go.sum | 4 ++++ lavalink.go | 13 ++++++++++++- player.go | 15 +++++++-------- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index d8f3feb..45749d6 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module meow.tf/astra/gavalink require ( + github.com/bwmarrin/discordgo v0.20.3 // indirect github.com/foxbot/gavalink v0.0.0-20181105223750-6252b1245300 github.com/gorilla/websocket v1.4.0 github.com/valyala/fastjson v1.4.1 diff --git a/go.sum b/go.sum index 2460f24..9f8fa13 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,10 @@ +github.com/bwmarrin/discordgo v0.20.3 h1:AxjcHGbyBFSC0a3Zx5nDQwbOjU7xai5dXjRnZ0YB7nU= +github.com/bwmarrin/discordgo v0.20.3/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q= github.com/foxbot/gavalink v0.0.0-20181105223750-6252b1245300 h1:/p4AzwhPqvi9V0Ktsd+4jpkTpkLbrns2AyCUrylJFDo= github.com/foxbot/gavalink v0.0.0-20181105223750-6252b1245300/go.mod h1:MDlIQ50PLaU0fUW0JcHFOxec8Q17F9byrnGi8ok5vVQ= github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= github.com/valyala/fastjson v1.4.1 h1:hrltpHpIpkaxll8QltMU8c3QZ5+qIiCL8yKqPFJI/yE= 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/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/lavalink.go b/lavalink.go index e9e2015..8631c5e 100644 --- a/lavalink.go +++ b/lavalink.go @@ -117,12 +117,17 @@ func (l *Lavalink) removeNode(node *Node) error { node.stop() + danglingPlayers := make([]*Player, 0) + l.playersMu.RLock() for _, player := range l.players { if player.node == node { + player.node = nil + n, err := l.BestNode() - if err != nil { + if err != nil || n == nil { + danglingPlayers = append(danglingPlayers, player) continue } @@ -131,6 +136,12 @@ func (l *Lavalink) removeNode(node *Node) error { } l.playersMu.RUnlock() + if len(danglingPlayers) > 0 { + for _, player := range danglingPlayers { + player.Destroy() + } + } + // temp var for easier reading n := l.nodes z := len(n) - 1 diff --git a/player.go b/player.go index a32295a..5c7bcf3 100644 --- a/player.go +++ b/player.go @@ -191,15 +191,14 @@ func (player *Player) ChangeNode(node *Node) error { // Destroy will destroy this player func (player *Player) Destroy() error { - msg := basicMessage{ - Op: opDestroy, - GuildID: player.guildID, - } + if player.node != nil && player.node.wsConn != nil { + msg := basicMessage{ + Op: opDestroy, + GuildID: player.guildID, + } - err := player.node.wsConn.WriteJSON(msg) - - if err != nil { - return err + // We don't actually care if this goes through, since the node/connection may be invalid anyway. + player.node.wsConn.WriteJSON(msg) } player.manager.playersMu.Lock()