2018-08-24 01:24:20 +00:00
|
|
|
package gavalink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
2019-10-13 04:42:08 +00:00
|
|
|
"net/http"
|
2018-08-24 01:24:20 +00:00
|
|
|
"os"
|
2020-06-20 03:19:33 +00:00
|
|
|
"runtime"
|
2020-06-20 02:50:55 +00:00
|
|
|
"sync"
|
2019-10-13 04:42:08 +00:00
|
|
|
"time"
|
2018-08-24 01:24:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Log sets the log.Logger gavalink will write to
|
|
|
|
var Log *log.Logger
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Log = log.New(os.Stdout, "(gavalink) ", 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lavalink manages a connection to Lavalink Nodes
|
|
|
|
type Lavalink struct {
|
2020-06-20 03:19:33 +00:00
|
|
|
shards int
|
2018-08-24 01:24:20 +00:00
|
|
|
userID string
|
|
|
|
|
2020-06-20 03:19:33 +00:00
|
|
|
nodes []*Node
|
|
|
|
|
|
|
|
players map[string]*Player
|
|
|
|
playersMu sync.RWMutex
|
2019-10-13 03:57:59 +00:00
|
|
|
|
2020-06-20 02:50:55 +00:00
|
|
|
// Event handlers
|
|
|
|
handlersMu sync.RWMutex
|
|
|
|
handlers map[string][]*eventHandlerInstance
|
|
|
|
onceHandlers map[string][]*eventHandlerInstance
|
|
|
|
|
2019-10-13 18:07:59 +00:00
|
|
|
capabilities map[string]interface{}
|
|
|
|
|
2019-10-13 03:57:59 +00:00
|
|
|
BestNodeFunc func([]*Node) (*Node, error)
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
errNoNodes = errors.New("No nodes present")
|
|
|
|
errNodeNotFound = errors.New("Couldn't find that node")
|
|
|
|
errPlayerNotFound = errors.New("Couldn't find a player for that guild")
|
|
|
|
errVolumeOutOfRange = errors.New("Volume is out of range, must be within [0, 1000]")
|
|
|
|
errInvalidVersion = errors.New("This library requires Lavalink >= 3")
|
|
|
|
errUnknownPayload = errors.New("Lavalink sent an unknown payload")
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewLavalink creates a new Lavalink manager
|
2020-06-20 03:19:33 +00:00
|
|
|
func NewLavalink(shards int, userID string) *Lavalink {
|
2018-08-24 01:24:20 +00:00
|
|
|
return &Lavalink{
|
2019-10-13 03:57:59 +00:00
|
|
|
shards: shards,
|
|
|
|
userID: userID,
|
2018-08-24 01:24:20 +00:00
|
|
|
players: make(map[string]*Player),
|
2019-10-13 03:57:59 +00:00
|
|
|
|
2019-10-13 08:39:18 +00:00
|
|
|
BestNodeFunc: BestNodeByPenalties,
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddNodes adds a node to the Lavalink manager
|
2020-06-20 03:19:33 +00:00
|
|
|
// This function calls all of the node connect methods at once.
|
|
|
|
// TODO perhaps add a pool/max at a time limit?
|
2020-06-20 02:50:55 +00:00
|
|
|
func (l *Lavalink) AddNodes(nodeConfigs ...NodeConfig) error {
|
2019-10-13 04:42:08 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 60 * time.Second,
|
|
|
|
}
|
|
|
|
|
2020-06-20 03:19:33 +00:00
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
for _, c := range nodeConfigs {
|
2019-10-13 03:57:59 +00:00
|
|
|
n := &Node{
|
2018-08-24 01:24:20 +00:00
|
|
|
config: c,
|
2020-06-20 02:50:55 +00:00
|
|
|
manager: l,
|
2019-10-13 04:42:08 +00:00
|
|
|
client: client,
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2020-06-20 03:19:33 +00:00
|
|
|
wg.Add(1)
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2020-06-20 03:19:33 +00:00
|
|
|
go func(n *Node, wg *sync.WaitGroup, errCh chan error) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := n.open()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errCh <- err
|
|
|
|
return
|
|
|
|
}
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2020-06-20 03:19:33 +00:00
|
|
|
l.nodes = append(l.nodes, n)
|
|
|
|
}(n, wg, errCh)
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2020-06-20 03:19:33 +00:00
|
|
|
wg.Wait()
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2020-06-20 03:19:33 +00:00
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveNode removes a node from the manager
|
2020-06-20 02:50:55 +00:00
|
|
|
func (l *Lavalink) removeNode(node *Node) error {
|
2018-08-24 01:24:20 +00:00
|
|
|
idx := -1
|
2020-06-20 02:50:55 +00:00
|
|
|
for i, n := range l.nodes {
|
2019-10-13 03:57:59 +00:00
|
|
|
if n == node {
|
2018-08-24 01:24:20 +00:00
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx == -1 {
|
|
|
|
return errNodeNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
node.stop()
|
|
|
|
|
2020-06-22 02:53:33 +00:00
|
|
|
danglingPlayers := make([]*Player, 0)
|
|
|
|
|
2020-06-20 03:19:33 +00:00
|
|
|
l.playersMu.RLock()
|
2020-06-20 02:50:55 +00:00
|
|
|
for _, player := range l.players {
|
2019-10-13 03:57:59 +00:00
|
|
|
if player.node == node {
|
2020-06-22 02:53:33 +00:00
|
|
|
player.node = nil
|
|
|
|
|
2020-06-20 02:50:55 +00:00
|
|
|
n, err := l.BestNode()
|
2019-10-13 03:57:59 +00:00
|
|
|
|
2020-06-22 02:53:33 +00:00
|
|
|
if err != nil || n == nil {
|
|
|
|
danglingPlayers = append(danglingPlayers, player)
|
2019-10-13 03:57:59 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
player.ChangeNode(n)
|
|
|
|
}
|
|
|
|
}
|
2020-06-20 03:19:33 +00:00
|
|
|
l.playersMu.RUnlock()
|
2019-10-13 03:57:59 +00:00
|
|
|
|
2020-06-22 02:53:33 +00:00
|
|
|
if len(danglingPlayers) > 0 {
|
|
|
|
for _, player := range danglingPlayers {
|
|
|
|
player.Destroy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
// temp var for easier reading
|
2020-06-20 02:50:55 +00:00
|
|
|
n := l.nodes
|
2018-08-24 01:24:20 +00:00
|
|
|
z := len(n) - 1
|
|
|
|
|
|
|
|
n[idx] = n[z] // swap idx with last
|
|
|
|
n = n[:z]
|
|
|
|
|
2020-06-20 02:50:55 +00:00
|
|
|
l.nodes = n
|
2018-08-24 01:24:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BestNode returns the Node with the lowest latency
|
2020-06-20 02:50:55 +00:00
|
|
|
func (l *Lavalink) BestNode() (*Node, error) {
|
|
|
|
if len(l.nodes) < 1 {
|
2018-08-24 01:24:20 +00:00
|
|
|
return nil, errNoNodes
|
|
|
|
}
|
|
|
|
|
2020-06-20 02:50:55 +00:00
|
|
|
return l.BestNodeFunc(l.nodes)
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPlayer gets a player for a guild
|
2020-06-20 02:50:55 +00:00
|
|
|
func (l *Lavalink) GetPlayer(guild string) (*Player, error) {
|
2020-06-20 03:19:33 +00:00
|
|
|
l.playersMu.RLock()
|
|
|
|
defer l.playersMu.RUnlock()
|
|
|
|
|
2020-06-20 02:50:55 +00:00
|
|
|
p, ok := l.players[guild]
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errPlayerNotFound
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
2019-10-13 18:07:59 +00:00
|
|
|
|
|
|
|
// Add capabilities mappings to the client, letting the server know what we support
|
2020-06-20 02:50:55 +00:00
|
|
|
func (l *Lavalink) AddCapability(key string, i interface{}) {
|
|
|
|
if l.capabilities == nil {
|
|
|
|
l.capabilities = make(map[string]interface{})
|
2019-10-13 18:07:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-20 02:50:55 +00:00
|
|
|
l.capabilities[key] = i
|
2019-10-13 18:07:59 +00:00
|
|
|
}
|
2020-06-20 03:19:33 +00:00
|
|
|
|
|
|
|
func gavalinkUserAgent() string {
|
|
|
|
return "Gavalink (v1.0, " + runtime.Version() + ")"
|
|
|
|
}
|