idOrName -> name (#169)
This commit is contained in:
@@ -16,9 +16,9 @@ import (
|
|||||||
//counterfeiter:generate . RoomStore
|
//counterfeiter:generate . RoomStore
|
||||||
type RoomStore interface {
|
type RoomStore interface {
|
||||||
StoreRoom(ctx context.Context, room *livekit.Room) error
|
StoreRoom(ctx context.Context, room *livekit.Room) error
|
||||||
LoadRoom(ctx context.Context, idOrName string) (*livekit.Room, error)
|
LoadRoom(ctx context.Context, name string) (*livekit.Room, error)
|
||||||
ListRooms(ctx context.Context) ([]*livekit.Room, error)
|
ListRooms(ctx context.Context) ([]*livekit.Room, error)
|
||||||
DeleteRoom(ctx context.Context, idOrName string) error
|
DeleteRoom(ctx context.Context, name string) error
|
||||||
|
|
||||||
// enable locking on a specific room to prevent race
|
// enable locking on a specific room to prevent race
|
||||||
// returns a (lock uuid, error)
|
// returns a (lock uuid, error)
|
||||||
|
|||||||
@@ -10,10 +10,8 @@ import (
|
|||||||
|
|
||||||
// encapsulates CRUD operations for room settings
|
// encapsulates CRUD operations for room settings
|
||||||
type LocalRoomStore struct {
|
type LocalRoomStore struct {
|
||||||
// map of roomId => room
|
// map of roomName => room
|
||||||
rooms map[string]*livekit.Room
|
rooms map[string]*livekit.Room
|
||||||
// map of roomName => roomId
|
|
||||||
roomIds map[string]string
|
|
||||||
// map of roomName => { identity: participant }
|
// map of roomName => { identity: participant }
|
||||||
participants map[string]map[string]*livekit.ParticipantInfo
|
participants map[string]map[string]*livekit.ParticipantInfo
|
||||||
lock sync.RWMutex
|
lock sync.RWMutex
|
||||||
@@ -23,7 +21,6 @@ type LocalRoomStore struct {
|
|||||||
func NewLocalRoomStore() *LocalRoomStore {
|
func NewLocalRoomStore() *LocalRoomStore {
|
||||||
return &LocalRoomStore{
|
return &LocalRoomStore{
|
||||||
rooms: make(map[string]*livekit.Room),
|
rooms: make(map[string]*livekit.Room),
|
||||||
roomIds: make(map[string]string),
|
|
||||||
participants: make(map[string]map[string]*livekit.ParticipantInfo),
|
participants: make(map[string]map[string]*livekit.ParticipantInfo),
|
||||||
lock: sync.RWMutex{},
|
lock: sync.RWMutex{},
|
||||||
}
|
}
|
||||||
@@ -34,21 +31,16 @@ func (p *LocalRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) erro
|
|||||||
room.CreationTime = time.Now().Unix()
|
room.CreationTime = time.Now().Unix()
|
||||||
}
|
}
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
p.rooms[room.Sid] = room
|
p.rooms[room.Name] = room
|
||||||
p.roomIds[room.Name] = room.Sid
|
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *LocalRoomStore) LoadRoom(ctx context.Context, idOrName string) (*livekit.Room, error) {
|
func (p *LocalRoomStore) LoadRoom(ctx context.Context, name string) (*livekit.Room, error) {
|
||||||
p.lock.RLock()
|
p.lock.RLock()
|
||||||
defer p.lock.RUnlock()
|
defer p.lock.RUnlock()
|
||||||
// see if it's an id or name
|
|
||||||
if p.rooms[idOrName] == nil {
|
|
||||||
idOrName = p.roomIds[idOrName]
|
|
||||||
}
|
|
||||||
|
|
||||||
room := p.rooms[idOrName]
|
room := p.rooms[name]
|
||||||
if room == nil {
|
if room == nil {
|
||||||
return nil, ErrRoomNotFound
|
return nil, ErrRoomNotFound
|
||||||
}
|
}
|
||||||
@@ -65,8 +57,8 @@ func (p *LocalRoomStore) ListRooms(ctx context.Context) ([]*livekit.Room, error)
|
|||||||
return rooms, nil
|
return rooms, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *LocalRoomStore) DeleteRoom(ctx context.Context, idOrName string) error {
|
func (p *LocalRoomStore) DeleteRoom(ctx context.Context, name string) error {
|
||||||
room, err := p.LoadRoom(ctx, idOrName)
|
room, err := p.LoadRoom(ctx, name)
|
||||||
if err == ErrRoomNotFound {
|
if err == ErrRoomNotFound {
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
@@ -77,8 +69,7 @@ func (p *LocalRoomStore) DeleteRoom(ctx context.Context, idOrName string) error
|
|||||||
defer p.lock.Unlock()
|
defer p.lock.Unlock()
|
||||||
|
|
||||||
delete(p.participants, room.Name)
|
delete(p.participants, room.Name)
|
||||||
delete(p.roomIds, room.Name)
|
delete(p.rooms, room.Name)
|
||||||
delete(p.rooms, room.Sid)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,9 +15,6 @@ const (
|
|||||||
// RoomsKey is hash of room_name => Room proto
|
// RoomsKey is hash of room_name => Room proto
|
||||||
RoomsKey = "rooms"
|
RoomsKey = "rooms"
|
||||||
|
|
||||||
// RoomIdMap is hash of room_id => room name
|
|
||||||
RoomIdMap = "room_id_map"
|
|
||||||
|
|
||||||
// RoomParticipantsPrefix is hash of participant_name => ParticipantInfo
|
// RoomParticipantsPrefix is hash of participant_name => ParticipantInfo
|
||||||
// a key for each room, with expiration
|
// a key for each room, with expiration
|
||||||
RoomParticipantsPrefix = "room_participants:"
|
RoomParticipantsPrefix = "room_participants:"
|
||||||
@@ -49,7 +46,6 @@ func (p *RedisRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
pp := p.rc.Pipeline()
|
pp := p.rc.Pipeline()
|
||||||
pp.HSet(p.ctx, RoomIdMap, room.Sid, room.Name)
|
|
||||||
pp.HSet(p.ctx, RoomsKey, room.Name, data)
|
pp.HSet(p.ctx, RoomsKey, room.Name, data)
|
||||||
|
|
||||||
if _, err = pp.Exec(p.ctx); err != nil {
|
if _, err = pp.Exec(p.ctx); err != nil {
|
||||||
@@ -58,13 +54,7 @@ func (p *RedisRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) erro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *RedisRoomStore) LoadRoom(ctx context.Context, idOrName string) (*livekit.Room, error) {
|
func (p *RedisRoomStore) LoadRoom(ctx context.Context, name string) (*livekit.Room, error) {
|
||||||
// see if matches any ids
|
|
||||||
name, err := p.rc.HGet(p.ctx, RoomIdMap, idOrName).Result()
|
|
||||||
if err != nil {
|
|
||||||
name = idOrName
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := p.rc.HGet(p.ctx, RoomsKey, name).Result()
|
data, err := p.rc.HGet(p.ctx, RoomsKey, name).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == redis.Nil {
|
if err == redis.Nil {
|
||||||
@@ -101,23 +91,13 @@ func (p *RedisRoomStore) ListRooms(ctx context.Context) ([]*livekit.Room, error)
|
|||||||
return rooms, nil
|
return rooms, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *RedisRoomStore) DeleteRoom(ctx context.Context, idOrName string) error {
|
func (p *RedisRoomStore) DeleteRoom(ctx context.Context, name string) error {
|
||||||
room, err := p.LoadRoom(ctx, idOrName)
|
_, err := p.LoadRoom(ctx, name)
|
||||||
var sid, name string
|
|
||||||
|
|
||||||
if err == ErrRoomNotFound {
|
if err == ErrRoomNotFound {
|
||||||
// try to clean up as best as we could
|
return nil
|
||||||
sid = idOrName
|
|
||||||
name = idOrName
|
|
||||||
} else if err == nil {
|
|
||||||
sid = room.Sid
|
|
||||||
name = room.Name
|
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pp := p.rc.Pipeline()
|
pp := p.rc.Pipeline()
|
||||||
pp.HDel(p.ctx, RoomIdMap, sid)
|
|
||||||
pp.HDel(p.ctx, RoomsKey, name)
|
pp.HDel(p.ctx, RoomsKey, name)
|
||||||
pp.Del(p.ctx, RoomParticipantsPrefix+name)
|
pp.Del(p.ctx, RoomParticipantsPrefix+name)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user