Files
livekit/pkg/service/localstore.go
David Colburn 0b8a180554 Code inspection (#581)
* Code inspection

* fix [4]int64 conversiong
2022-03-30 13:49:53 -07:00

172 lines
4.5 KiB
Go

package service
import (
"context"
"sync"
"time"
"github.com/thoas/go-funk"
"github.com/livekit/protocol/livekit"
)
// encapsulates CRUD operations for room settings
type LocalStore struct {
// map of roomName => room
rooms map[livekit.RoomName]*livekit.Room
// map of roomName => { identity: participant }
participants map[livekit.RoomName]map[livekit.ParticipantIdentity]*livekit.ParticipantInfo
lock sync.RWMutex
globalLock sync.Mutex
}
func NewLocalStore() *LocalStore {
return &LocalStore{
rooms: make(map[livekit.RoomName]*livekit.Room),
participants: make(map[livekit.RoomName]map[livekit.ParticipantIdentity]*livekit.ParticipantInfo),
lock: sync.RWMutex{},
}
}
func (s *LocalStore) StoreRoom(_ context.Context, room *livekit.Room) error {
if room.CreationTime == 0 {
room.CreationTime = time.Now().Unix()
}
s.lock.Lock()
s.rooms[livekit.RoomName(room.Name)] = room
s.lock.Unlock()
return nil
}
func (s *LocalStore) LoadRoom(_ context.Context, name livekit.RoomName) (*livekit.Room, error) {
s.lock.RLock()
defer s.lock.RUnlock()
room := s.rooms[name]
if room == nil {
return nil, ErrRoomNotFound
}
return room, nil
}
func (s *LocalStore) ListRooms(_ context.Context, names []livekit.RoomName) ([]*livekit.Room, error) {
s.lock.RLock()
defer s.lock.RUnlock()
rooms := make([]*livekit.Room, 0, len(s.rooms))
for _, r := range s.rooms {
if names == nil || funk.Contains(names, livekit.RoomName(r.Name)) {
rooms = append(rooms, r)
}
}
return rooms, nil
}
func (s *LocalStore) DeleteRoom(ctx context.Context, name livekit.RoomName) error {
room, err := s.LoadRoom(ctx, name)
if err == ErrRoomNotFound {
return nil
} else if err != nil {
return err
}
s.lock.Lock()
defer s.lock.Unlock()
delete(s.participants, livekit.RoomName(room.Name))
delete(s.rooms, livekit.RoomName(room.Name))
return nil
}
func (s *LocalStore) LockRoom(_ context.Context, _ livekit.RoomName, _ time.Duration) (string, error) {
// local rooms lock & unlock globally
s.globalLock.Lock()
return "", nil
}
func (s *LocalStore) UnlockRoom(_ context.Context, _ livekit.RoomName, _ string) error {
s.globalLock.Unlock()
return nil
}
func (s *LocalStore) StoreParticipant(_ context.Context, roomName livekit.RoomName, participant *livekit.ParticipantInfo) error {
s.lock.Lock()
defer s.lock.Unlock()
roomParticipants := s.participants[roomName]
if roomParticipants == nil {
roomParticipants = make(map[livekit.ParticipantIdentity]*livekit.ParticipantInfo)
s.participants[roomName] = roomParticipants
}
roomParticipants[livekit.ParticipantIdentity(participant.Identity)] = participant
return nil
}
func (s *LocalStore) LoadParticipant(_ context.Context, roomName livekit.RoomName, identity livekit.ParticipantIdentity) (*livekit.ParticipantInfo, error) {
s.lock.RLock()
defer s.lock.RUnlock()
roomParticipants := s.participants[roomName]
if roomParticipants == nil {
return nil, ErrParticipantNotFound
}
participant := roomParticipants[identity]
if participant == nil {
return nil, ErrParticipantNotFound
}
return participant, nil
}
func (s *LocalStore) ListParticipants(_ context.Context, roomName livekit.RoomName) ([]*livekit.ParticipantInfo, error) {
s.lock.RLock()
defer s.lock.RUnlock()
roomParticipants := s.participants[roomName]
if roomParticipants == nil {
// empty array
return nil, nil
}
items := make([]*livekit.ParticipantInfo, 0, len(roomParticipants))
for _, p := range roomParticipants {
items = append(items, p)
}
return items, nil
}
func (s *LocalStore) DeleteParticipant(_ context.Context, roomName livekit.RoomName, identity livekit.ParticipantIdentity) error {
s.lock.Lock()
defer s.lock.Unlock()
roomParticipants := s.participants[roomName]
if roomParticipants != nil {
delete(roomParticipants, identity)
}
return nil
}
func (s *LocalStore) StoreEgress(_ context.Context, _ *livekit.EgressInfo) error {
// redis is required for egress
return nil
}
func (s *LocalStore) LoadEgress(_ context.Context, _ string) (*livekit.EgressInfo, error) {
// redis is required for egress
return nil, ErrEgressNotFound
}
func (s *LocalStore) ListEgress(_ context.Context, _ livekit.RoomID) ([]*livekit.EgressInfo, error) {
// redis is required for egress
return nil, nil
}
func (s *LocalStore) UpdateEgress(_ context.Context, _ *livekit.EgressInfo) error {
// redis is required for egress
return nil
}
func (s *LocalStore) DeleteEgress(_ context.Context, _ *livekit.EgressInfo) error {
// redis is required for egress
return nil
}