reuse the same Participant ID within the same room/session
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package routing_test
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
func TestMessageChannel_WriteMessageClosed(t *testing.T) {
|
||||
// ensure it doesn't panic when written to after closing
|
||||
m := routing.NewMessageChannel()
|
||||
go func() {
|
||||
for msg := range m.ReadChan() {
|
||||
if msg == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
wg := sync.WaitGroup{}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < 100; i++ {
|
||||
m.WriteMessage(&livekit.RouterMessage{})
|
||||
}
|
||||
}()
|
||||
m.WriteMessage(&livekit.RouterMessage{})
|
||||
m.Close()
|
||||
m.WriteMessage(&livekit.RouterMessage{})
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"github.com/twitchtv/twirp"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/auth"
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -132,9 +131,3 @@ func EnsureListPermission(ctx context.Context) error {
|
||||
func twirpAuthError(err error) error {
|
||||
return twirp.NewError(twirp.Unauthenticated, err.Error())
|
||||
}
|
||||
|
||||
func handleError(w http.ResponseWriter, status int, msg string) {
|
||||
logger.Debugw("error handling request", "error", msg, "status", status)
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(msg))
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/thoas/go-funk"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
@@ -15,14 +16,17 @@ type LocalRoomStore struct {
|
||||
rooms map[string]*livekit.Room
|
||||
// map of roomName => roomId
|
||||
roomIds map[string]string
|
||||
lock sync.RWMutex
|
||||
// map of roomName => { participantName: participantId }
|
||||
participantIds map[string]map[string]string
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
func NewLocalRoomStore() *LocalRoomStore {
|
||||
return &LocalRoomStore{
|
||||
rooms: make(map[string]*livekit.Room),
|
||||
roomIds: make(map[string]string),
|
||||
lock: sync.RWMutex{},
|
||||
rooms: make(map[string]*livekit.Room),
|
||||
roomIds: make(map[string]string),
|
||||
participantIds: make(map[string]map[string]string),
|
||||
lock: sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,3 +77,22 @@ func (p *LocalRoomStore) DeleteRoom(idOrName string) error {
|
||||
delete(p.roomIds, room.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *LocalRoomStore) GetParticipantId(room, name string) (string, error) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
roomParticipantIds := p.participantIds[room]
|
||||
if roomParticipantIds == nil {
|
||||
p.participantIds[room] = make(map[string]string)
|
||||
roomParticipantIds = p.participantIds[room]
|
||||
}
|
||||
|
||||
pId := roomParticipantIds[name]
|
||||
if pId == "" {
|
||||
pId = utils.NewGuid(utils.ParticipantPrefix)
|
||||
roomParticipantIds[name] = pId
|
||||
}
|
||||
|
||||
return pId, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/service"
|
||||
)
|
||||
|
||||
func TestLocalRoomStore_GetParticipantId(t *testing.T) {
|
||||
s := service.NewLocalRoomStore()
|
||||
id, _ := s.GetParticipantId("room1", "p1")
|
||||
assert.NotEmpty(t, id)
|
||||
|
||||
t.Run("diff room, same name returns a new ID", func(t *testing.T) {
|
||||
id2, _ := s.GetParticipantId("room2", "p1")
|
||||
assert.NotEmpty(t, id2)
|
||||
assert.NotEqual(t, id, id2)
|
||||
})
|
||||
|
||||
t.Run("same room returns identical id", func(t *testing.T) {
|
||||
id2, _ := s.GetParticipantId("room1", "p1")
|
||||
assert.Equal(t, id, id2)
|
||||
})
|
||||
|
||||
t.Run("same room with different name", func(t *testing.T) {
|
||||
id2, _ := s.GetParticipantId("room1", "p2")
|
||||
assert.NotEqual(t, id, id2)
|
||||
})
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
@@ -17,6 +18,12 @@ const (
|
||||
|
||||
// hash of room_id => room name
|
||||
RoomIdMap = "room_id_map"
|
||||
|
||||
// hash of participant_name => participant_id
|
||||
// a key for each room, with expiration
|
||||
RoomParticipantMapPrefix = "participant_map:room:"
|
||||
|
||||
participantMappingTTL = 24 * time.Hour
|
||||
)
|
||||
|
||||
type RedisRoomStore struct {
|
||||
@@ -105,7 +112,26 @@ func (p *RedisRoomStore) DeleteRoom(idOrName string) error {
|
||||
pp := p.rc.Pipeline()
|
||||
pp.HDel(p.ctx, RoomIdMap, room.Sid)
|
||||
pp.HDel(p.ctx, RoomsKey, room.Name)
|
||||
pp.HDel(p.ctx, RoomParticipantMapPrefix+room.Name)
|
||||
|
||||
_, err = pp.Exec(p.ctx)
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *RedisRoomStore) GetParticipantId(room, name string) (string, error) {
|
||||
key := RoomParticipantMapPrefix + room
|
||||
|
||||
pId, err := p.rc.HGet(p.ctx, key, name).Result()
|
||||
if err == redis.Nil {
|
||||
// create
|
||||
pId = utils.NewGuid(utils.ParticipantPrefix)
|
||||
pp := p.rc.Pipeline()
|
||||
pp.HSet(p.ctx, key, name, pId)
|
||||
pp.Expire(p.ctx, key, participantMappingTTL)
|
||||
_, err = pp.Exec(p.ctx)
|
||||
} else if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return pId, err
|
||||
}
|
||||
|
||||
@@ -7,10 +7,13 @@ import (
|
||||
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
|
||||
|
||||
// encapsulates CRUD operations for room settings
|
||||
// look up participant
|
||||
//counterfeiter:generate . RoomStore
|
||||
type RoomStore interface {
|
||||
CreateRoom(room *livekit.Room) error
|
||||
GetRoom(idOrName string) (*livekit.Room, error)
|
||||
ListRooms() ([]*livekit.Room, error)
|
||||
DeleteRoom(idOrName string) error
|
||||
// returns the current participant id in room, or create new one
|
||||
GetParticipantId(room, name string) (string, error)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
@@ -63,14 +62,17 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
rm, err := s.roomManager.roomStore.GetRoom(roomName)
|
||||
if err == ErrRoomNotFound {
|
||||
rm, err = s.roomManager.CreateRoom(&livekit.CreateRoomRequest{Name: roomName})
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
} else if err != nil {
|
||||
handleError(w, http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
participantId := utils.NewGuid(utils.ParticipantPrefix)
|
||||
participantId, err := s.roomManager.roomStore.GetParticipantId(roomName, pName)
|
||||
if err != nil {
|
||||
handleError(w, http.StatusInternalServerError, "could not get participant ID: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
err = s.router.StartParticipantSignal(roomName, participantId, pName)
|
||||
if err != nil {
|
||||
handleError(w, http.StatusInternalServerError, "could not start session: "+err.Error())
|
||||
|
||||
@@ -31,6 +31,20 @@ type FakeRoomStore struct {
|
||||
deleteRoomReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
GetParticipantIdStub func(string, string) (string, error)
|
||||
getParticipantIdMutex sync.RWMutex
|
||||
getParticipantIdArgsForCall []struct {
|
||||
arg1 string
|
||||
arg2 string
|
||||
}
|
||||
getParticipantIdReturns struct {
|
||||
result1 string
|
||||
result2 error
|
||||
}
|
||||
getParticipantIdReturnsOnCall map[int]struct {
|
||||
result1 string
|
||||
result2 error
|
||||
}
|
||||
GetRoomStub func(string) (*livekit.Room, error)
|
||||
getRoomMutex sync.RWMutex
|
||||
getRoomArgsForCall []struct {
|
||||
@@ -182,6 +196,71 @@ func (fake *FakeRoomStore) DeleteRoomReturnsOnCall(i int, result1 error) {
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeRoomStore) GetParticipantId(arg1 string, arg2 string) (string, error) {
|
||||
fake.getParticipantIdMutex.Lock()
|
||||
ret, specificReturn := fake.getParticipantIdReturnsOnCall[len(fake.getParticipantIdArgsForCall)]
|
||||
fake.getParticipantIdArgsForCall = append(fake.getParticipantIdArgsForCall, struct {
|
||||
arg1 string
|
||||
arg2 string
|
||||
}{arg1, arg2})
|
||||
stub := fake.GetParticipantIdStub
|
||||
fakeReturns := fake.getParticipantIdReturns
|
||||
fake.recordInvocation("GetParticipantId", []interface{}{arg1, arg2})
|
||||
fake.getParticipantIdMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub(arg1, arg2)
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1, ret.result2
|
||||
}
|
||||
return fakeReturns.result1, fakeReturns.result2
|
||||
}
|
||||
|
||||
func (fake *FakeRoomStore) GetParticipantIdCallCount() int {
|
||||
fake.getParticipantIdMutex.RLock()
|
||||
defer fake.getParticipantIdMutex.RUnlock()
|
||||
return len(fake.getParticipantIdArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeRoomStore) GetParticipantIdCalls(stub func(string, string) (string, error)) {
|
||||
fake.getParticipantIdMutex.Lock()
|
||||
defer fake.getParticipantIdMutex.Unlock()
|
||||
fake.GetParticipantIdStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeRoomStore) GetParticipantIdArgsForCall(i int) (string, string) {
|
||||
fake.getParticipantIdMutex.RLock()
|
||||
defer fake.getParticipantIdMutex.RUnlock()
|
||||
argsForCall := fake.getParticipantIdArgsForCall[i]
|
||||
return argsForCall.arg1, argsForCall.arg2
|
||||
}
|
||||
|
||||
func (fake *FakeRoomStore) GetParticipantIdReturns(result1 string, result2 error) {
|
||||
fake.getParticipantIdMutex.Lock()
|
||||
defer fake.getParticipantIdMutex.Unlock()
|
||||
fake.GetParticipantIdStub = nil
|
||||
fake.getParticipantIdReturns = struct {
|
||||
result1 string
|
||||
result2 error
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeRoomStore) GetParticipantIdReturnsOnCall(i int, result1 string, result2 error) {
|
||||
fake.getParticipantIdMutex.Lock()
|
||||
defer fake.getParticipantIdMutex.Unlock()
|
||||
fake.GetParticipantIdStub = nil
|
||||
if fake.getParticipantIdReturnsOnCall == nil {
|
||||
fake.getParticipantIdReturnsOnCall = make(map[int]struct {
|
||||
result1 string
|
||||
result2 error
|
||||
})
|
||||
}
|
||||
fake.getParticipantIdReturnsOnCall[i] = struct {
|
||||
result1 string
|
||||
result2 error
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeRoomStore) GetRoom(arg1 string) (*livekit.Room, error) {
|
||||
fake.getRoomMutex.Lock()
|
||||
ret, specificReturn := fake.getRoomReturnsOnCall[len(fake.getRoomArgsForCall)]
|
||||
@@ -309,6 +388,8 @@ func (fake *FakeRoomStore) Invocations() map[string][][]interface{} {
|
||||
defer fake.createRoomMutex.RUnlock()
|
||||
fake.deleteRoomMutex.RLock()
|
||||
defer fake.deleteRoomMutex.RUnlock()
|
||||
fake.getParticipantIdMutex.RLock()
|
||||
defer fake.getParticipantIdMutex.RUnlock()
|
||||
fake.getRoomMutex.RLock()
|
||||
defer fake.getRoomMutex.RUnlock()
|
||||
fake.listRoomsMutex.RLock()
|
||||
|
||||
+12
-1
@@ -1,8 +1,12 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"github.com/google/wire"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/wire"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
@@ -21,3 +25,10 @@ var ServiceSet = wire.NewSet(
|
||||
func externalIpFromNode(currentNode routing.LocalNode) rtc.ExternalIP {
|
||||
return rtc.ExternalIP(currentNode.Ip)
|
||||
}
|
||||
|
||||
func handleError(w http.ResponseWriter, status int, msg string) {
|
||||
l := logger.Desugar().WithOptions(zap.AddCallerSkip(1))
|
||||
l.Debug("error handling request", zap.String("error", msg), zap.Int("status", status))
|
||||
w.WriteHeader(status)
|
||||
w.Write([]byte(msg))
|
||||
}
|
||||
|
||||
@@ -8,3 +8,13 @@ import (
|
||||
func scenarioRandom(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
// websocket reconnects
|
||||
func scenarioWSReconnect(t *testing.T) {
|
||||
c1 := createRTCClient("wsr_c1", defaultServerPort)
|
||||
c2 := createRTCClient("wsr_c2", defaultServerPort)
|
||||
|
||||
waitUntilConnected(t, c1, c2)
|
||||
|
||||
// c1 publishes track, but disconnects websockets and reconnects
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user