From 862a212b931a2f6f88966a7cbe3c47c081436c34 Mon Sep 17 00:00:00 2001 From: David Colburn Date: Wed, 3 Nov 2021 13:11:44 -0700 Subject: [PATCH] idOrName -> name (#169) --- pkg/service/interfaces.go | 4 ++-- pkg/service/localroomstore.go | 23 +++++++---------------- pkg/service/redisroomstore.go | 28 ++++------------------------ 3 files changed, 13 insertions(+), 42 deletions(-) diff --git a/pkg/service/interfaces.go b/pkg/service/interfaces.go index cb4d791e..06e873a0 100644 --- a/pkg/service/interfaces.go +++ b/pkg/service/interfaces.go @@ -16,9 +16,9 @@ import ( //counterfeiter:generate . RoomStore type RoomStore interface { 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) - DeleteRoom(ctx context.Context, idOrName string) error + DeleteRoom(ctx context.Context, name string) error // enable locking on a specific room to prevent race // returns a (lock uuid, error) diff --git a/pkg/service/localroomstore.go b/pkg/service/localroomstore.go index c0ee06d0..9bdf0a04 100644 --- a/pkg/service/localroomstore.go +++ b/pkg/service/localroomstore.go @@ -10,10 +10,8 @@ import ( // encapsulates CRUD operations for room settings type LocalRoomStore struct { - // map of roomId => room + // map of roomName => room rooms map[string]*livekit.Room - // map of roomName => roomId - roomIds map[string]string // map of roomName => { identity: participant } participants map[string]map[string]*livekit.ParticipantInfo lock sync.RWMutex @@ -23,7 +21,6 @@ type LocalRoomStore struct { func NewLocalRoomStore() *LocalRoomStore { return &LocalRoomStore{ rooms: make(map[string]*livekit.Room), - roomIds: make(map[string]string), participants: make(map[string]map[string]*livekit.ParticipantInfo), lock: sync.RWMutex{}, } @@ -34,21 +31,16 @@ func (p *LocalRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) erro room.CreationTime = time.Now().Unix() } p.lock.Lock() - p.rooms[room.Sid] = room - p.roomIds[room.Name] = room.Sid + p.rooms[room.Name] = room p.lock.Unlock() 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() 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 { return nil, ErrRoomNotFound } @@ -65,8 +57,8 @@ func (p *LocalRoomStore) ListRooms(ctx context.Context) ([]*livekit.Room, error) return rooms, nil } -func (p *LocalRoomStore) DeleteRoom(ctx context.Context, idOrName string) error { - room, err := p.LoadRoom(ctx, idOrName) +func (p *LocalRoomStore) DeleteRoom(ctx context.Context, name string) error { + room, err := p.LoadRoom(ctx, name) if err == ErrRoomNotFound { return nil } else if err != nil { @@ -77,8 +69,7 @@ func (p *LocalRoomStore) DeleteRoom(ctx context.Context, idOrName string) error defer p.lock.Unlock() delete(p.participants, room.Name) - delete(p.roomIds, room.Name) - delete(p.rooms, room.Sid) + delete(p.rooms, room.Name) return nil } diff --git a/pkg/service/redisroomstore.go b/pkg/service/redisroomstore.go index 2794a899..64db0e18 100644 --- a/pkg/service/redisroomstore.go +++ b/pkg/service/redisroomstore.go @@ -15,9 +15,6 @@ const ( // RoomsKey is hash of room_name => Room proto RoomsKey = "rooms" - // RoomIdMap is hash of room_id => room name - RoomIdMap = "room_id_map" - // RoomParticipantsPrefix is hash of participant_name => ParticipantInfo // a key for each room, with expiration RoomParticipantsPrefix = "room_participants:" @@ -49,7 +46,6 @@ func (p *RedisRoomStore) StoreRoom(ctx context.Context, room *livekit.Room) erro } pp := p.rc.Pipeline() - pp.HSet(p.ctx, RoomIdMap, room.Sid, room.Name) pp.HSet(p.ctx, RoomsKey, room.Name, data) 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 } -func (p *RedisRoomStore) LoadRoom(ctx context.Context, idOrName string) (*livekit.Room, error) { - // see if matches any ids - name, err := p.rc.HGet(p.ctx, RoomIdMap, idOrName).Result() - if err != nil { - name = idOrName - } - +func (p *RedisRoomStore) LoadRoom(ctx context.Context, name string) (*livekit.Room, error) { data, err := p.rc.HGet(p.ctx, RoomsKey, name).Result() if err != nil { if err == redis.Nil { @@ -101,23 +91,13 @@ func (p *RedisRoomStore) ListRooms(ctx context.Context) ([]*livekit.Room, error) return rooms, nil } -func (p *RedisRoomStore) DeleteRoom(ctx context.Context, idOrName string) error { - room, err := p.LoadRoom(ctx, idOrName) - var sid, name string - +func (p *RedisRoomStore) DeleteRoom(ctx context.Context, name string) error { + _, err := p.LoadRoom(ctx, name) if err == ErrRoomNotFound { - // try to clean up as best as we could - sid = idOrName - name = idOrName - } else if err == nil { - sid = room.Sid - name = room.Name - } else { - return err + return nil } pp := p.rc.Pipeline() - pp.HDel(p.ctx, RoomIdMap, sid) pp.HDel(p.ctx, RoomsKey, name) pp.Del(p.ctx, RoomParticipantsPrefix+name)