示例#1
0
        public async Task StoreActivation(
            string memberId,
            SpawnLock spawnLock,
            PID pid,
            CancellationToken ct
        )
        {
            var key = IdKey(spawnLock.ClusterIdentity);

            var values = new[]
            {
                new HashEntry(UniqueIdentity, pid.Id),
                new HashEntry(Address, pid.Address),
                new HashEntry(MemberId, memberId),
                new HashEntry(LockId, RedisValue.EmptyString)
            };

            var executed = await _asyncSemaphore.WaitAsync(() => {
                    var db = GetDb();

                    var transaction = db.CreateTransaction();
                    transaction.AddCondition(Condition.HashEqual(key, LockId, spawnLock.LockId));
                    _ = transaction.HashSetAsync(key, values, CommandFlags.DemandMaster);
                    _ = transaction.SetAddAsync(MemberKey(memberId), key.ToString());
                    _ = transaction.KeyPersistAsync(key);
                    return transaction.ExecuteAsync();
                }
            ).ConfigureAwait(false);

            if (!executed) throw new LockNotFoundException($"Failed to store activation of {pid}");
        }
示例#2
0
        public Task RemoveLock(SpawnLock spawnLock, CancellationToken ct)
        {
            var db = GetDb();

            var key = IdKey(spawnLock.ClusterIdentity);
            var transaction = db.CreateTransaction();
            transaction.AddCondition(Condition.HashEqual(key, LockId, spawnLock.LockId));
            _ = transaction.HashDeleteAsync(key, LockId);
            return transaction.ExecuteAsync();
        }
示例#3
0
        public void LockCharacterToStart()
        {
            GameObject respawn  = SpawnSingleton.instance;
            Vector3    position = respawn.transform.position;

            Debug.Log("Text: " + position);
            position.y += 2.56f;
            float diameter = respawn.GetComponent <SpriteRenderer>().bounds.size.x;

            spawnLock = Instantiate(spawnLockPrefab, position, Quaternion.identity);
            spawnLock.SetDiameter(diameter);
        }
        public Task RemoveLock(SpawnLock spawnLock, CancellationToken ct)
        => _asyncSemaphore.WaitAsync(() =>
        {
            Logger.LogInformation("[MongoIdentityStorage] DeleteManyAsync");
            if (ct.IsCancellationRequested)
            {
                Logger.LogInformation("[MongoIdentityStorage] DeleteManyAsync, resetting cancellation token.");
                ct = CancellationTokens.WithTimeout(TimeSpan.FromSeconds(5));
            }

            Logger.LogInformation("[MongoIdentityStorage] DeleteManyAsync, LockId {LockId}.", spawnLock.LockId);
            return(_pids.DeleteManyAsync(p => p.LockedBy == spawnLock.LockId, ct));
        });
示例#5
0
        public void CannotStoreWithoutLock()
        {
            var timeout   = new CancellationTokenSource(1000).Token;
            var activator = GetFakeActivator();
            var identity  = new ClusterIdentity {
                Kind = "thing", Identity = NextId().ToString()
            };
            var spawnLock = new SpawnLock("not-a-lock", identity);
            var pid       = Activate(activator, identity);

            _storage.Invoking(storage =>
                              storage.StoreActivation(activator.Id, spawnLock, pid, timeout)
                              ).Should().Throw <LockNotFoundException>();
        }
        public async Task StoreActivation(string memberId, SpawnLock spawnLock, PID pid, CancellationToken ct)
        {
            Logger.LogDebug("Storing activation: {@ActivatorId}, {@SpawnLock}, {@PID}", memberId, spawnLock, pid);
            var key = GetKey(spawnLock.ClusterIdentity);
            var res = await _asyncSemaphore.WaitAsync(() => _pids.UpdateOneAsync(
                                                          s => s.Key == key && s.LockedBy == spawnLock.LockId && s.Revision == 1,
                                                          Builders <PidLookupEntity> .Update
                                                          .Set(l => l.Address, pid.Address)
                                                          .Set(l => l.MemberId, memberId)
                                                          .Set(l => l.UniqueIdentity, pid.Id)
                                                          .Set(l => l.Revision, 2)
                                                          .Unset(l => l.LockedBy)
                                                          , new UpdateOptions(), ct
                                                          )
                                                      );

            if (res.MatchedCount != 1)
            {
                throw new LockNotFoundException($"Failed to store activation of {pid}");
            }
        }
 public Task RemoveLock(SpawnLock spawnLock, CancellationToken ct)
 => _asyncSemaphore.WaitAsync(() => _pids.DeleteManyAsync(p => p.LockedBy == spawnLock.LockId, ct));