close rooms after delay instead of immediately after participant departure, to allow reconnects

This commit is contained in:
David Zhao
2021-01-31 14:19:16 -08:00
parent 084ec378f0
commit 76b0dc62a7
6 changed files with 149 additions and 39 deletions
+2
View File
@@ -12,6 +12,8 @@ import (
"github.com/livekit/livekit-server/proto/livekit"
)
//func Test
func TestIsReady(t *testing.T) {
tests := []struct {
state livekit.ParticipantInfo_State
+49 -9
View File
@@ -2,6 +2,7 @@ package rtc
import (
"sync"
"sync/atomic"
"time"
"github.com/thoas/go-funk"
@@ -12,24 +13,38 @@ import (
"github.com/livekit/livekit-server/proto/livekit"
)
const (
DefaultEmptyTimeout = 5 * 60 // 5 mins
)
type Room struct {
livekit.Room
config WebRTCConfig
lock sync.RWMutex
// map of identity -> Participant
participants map[string]types.Participant
hasJoined bool
isClosed utils.AtomicFlag
onClose func()
// time the first participant joined the room
joinedAt atomic.Value
// time that the last participant left the room
leftAt atomic.Value
isClosed utils.AtomicFlag
onClose func()
}
func NewRoom(room *livekit.Room, config WebRTCConfig) *Room {
return &Room{
r := &Room{
Room: *room,
config: config,
lock: sync.RWMutex{},
participants: make(map[string]types.Participant),
}
if r.EmptyTimeout == 0 {
r.EmptyTimeout = DefaultEmptyTimeout
}
if r.CreationTime == 0 {
r.CreationTime = time.Now().Unix()
}
return r
}
func (r *Room) GetParticipant(identity string) types.Participant {
@@ -44,6 +59,22 @@ func (r *Room) GetParticipants() []types.Participant {
return funk.Values(r.participants).([]types.Participant)
}
func (r *Room) FirstJoinedAt() int64 {
j := r.joinedAt.Load()
if t, ok := j.(int64); ok {
return t
}
return 0
}
func (r *Room) LastLeftAt() int64 {
l := r.leftAt.Load()
if t, ok := l.(int64); ok {
return t
}
return 0
}
func (r *Room) Join(participant types.Participant) error {
if r.isClosed.Get() {
return ErrRoomClosed
@@ -54,14 +85,15 @@ func (r *Room) Join(participant types.Participant) error {
if r.participants[participant.Identity()] != nil {
return ErrAlreadyJoined
}
if r.MaxParticipants > 0 && int(r.MaxParticipants) == len(r.participants) {
return ErrMaxParticipantsExceeded
}
r.hasJoined = true
if r.FirstJoinedAt() == 0 {
r.joinedAt.Store(time.Now().Unix())
}
// it's important to set this before connection, we don't want to miss out on any publishedTracks
participant.OnTrackPublished(r.onTrackAdded)
@@ -126,7 +158,9 @@ func (r *Room) RemoveParticipant(identity string) {
delete(r.participants, identity)
go r.CloseIfEmpty()
if len(r.participants) == 0 {
r.leftAt.Store(time.Now().Unix())
}
}
// Close the room if all participants had left, or it's still empty past timeout
@@ -143,8 +177,14 @@ func (r *Room) CloseIfEmpty() {
return
}
elapsed := uint32(time.Now().Unix() - r.CreationTime)
if r.hasJoined || (r.EmptyTimeout > 0 && elapsed >= r.EmptyTimeout) {
var elapsed int64
if r.FirstJoinedAt() > 0 {
// compute elasped from last departure
elapsed = time.Now().Unix() - r.LastLeftAt()
} else {
elapsed = time.Now().Unix() - r.CreationTime
}
if elapsed >= int64(r.EmptyTimeout) {
r.Close()
}
}
+42 -5
View File
@@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/pkg/logger"
"github.com/livekit/livekit-server/pkg/rtc"
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
"github.com/livekit/livekit-server/proto/livekit"
@@ -17,15 +18,49 @@ const (
defaultDelay = 10 * time.Millisecond
)
func init() {
logger.InitDevelopment("")
}
func TestJoinedState(t *testing.T) {
t.Run("new room should return joinedAt 0", func(t *testing.T) {
rm := newRoomWithParticipants(t, 0)
assert.Equal(t, int64(0), rm.FirstJoinedAt())
assert.Equal(t, int64(0), rm.LastLeftAt())
})
t.Run("should be current time when a participant joins", func(t *testing.T) {
s := time.Now().Unix()
rm := newRoomWithParticipants(t, 1)
assert.Equal(t, s, rm.FirstJoinedAt())
assert.Equal(t, int64(0), rm.LastLeftAt())
})
t.Run("should be set when a participant leaves", func(t *testing.T) {
rm := newRoomWithParticipants(t, 1)
p0 := rm.GetParticipants()[0]
s := time.Now().Unix()
rm.RemoveParticipant(p0.Identity())
assert.Equal(t, s, rm.LastLeftAt())
})
t.Run("LastLeftAt should not be set when there are still participants in the room", func(t *testing.T) {
rm := newRoomWithParticipants(t, 2)
p0 := rm.GetParticipants()[0]
rm.RemoveParticipant(p0.Identity())
assert.EqualValues(t, 0, rm.LastLeftAt())
})
}
func TestRoomJoin(t *testing.T) {
t.Run("joining returns existing participant data", func(t *testing.T) {
rm := newRoomWithParticipants(t, numParticipants)
p := newMockParticipant("new")
pNew := newMockParticipant("new")
rm.Join(p)
rm.Join(pNew)
// expect new participant to get a JoinReply
info, participants := p.SendJoinResponseArgsForCall(0)
info, participants := pNew.SendJoinResponseArgsForCall(0)
assert.Equal(t, info.Sid, rm.Sid)
assert.Len(t, participants, numParticipants)
assert.Len(t, rm.GetParticipants(), numParticipants+1)
@@ -98,10 +133,13 @@ func TestRoomClosure(t *testing.T) {
isClosed = true
})
p := rm.GetParticipants()[0]
// allows immediate close after
rm.EmptyTimeout = 0
rm.RemoveParticipant(p.Identity())
time.Sleep(defaultDelay)
rm.CloseIfEmpty()
assert.Len(t, rm.GetParticipants(), 0)
assert.True(t, isClosed)
@@ -114,7 +152,7 @@ func TestRoomClosure(t *testing.T) {
rm.OnClose(func() {
isClosed = true
})
assert.NotZero(t, rm.EmptyTimeout)
rm.CloseIfEmpty()
assert.False(t, isClosed)
})
@@ -156,7 +194,6 @@ func TestNewTrack(t *testing.T) {
}
func newRoomWithParticipants(t *testing.T, num int) *rtc.Room {
rm := rtc.NewRoom(
&livekit.Room{Name: "room"},
rtc.WebRTCConfig{},