RedisStore: make UnlockRoom atomic (#1044)
Co-authored-by: David Zhao <dz@livekit.io>
This commit is contained in:
+19
-14
@@ -48,15 +48,22 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type RedisStore struct {
|
type RedisStore struct {
|
||||||
rc redis.UniversalClient
|
rc redis.UniversalClient
|
||||||
ctx context.Context
|
unlockScript *redis.Script
|
||||||
done chan struct{}
|
ctx context.Context
|
||||||
|
done chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRedisStore(rc redis.UniversalClient) *RedisStore {
|
func NewRedisStore(rc redis.UniversalClient) *RedisStore {
|
||||||
|
unlockScript := `if redis.call("get", KEYS[1]) == ARGV[1] then
|
||||||
|
return redis.call("del", KEYS[1])
|
||||||
|
else return 0
|
||||||
|
end`
|
||||||
|
|
||||||
return &RedisStore{
|
return &RedisStore{
|
||||||
ctx: context.Background(),
|
ctx: context.Background(),
|
||||||
rc: rc,
|
rc: rc,
|
||||||
|
unlockScript: redis.NewScript(unlockScript),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,21 +251,19 @@ func (s *RedisStore) LockRoom(_ context.Context, roomName livekit.RoomName, dura
|
|||||||
return "", ErrRoomLockFailed
|
return "", ErrRoomLockFailed
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RedisStore) UnlockRoom(_ context.Context, roomName livekit.RoomName, uid string) error {
|
func (s *RedisStore) UnlockRoom(ctx context.Context, roomName livekit.RoomName, uid string) error {
|
||||||
key := RoomLockPrefix + string(roomName)
|
key := RoomLockPrefix + string(roomName)
|
||||||
|
res, err := s.unlockScript.Run(ctx, s.rc, []string{key}, uid).Result()
|
||||||
val, err := s.rc.Get(s.ctx, key).Result()
|
if err != nil {
|
||||||
if err == redis.Nil {
|
|
||||||
// already unlocked
|
|
||||||
return nil
|
|
||||||
} else if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if val != uid {
|
// uid does not match
|
||||||
|
if i, ok := res.(int64); !ok || i != 1 {
|
||||||
return ErrRoomUnlockFailed
|
return ErrRoomUnlockFailed
|
||||||
}
|
}
|
||||||
return s.rc.Del(s.ctx, key).Err()
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RedisStore) StoreParticipant(_ context.Context, roomName livekit.RoomName, participant *livekit.ParticipantInfo) error {
|
func (s *RedisStore) StoreParticipant(_ context.Context, roomName livekit.RoomName, participant *livekit.ParticipantInfo) error {
|
||||||
|
|||||||
Reference in New Issue
Block a user