Ensure RoomService operation is complete prior to returning (#362)

* Ensure service doesn't return before operation is complete

* added integration tests for RoomService
This commit is contained in:
David Zhao
2022-01-21 11:00:33 -08:00
committed by GitHub
parent 126bb8867b
commit f9a1dd97c2
7 changed files with 261 additions and 21 deletions
+3
View File
@@ -65,6 +65,9 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
if req.MaxParticipants > 0 {
rm.MaxParticipants = req.MaxParticipants
}
if req.Metadata != "" {
rm.Metadata = req.Metadata
}
if err := r.roomStore.StoreRoom(ctx, rm); err != nil {
return nil, err
}
+5 -1
View File
@@ -397,10 +397,14 @@ func (r *RoomManager) handleRTCMessage(_ context.Context, roomName livekit.RoomN
}
participant := room.GetParticipant(identity)
var sid livekit.ParticipantID
if participant != nil {
sid = participant.ID()
}
pLogger := rtc.LoggerWithParticipant(
rtc.LoggerWithRoom(logger.Logger(logger.GetLogger()), roomName, room.ID()),
identity,
"",
sid,
)
switch rm := msg.Message.(type) {
+75 -8
View File
@@ -139,6 +139,20 @@ func (s *RoomService) RemoveParticipant(ctx context.Context, req *livekit.RoomPa
return
}
err = confirmExecution(func() error {
_, err := s.roomStore.LoadParticipant(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity))
if err == ErrParticipantNotFound {
return nil
} else if err != nil {
return err
} else {
return ErrOperationFailed
}
})
if err != nil {
return nil, err
}
res = &livekit.RemoveParticipantResponse{}
return
}
@@ -153,10 +167,10 @@ func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteR
return nil, err
}
// find the track
track := funk.Find(participant.Tracks, func(t *livekit.TrackInfo) bool {
t := funk.Find(participant.Tracks, func(t *livekit.TrackInfo) bool {
return t.Sid == req.TrackSid
})
if track == nil {
if t == nil {
return nil, twirp.NotFoundError(ErrTrackNotFound.Error())
}
@@ -169,11 +183,33 @@ func (s *RoomService) MutePublishedTrack(ctx context.Context, req *livekit.MuteR
return
}
var track *livekit.TrackInfo
err = confirmExecution(func() error {
p, err := s.roomStore.LoadParticipant(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity))
if err != nil {
return err
}
// ensure track is muted
t := funk.Find(p.Tracks, func(t *livekit.TrackInfo) bool {
return t.Sid == req.TrackSid
})
var ok bool
track, ok = t.(*livekit.TrackInfo)
if !ok {
return ErrTrackNotFound
}
if track.Muted != req.Muted {
return ErrOperationFailed
}
return nil
})
if err != nil {
return nil, err
}
res = &livekit.MuteRoomTrackResponse{
Track: track.(*livekit.TrackInfo),
Track: track,
}
// mute might not have happened, reflect desired state
res.Track.Muted = req.Muted
return
}
@@ -187,12 +223,21 @@ func (s *RoomService) UpdateParticipant(ctx context.Context, req *livekit.Update
return nil, err
}
participant, err := s.roomStore.LoadParticipant(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity))
var participant *livekit.ParticipantInfo
err = confirmExecution(func() error {
participant, err = s.roomStore.LoadParticipant(ctx, livekit.RoomName(req.Room), livekit.ParticipantIdentity(req.Identity))
if err != nil {
return err
}
if participant.Metadata != req.Metadata {
return ErrOperationFailed
}
return nil
})
if err != nil {
return nil, err
}
participant.Metadata = req.Metadata
return participant, nil
}
@@ -232,7 +277,15 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
return nil, err
}
room.Metadata = req.Metadata
// no one has joined the room, would not have been created on an RTC node.
// in this case, we'd want to run create again
_, err = s.roomAllocator.CreateRoom(ctx, &livekit.CreateRoomRequest{
Name: req.Room,
Metadata: req.Metadata,
})
if err != nil {
return nil, err
}
err = s.writeRoomMessage(ctx, livekit.RoomName(req.Room), "", &livekit.RTCNodeMessage{
Message: &livekit.RTCNodeMessage_UpdateRoomMetadata{
@@ -243,6 +296,20 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
return nil, err
}
err = confirmExecution(func() error {
room, err = s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Room))
if err != nil {
return err
}
if room.Metadata != req.Metadata {
return ErrOperationFailed
}
return nil
})
if err != nil {
return nil, err
}
return room, nil
}