512 lines
12 KiB
Go
512 lines
12 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/pkg/errors"
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"github.com/livekit/protocol/livekit"
|
|
"github.com/livekit/protocol/utils"
|
|
)
|
|
|
|
const (
|
|
// RoomsKey is hash of room_name => Room proto
|
|
RoomsKey = "rooms"
|
|
|
|
// EgressKey is a hash of egressID => egress info
|
|
EgressKey = "egress"
|
|
RoomEgressPrefix = "room_egress:"
|
|
|
|
// IngressKey is a hash of ingressID => ingress info
|
|
IngressKey = "ingress"
|
|
StreamKeyKey = "stream_key"
|
|
RoomIngressPrefix = "room_ingress:"
|
|
|
|
// RoomParticipantsPrefix is hash of participant_name => ParticipantInfo
|
|
RoomParticipantsPrefix = "room_participants:"
|
|
|
|
// RoomLockPrefix is a simple key containing a provided lock uid
|
|
RoomLockPrefix = "room_lock:"
|
|
|
|
maxRetries = 5
|
|
)
|
|
|
|
type RedisStore struct {
|
|
rc *redis.Client
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewRedisStore(rc *redis.Client) *RedisStore {
|
|
return &RedisStore{
|
|
ctx: context.Background(),
|
|
rc: rc,
|
|
}
|
|
}
|
|
|
|
func (s *RedisStore) StoreRoom(_ context.Context, room *livekit.Room) error {
|
|
if room.CreationTime == 0 {
|
|
room.CreationTime = time.Now().Unix()
|
|
}
|
|
|
|
data, err := proto.Marshal(room)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pp := s.rc.Pipeline()
|
|
pp.HSet(s.ctx, RoomsKey, room.Name, data)
|
|
|
|
if _, err = pp.Exec(s.ctx); err != nil {
|
|
return errors.Wrap(err, "could not create room")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *RedisStore) LoadRoom(_ context.Context, name livekit.RoomName) (*livekit.Room, error) {
|
|
data, err := s.rc.HGet(s.ctx, RoomsKey, string(name)).Result()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
err = ErrRoomNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
room := livekit.Room{}
|
|
err = proto.Unmarshal([]byte(data), &room)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &room, nil
|
|
}
|
|
|
|
func (s *RedisStore) ListRooms(_ context.Context, names []livekit.RoomName) ([]*livekit.Room, error) {
|
|
var items []string
|
|
var err error
|
|
if names == nil {
|
|
items, err = s.rc.HVals(s.ctx, RoomsKey).Result()
|
|
if err != nil && err != redis.Nil {
|
|
return nil, errors.Wrap(err, "could not get rooms")
|
|
}
|
|
} else {
|
|
roomNames := livekit.RoomNamesAsStrings(names)
|
|
var results []interface{}
|
|
results, err = s.rc.HMGet(s.ctx, RoomsKey, roomNames...).Result()
|
|
if err != nil && err != redis.Nil {
|
|
return nil, errors.Wrap(err, "could not get rooms by names")
|
|
}
|
|
for _, r := range results {
|
|
if item, ok := r.(string); ok {
|
|
items = append(items, item)
|
|
}
|
|
}
|
|
}
|
|
|
|
rooms := make([]*livekit.Room, 0, len(items))
|
|
|
|
for _, item := range items {
|
|
room := livekit.Room{}
|
|
err := proto.Unmarshal([]byte(item), &room)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
rooms = append(rooms, &room)
|
|
}
|
|
return rooms, nil
|
|
}
|
|
|
|
func (s *RedisStore) DeleteRoom(ctx context.Context, name livekit.RoomName) error {
|
|
_, err := s.LoadRoom(ctx, name)
|
|
if err == ErrRoomNotFound {
|
|
return nil
|
|
}
|
|
|
|
pp := s.rc.Pipeline()
|
|
pp.HDel(s.ctx, RoomsKey, string(name))
|
|
pp.Del(s.ctx, RoomParticipantsPrefix+string(name))
|
|
|
|
_, err = pp.Exec(s.ctx)
|
|
return err
|
|
}
|
|
|
|
func (s *RedisStore) LockRoom(_ context.Context, name livekit.RoomName, duration time.Duration) (string, error) {
|
|
token := utils.NewGuid("LOCK")
|
|
key := RoomLockPrefix + string(name)
|
|
|
|
startTime := time.Now()
|
|
for {
|
|
locked, err := s.rc.SetNX(s.ctx, key, token, duration).Result()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if locked {
|
|
return token, nil
|
|
}
|
|
|
|
// stop waiting past lock duration
|
|
if time.Since(startTime) > duration {
|
|
break
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
|
|
return "", ErrRoomLockFailed
|
|
}
|
|
|
|
func (s *RedisStore) UnlockRoom(_ context.Context, name livekit.RoomName, uid string) error {
|
|
key := RoomLockPrefix + string(name)
|
|
|
|
val, err := s.rc.Get(s.ctx, key).Result()
|
|
if err == redis.Nil {
|
|
// already unlocked
|
|
return nil
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
|
|
if val != uid {
|
|
return ErrRoomUnlockFailed
|
|
}
|
|
return s.rc.Del(s.ctx, key).Err()
|
|
}
|
|
|
|
func (s *RedisStore) StoreParticipant(_ context.Context, roomName livekit.RoomName, participant *livekit.ParticipantInfo) error {
|
|
key := RoomParticipantsPrefix + string(roomName)
|
|
|
|
data, err := proto.Marshal(participant)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.rc.HSet(s.ctx, key, participant.Identity, data).Err()
|
|
}
|
|
|
|
func (s *RedisStore) LoadParticipant(_ context.Context, roomName livekit.RoomName, identity livekit.ParticipantIdentity) (*livekit.ParticipantInfo, error) {
|
|
key := RoomParticipantsPrefix + string(roomName)
|
|
data, err := s.rc.HGet(s.ctx, key, string(identity)).Result()
|
|
if err == redis.Nil {
|
|
return nil, ErrParticipantNotFound
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pi := livekit.ParticipantInfo{}
|
|
if err := proto.Unmarshal([]byte(data), &pi); err != nil {
|
|
return nil, err
|
|
}
|
|
return &pi, nil
|
|
}
|
|
|
|
func (s *RedisStore) ListParticipants(_ context.Context, roomName livekit.RoomName) ([]*livekit.ParticipantInfo, error) {
|
|
key := RoomParticipantsPrefix + string(roomName)
|
|
items, err := s.rc.HVals(s.ctx, key).Result()
|
|
if err == redis.Nil {
|
|
return nil, nil
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
participants := make([]*livekit.ParticipantInfo, 0, len(items))
|
|
for _, item := range items {
|
|
pi := livekit.ParticipantInfo{}
|
|
if err := proto.Unmarshal([]byte(item), &pi); err != nil {
|
|
return nil, err
|
|
}
|
|
participants = append(participants, &pi)
|
|
}
|
|
return participants, nil
|
|
}
|
|
|
|
func (s *RedisStore) DeleteParticipant(_ context.Context, roomName livekit.RoomName, identity livekit.ParticipantIdentity) error {
|
|
key := RoomParticipantsPrefix + string(roomName)
|
|
|
|
return s.rc.HDel(s.ctx, key, string(identity)).Err()
|
|
}
|
|
|
|
func (s *RedisStore) StoreEgress(_ context.Context, info *livekit.EgressInfo) error {
|
|
data, err := proto.Marshal(info)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pp := s.rc.Pipeline()
|
|
pp.HSet(s.ctx, EgressKey, info.EgressId, data)
|
|
pp.SAdd(s.ctx, RoomEgressPrefix+info.RoomId, info.EgressId)
|
|
|
|
if _, err = pp.Exec(s.ctx); err != nil {
|
|
return errors.Wrap(err, "could not store egress info")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *RedisStore) LoadEgress(_ context.Context, egressID string) (*livekit.EgressInfo, error) {
|
|
data, err := s.rc.HGet(s.ctx, EgressKey, egressID).Result()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
return nil, ErrEgressNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
info := &livekit.EgressInfo{}
|
|
err = proto.Unmarshal([]byte(data), info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return info, nil
|
|
}
|
|
|
|
func (s *RedisStore) ListEgress(_ context.Context, roomID livekit.RoomID) ([]*livekit.EgressInfo, error) {
|
|
var infos []*livekit.EgressInfo
|
|
|
|
if roomID == "" {
|
|
data, err := s.rc.HGetAll(s.ctx, EgressKey).Result()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
for _, d := range data {
|
|
info := &livekit.EgressInfo{}
|
|
err = proto.Unmarshal([]byte(d), info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
infos = append(infos, info)
|
|
}
|
|
} else {
|
|
ids, err := s.rc.SMembers(s.ctx, RoomEgressPrefix+string(roomID)).Result()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
data, _ := s.rc.HMGet(s.ctx, EgressKey, ids...).Result()
|
|
for _, d := range data {
|
|
if d == nil {
|
|
continue
|
|
}
|
|
info := &livekit.EgressInfo{}
|
|
err = proto.Unmarshal([]byte(d.(string)), info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
infos = append(infos, info)
|
|
}
|
|
}
|
|
|
|
return infos, nil
|
|
}
|
|
|
|
func (s *RedisStore) UpdateEgress(_ context.Context, info *livekit.EgressInfo) error {
|
|
data, err := proto.Marshal(info)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pp := s.rc.Pipeline()
|
|
pp.HSet(s.ctx, EgressKey, info.EgressId, data)
|
|
|
|
if _, err = pp.Exec(s.ctx); err != nil {
|
|
return errors.Wrap(err, "could not store egress info")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *RedisStore) DeleteEgress(_ context.Context, info *livekit.EgressInfo) error {
|
|
err := s.rc.SRem(s.ctx, RoomEgressPrefix+info.RoomId, info.EgressId).Err()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return s.rc.HDel(s.ctx, EgressKey, info.EgressId).Err()
|
|
}
|
|
|
|
func (s *RedisStore) StoreIngress(_ context.Context, info *livekit.IngressInfo) error {
|
|
if info.IngressId == "" {
|
|
return errors.New("Missing IngressId")
|
|
}
|
|
if info.StreamKey == "" {
|
|
return errors.New("Missing StreamKey")
|
|
}
|
|
|
|
data, err := proto.Marshal(info)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Use a "transaction" to remove the old room association if it changed
|
|
txf := func(tx *redis.Tx) error {
|
|
var oldRoom string
|
|
|
|
oldInfo, err := s.loadIngress(tx, info.IngressId)
|
|
switch err {
|
|
case ErrIngressNotFound:
|
|
// Ingress doesn't exist yet
|
|
case nil:
|
|
oldRoom = oldInfo.RoomName
|
|
default:
|
|
return err
|
|
}
|
|
|
|
results, err := tx.TxPipelined(s.ctx, func(pipe redis.Pipeliner) error {
|
|
pipe.HSet(s.ctx, IngressKey, info.IngressId, data)
|
|
pipe.HSet(s.ctx, StreamKeyKey, info.IngressId, info.StreamKey)
|
|
|
|
if oldRoom != info.RoomName {
|
|
if oldRoom != "" {
|
|
pipe.SRem(s.ctx, RoomIngressPrefix+oldRoom, info.IngressId)
|
|
}
|
|
if info.RoomName != "" {
|
|
pipe.SAdd(s.ctx, RoomIngressPrefix+info.RoomName, info.IngressId)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, res := range results {
|
|
if err := res.Err(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Retry if the key has been changed.
|
|
for i := 0; i < maxRetries; i++ {
|
|
err := s.rc.Watch(s.ctx, txf, IngressKey, StreamKeyKey)
|
|
if err == nil {
|
|
// Success.
|
|
return nil
|
|
}
|
|
if err == redis.TxFailedErr {
|
|
// Optimistic lock lost. Retry.
|
|
continue
|
|
}
|
|
// Return any other error.
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *RedisStore) loadIngress(c redis.Cmdable, ingressId string) (*livekit.IngressInfo, error) {
|
|
data, err := c.HGet(s.ctx, IngressKey, ingressId).Result()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
return nil, ErrIngressNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
info := &livekit.IngressInfo{}
|
|
err = proto.Unmarshal([]byte(data), info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return info, nil
|
|
}
|
|
|
|
func (s *RedisStore) LoadIngress(_ context.Context, ingressId string) (*livekit.IngressInfo, error) {
|
|
return s.loadIngress(s.rc, ingressId)
|
|
}
|
|
|
|
func (s *RedisStore) LoadIngressFromStreamKey(_ context.Context, streamKey string) (*livekit.IngressInfo, error) {
|
|
ingressId, err := s.rc.HGet(s.ctx, StreamKeyKey, streamKey).Result()
|
|
switch err {
|
|
case nil:
|
|
case redis.Nil:
|
|
return nil, ErrIngressNotFound
|
|
default:
|
|
return nil, err
|
|
}
|
|
|
|
return s.loadIngress(s.rc, ingressId)
|
|
}
|
|
|
|
func (s *RedisStore) ListIngress(_ context.Context, roomName livekit.RoomName) ([]*livekit.IngressInfo, error) {
|
|
var infos []*livekit.IngressInfo
|
|
|
|
if roomName == "" {
|
|
data, err := s.rc.HGetAll(s.ctx, IngressKey).Result()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
for _, d := range data {
|
|
info := &livekit.IngressInfo{}
|
|
err = proto.Unmarshal([]byte(d), info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
infos = append(infos, info)
|
|
}
|
|
} else {
|
|
ids, err := s.rc.SMembers(s.ctx, RoomIngressPrefix+string(roomName)).Result()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
data, _ := s.rc.HMGet(s.ctx, IngressKey, ids...).Result()
|
|
for _, d := range data {
|
|
if d == nil {
|
|
continue
|
|
}
|
|
info := &livekit.IngressInfo{}
|
|
err = proto.Unmarshal([]byte(d.(string)), info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
infos = append(infos, info)
|
|
}
|
|
}
|
|
|
|
return infos, nil
|
|
}
|
|
|
|
func (s *RedisStore) UpdateIngress(ctx context.Context, info *livekit.IngressInfo) error {
|
|
return s.StoreIngress(ctx, info)
|
|
}
|
|
|
|
func (s *RedisStore) DeleteIngress(ctx context.Context, info *livekit.IngressInfo) error {
|
|
err := s.rc.SRem(s.ctx, RoomIngressPrefix+info.RoomName, info.IngressId).Err()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.rc.HDel(s.ctx, StreamKeyKey, info.IngressId).Err()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.rc.HDel(s.ctx, EgressKey, info.IngressId).Err()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|