Ability to disable auto-create (#361)

* setting to disable autocreate

* improve test reliability

* update comment

* update to address feedback
This commit is contained in:
David Zhao
2022-01-21 09:57:36 -08:00
committed by GitHub
parent 7f4ead9b25
commit 126bb8867b
13 changed files with 178 additions and 22 deletions
+3
View File
@@ -103,6 +103,8 @@ type RedisConfig struct {
}
type RoomConfig struct {
// enable rooms to be automatically created
AutoCreate bool `yaml:"auto_create"`
EnabledCodecs []CodecSpec `yaml:"enabled_codecs"`
MaxParticipants uint32 `yaml:"max_participants"`
EmptyTimeout uint32 `yaml:"empty_timeout"`
@@ -187,6 +189,7 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) {
},
Redis: RedisConfig{},
Room: RoomConfig{
AutoCreate: true,
// by default only enable opus and VP8
EnabledCodecs: []CodecSpec{
{Mime: webrtc.MimeTypeOpus},
+9
View File
@@ -13,3 +13,12 @@ func TestConfig_UnmarshalKeys(t *testing.T) {
require.NoError(t, conf.unmarshalKeys("key1: secret1"))
require.Equal(t, "secret1", conf.Keys["key1"])
}
func TestConfig_DefaultsKept(t *testing.T) {
const content = `room:
empty_timeout: 10`
conf, err := NewConfig(content, nil)
require.NoError(t, err)
require.Equal(t, true, conf.Room.AutoCreate)
require.Equal(t, uint32(10), conf.Room.EmptyTimeout)
}
+1
View File
@@ -9,4 +9,5 @@ var (
ErrParticipantNotFound = errors.New("participant does not exist")
ErrTrackNotFound = errors.New("track is not found")
ErrWebHookMissingAPIKey = errors.New("api_key is required to use webhooks")
ErrOperationFailed = errors.New("operation cannot be completed")
)
+38
View File
@@ -2,6 +2,7 @@ package service
import (
"context"
"time"
"github.com/livekit/protocol/livekit"
"github.com/pkg/errors"
@@ -11,6 +12,11 @@ import (
"github.com/livekit/livekit-server/pkg/routing"
)
const (
executionTimeout = 2 * time.Second
checkInterval = 50 * time.Millisecond
)
// A rooms service that supports a single node
type RoomService struct {
router routing.MessageRouter
@@ -75,6 +81,21 @@ func (s *RoomService) DeleteRoom(ctx context.Context, req *livekit.DeleteRoomReq
return nil, err
}
// we should not return until when the room is confirmed deleted
err = confirmExecution(func() error {
_, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Room))
if err == nil {
return ErrOperationFailed
} else if err != ErrRoomNotFound {
return err
} else {
return nil
}
})
if err != nil {
return nil, err
}
return &livekit.DeleteRoomResponse{}, nil
}
@@ -252,3 +273,20 @@ func (s *RoomService) writeRoomMessage(ctx context.Context, room livekit.RoomNam
return s.router.WriteRoomRTC(ctx, room, identity, msg)
}
func confirmExecution(f func() error) error {
expired := time.After(executionTimeout)
var err error
for {
select {
case <-expired:
return err
default:
err = f()
if err == nil {
return nil
}
time.Sleep(checkInterval)
}
}
}
+1
View File
@@ -24,6 +24,7 @@ func TestDeleteRoom(t *testing.T) {
},
}
ctx := context.WithValue(context.Background(), grantsKey, grant)
svc.store.LoadRoomReturns(nil, service.ErrRoomNotFound)
_, err := svc.DeleteRoom(ctx, &livekit.DeleteRoomRequest{
Room: "testroom",
})
+22 -1
View File
@@ -1,6 +1,7 @@
package service
import (
"context"
"fmt"
"io"
"net/http"
@@ -23,18 +24,28 @@ import (
type RTCService struct {
router routing.MessageRouter
roomAllocator RoomAllocator
store RoomStore
upgrader websocket.Upgrader
currentNode routing.LocalNode
config *config.Config
isDev bool
limits config.LimitConfig
}
func NewRTCService(conf *config.Config, ra RoomAllocator, router routing.MessageRouter, currentNode routing.LocalNode) *RTCService {
func NewRTCService(
conf *config.Config,
ra RoomAllocator,
store RoomStore,
router routing.MessageRouter,
currentNode routing.LocalNode,
) *RTCService {
s := &RTCService{
router: router,
roomAllocator: ra,
store: store,
upgrader: websocket.Upgrader{},
currentNode: currentNode,
config: conf,
isDev: conf.Development,
limits: conf.Limit,
}
@@ -130,6 +141,16 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
// when autocreate is disabled, we'll check to ensure it's already created
if !s.config.Room.AutoCreate {
_, err := s.store.LoadRoom(context.Background(), roomName)
if err == ErrRoomNotFound {
handleError(w, 404, err.Error())
} else if err != nil {
handleError(w, 500, err.Error())
}
}
// create room if it doesn't exist, also assigns an RTC node for the room
rm, err := s.roomAllocator.CreateRoom(r.Context(), &livekit.CreateRoomRequest{Name: string(roomName)})
if err != nil {
+1 -1
View File
@@ -50,7 +50,7 @@ func InitializeServer(conf *config.Config, currentNode routing.LocalNode) (*Live
analyticsService := telemetry.NewAnalyticsService(conf, currentNode)
telemetryService := telemetry.NewTelemetryService(notifier, analyticsService)
recordingService := NewRecordingService(messageBus, telemetryService)
rtcService := NewRTCService(conf, roomAllocator, router, currentNode)
rtcService := NewRTCService(conf, roomAllocator, roomStore, router, currentNode)
roomManager, err := NewLocalRoomManager(conf, roomStore, currentNode, router, telemetryService)
if err != nil {
return nil, err