resolve data race conditions, code quality
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/utils"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
livekit "github.com/livekit/livekit-server/proto"
|
||||
@@ -35,7 +36,10 @@ func NewLocalRouter(currentNode LocalNode) *LocalRouter {
|
||||
}
|
||||
|
||||
func (r *LocalRouter) GetNodeForRoom(roomName string) (*livekit.Node, error) {
|
||||
return r.currentNode, nil
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
node := proto.Clone((*livekit.Node)(r.currentNode)).(*livekit.Node)
|
||||
return node, nil
|
||||
}
|
||||
|
||||
func (r *LocalRouter) SetNodeForRoom(roomName string, nodeId string) error {
|
||||
@@ -132,7 +136,9 @@ func (r *LocalRouter) statsWorker() {
|
||||
}
|
||||
// update every 10 seconds
|
||||
<-time.After(statsUpdateInterval)
|
||||
r.lock.Lock()
|
||||
r.currentNode.Stats.UpdatedAt = time.Now().Unix()
|
||||
r.lock.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-15
@@ -63,7 +63,8 @@ func GetLocalIP(stunServers []string) (string, error) {
|
||||
}
|
||||
|
||||
var stunErr error
|
||||
var nodeIp string
|
||||
// sufficiently large buffer to not block it
|
||||
ipChan := make(chan string, 20)
|
||||
err = c.Start(message, func(res stun.Event) {
|
||||
if res.Error != nil {
|
||||
stunErr = res.Error
|
||||
@@ -77,7 +78,7 @@ func GetLocalIP(stunServers []string) (string, error) {
|
||||
}
|
||||
ip := xorAddr.IP.To4()
|
||||
if ip != nil {
|
||||
nodeIp = ip.String()
|
||||
ipChan <- ip.String()
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
@@ -86,21 +87,17 @@ func GetLocalIP(stunServers []string) (string, error) {
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
for nodeIp == "" {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
msg := "could not determine public IP"
|
||||
if stunErr != nil {
|
||||
return "", errors.Wrap(stunErr, msg)
|
||||
} else {
|
||||
return "", fmt.Errorf(msg)
|
||||
}
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
continue
|
||||
select {
|
||||
case nodeIP := <-ipChan:
|
||||
return nodeIP, nil
|
||||
case <-ctx.Done():
|
||||
msg := "could not determine public IP"
|
||||
if stunErr != nil {
|
||||
return "", errors.Wrap(stunErr, msg)
|
||||
} else {
|
||||
return "", fmt.Errorf(msg)
|
||||
}
|
||||
}
|
||||
|
||||
return nodeIp, nil
|
||||
}
|
||||
|
||||
// Creates a hashed ID from a unique string
|
||||
|
||||
Reference in New Issue
Block a user