config rework to handle distributed setup, fixed redis bugs

This commit is contained in:
David Zhao
2021-01-17 17:06:50 -08:00
parent 30e99e8c6b
commit 458241d1f1
21 changed files with 382 additions and 104 deletions
+2 -1
View File
@@ -3,5 +3,6 @@ package service
import "errors"
var (
ErrRoomNotFound = errors.New("requested room does not exist")
ErrRoomNotFound = errors.New("requested room does not exist")
ErrNoRegisteredNodes = errors.New("there are no registered nodes")
)
+8 -3
View File
@@ -4,6 +4,7 @@ import (
"context"
"github.com/go-redis/redis/v8"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/proto/livekit"
@@ -24,7 +25,8 @@ type RedisRoomStore struct {
func NewRedisRoomStore(rc *redis.Client) *RedisRoomStore {
return &RedisRoomStore{
rc: rc,
ctx: context.Background(),
rc: rc,
}
}
@@ -38,7 +40,10 @@ func (p *RedisRoomStore) CreateRoom(room *livekit.Room) error {
if err != nil {
return err
}
return p.rc.HSet(p.ctx, RoomsKey, room.Name, data).Err()
if err := p.rc.HSet(p.ctx, RoomsKey, room.Name, data).Err(); err != nil {
return errors.Wrap(err, "could not create room")
}
return nil
}
func (p *RedisRoomStore) GetRoom(idOrName string) (*livekit.Room, error) {
@@ -68,7 +73,7 @@ func (p *RedisRoomStore) GetRoom(idOrName string) (*livekit.Room, error) {
func (p *RedisRoomStore) ListRooms() ([]*livekit.Room, error) {
items, err := p.rc.HVals(p.ctx, RoomsKey).Result()
if err != nil && err != redis.Nil {
return nil, err
return nil, errors.Wrap(err, "could not get rooms")
}
rooms := make([]*livekit.Room, 0, len(items))
+21 -1
View File
@@ -4,8 +4,10 @@ import (
"context"
"time"
"github.com/thoas/go-funk"
"github.com/twitchtv/twirp"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/livekit-server/proto/livekit"
)
@@ -13,11 +15,13 @@ import (
// A rooms service that supports a single node
type RoomService struct {
roomProvider RoomStore
router routing.Router
}
func NewRoomService(rp RoomStore) (svc *RoomService, err error) {
func NewRoomService(rp RoomStore, router routing.Router) (svc *RoomService, err error) {
svc = &RoomService{
roomProvider: rp,
router: router,
}
return
@@ -40,6 +44,22 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
return
}
// allocate room to a node
nodes, err := s.router.ListNodes()
if err != nil {
return
}
if len(nodes) == 0 {
return nil, ErrNoRegisteredNodes
}
idx := funk.RandomInt(0, len(nodes))
node := nodes[idx]
if err = s.router.SetNodeForRoom(req.Name, node.Id); err != nil {
return
}
return
}
+5 -2
View File
@@ -11,9 +11,8 @@ import (
"github.com/livekit/livekit-server/proto/livekit"
)
// RTC runner manages the lifecycles of a WebRTC connection
// RTC runner manages the lifecycles of all WebRTC connections
// it creates a new goroutine for each participant it manages.
type RTCRunner struct {
lock sync.RWMutex
roomProvider RoomStore
@@ -106,12 +105,16 @@ func (r *RTCRunner) sessionWorker(room *rtc.Room, participant types.Participant,
)
// remove peer from room when participant leaves room
room.RemoveParticipant(participant.ID())
// TODO: notify router to cleanup?
}()
defer rtc.Recover()
for {
obj, err := requestSource.ReadMessage()
if err == io.EOF {
// TODO: when request is EOF, we might be better off requesting a new connection and waiting
// RTC connection terminating should be the only case that we exit
return
}
+1 -1
View File
@@ -81,7 +81,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
participantId := utils.NewGuid(utils.ParticipantPrefix)
err = s.router.StartParticipant(roomName, participantId, pName)
if err != nil {
handleError(w, http.StatusInternalServerError, "could not set signal node: "+err.Error())
handleError(w, http.StatusInternalServerError, "could not start session: "+err.Error())
return
}
+1 -1
View File
@@ -15,7 +15,7 @@ import (
// Injectors from wire.go:
func InitializeServer(conf *config.Config, keyProvider auth.KeyProvider, roomStore RoomStore, router routing.Router, currentNode routing.LocalNode) (*LivekitServer, error) {
roomService, err := NewRoomService(roomStore)
roomService, err := NewRoomService(roomStore, router)
if err != nil {
return nil, err
}