Exemple #1
0
        public async Task <string> Get()
        {
            ZooKeeperLock zooKeeperLock = new ZooKeeperLock();

            string result = null;

            if (await zooKeeperLock.TryLock(6000))
            {
                result = "Success";
            }
            else
            {
                result = "Fail";
            }
            await zooKeeperLock.UnLock();

            return(result);
        }
        GameUserShareAsync(
            GameUserShareRequest request)
        {
            try
            {
                var gameVersion = GameVersion;
                var userId      = request.UserId;
                // 分布式锁
                using (var zklock =
                           new ZooKeeperLock($"/DefaultGameManager/GameUserShareAsync/{gameVersion}/{userId:N}"))
                {
                    if (await zklock.WaitAsync(5000)) //如果锁释放 会立即执行,正常逻辑不会等待5秒钟
                    {
                        var l = await DalGameUserShare.InsertGameUserShareAsync(new GameUserShareModel
                        {
                            ActivityId = gameVersion,
                            UserId     = userId
                        });

                        if (l > 0)
                        {
                            return(null, null, new GameUserShareResponse()
                            {
                            });
                        }

                        return("-10", Resource.Invalid_Game_AlreadyShare, null);
                    }

                    return("-6", Resource.Invalid_Game_TryAgain, null);
                }
            }
            catch (Exception e)
            {
                Logger.Error($"{ManagerName} -> GameUserShareAsync -> {JsonConvert.SerializeObject(request)} ",
                             e.InnerException ?? e);
                throw;
            }
        }
Exemple #3
0
        public async Task <int?> GetCurrentActivityPurchaseCount()
        {
            int?result = null;

            string key         = GetActivityKey();
            var    dataInCache = await _counter.GetAsync <int>(new List <string>() { key });

            if (dataInCache.Success)
            {
                if (dataInCache.Value != null && dataInCache.Value.Any())
                {
                    result = dataInCache.Value.First().Value;
                }
                else
                {
                    using (var zlock = new ZooKeeperLock(key))
                    {
                        if (await zlock.WaitAsync(1500))
                        {
                            var retryGet = await _counter.GetAsync <int>(new List <string>() { key });

                            if (retryGet.Success && retryGet.Value != null && retryGet.Value.Any())
                            {
                                result = retryGet.Value.First().Value;
                            }
                            else
                            {
                                var orderIds = await DalFlashSale.SelectFlashSaleOrderIdsByActivityAsync(_activityId, _round.StartTime, _round.EndTime);

                                var count = 0;
                                if (orderIds != null && orderIds.Any())
                                {
                                    var orderStatus = await DalFlashSale.SelectOrderStatusByOrderIdsAsync(orderIds);

                                    count = orderStatus.Where(o => !string.Equals(o.Value, "7Canceled")).Count();
                                }

                                var increResult = await _counter.IncrementAsync(key, count);

                                if (increResult.Success)
                                {
                                    if (increResult.Value > count)
                                    {
                                        bool decResult = await RetryDecrement(key, count);
                                    }
                                    else
                                    {
                                        result = count;
                                    }
                                }
                                else
                                {
                                    _logger.Error($"计数器设置失败, activity: {_activityId}, {dataInCache.Message}", dataInCache.Exception);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                _logger.Error($"计数器获取失败, {dataInCache.Message}", dataInCache.Exception);
            }

            return(result);
        }
        public async Task <int?> GetCurrentActivityPurchaseCount()
        {
            int?result = null;
            var sw     = new Stopwatch();

            sw.Start();
            long   getCountCache  = 0;
            long   beforeLockTime = 0;
            long   inLockTime     = 0;
            long   selectFlashSaleOrderIdsByActivityTime = 0;
            long   selectOrderStatusByOrderIdsTime       = 0;
            string key         = GetActivityKey();
            var    dataInCache = await _counter.GetAsync <int>(new List <string>() { key });

            getCountCache = sw.ElapsedMilliseconds;
            if (dataInCache.Success)
            {
                if (dataInCache.Value != null && dataInCache.Value.Any())
                {
                    result = dataInCache.Value.First().Value;
                }
                else
                {
                    beforeLockTime = sw.ElapsedMilliseconds - getCountCache;
                    using (var zlock = new ZooKeeperLock(key))
                    {
                        if (await zlock.WaitAsync(1500))
                        {
                            inLockTime = sw.ElapsedMilliseconds - beforeLockTime;
                            var retryGet = await _counter.GetAsync <int>(new List <string>() { key });

                            if (retryGet.Success && retryGet.Value != null && retryGet.Value.Any())
                            {
                                result = retryGet.Value.First().Value;
                            }
                            else
                            {
                                var orderIds = await DalFlashSale.SelectFlashSaleOrderIdsByActivityAsync(_activityId, _round.StartTime, _round.EndTime);

                                selectFlashSaleOrderIdsByActivityTime = sw.ElapsedMilliseconds - inLockTime;
                                var count = 0;
                                if (orderIds != null && orderIds.Any())
                                {
                                    var orderStatus = await DalFlashSale.SelectOrderStatusByOrderIdsAsync(orderIds);

                                    selectOrderStatusByOrderIdsTime = sw.ElapsedMilliseconds - selectFlashSaleOrderIdsByActivityTime;
                                    count = orderStatus.Where(o => !string.Equals(o.Value, "7Canceled")).Count();
                                }

                                var increResult = await _counter.IncrementAsync(key, count);

                                if (increResult.Success)
                                {
                                    if (increResult.Value > count)
                                    {
                                        bool decResult = await RetryDecrement(key, count);
                                    }
                                    else
                                    {
                                        result = count;
                                    }
                                }
                                else
                                {
                                    _logger.Error($"计数器设置失败, activity: {_activityId}, {dataInCache.Message}", dataInCache.Exception);
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                _logger.Error($"计数器获取失败, {dataInCache.Message}", dataInCache.Exception);
            }
            _logger.Info($"GetCurrentActivityPurchaseCount=>getCountCache:{getCountCache}," +
                         $"beforeLockTime:{beforeLockTime},inLockTime:{inLockTime}," +
                         $"selectFlashSaleOrderIdsByActivityTime:{selectFlashSaleOrderIdsByActivityTime}," +
                         $"selectOrderStatusByOrderIdsTime:{selectOrderStatusByOrderIdsTime}," +
                         $"total:{sw.ElapsedMilliseconds}");
            sw.Stop();
            return(result);
        }