Add support for Ingress in store interface (#849)
This commit is contained in:
@@ -20,11 +20,18 @@ const (
|
||||
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 {
|
||||
@@ -324,3 +331,181 @@ func (s *RedisStore) DeleteEgress(_ context.Context, info *livekit.EgressInfo) e
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user