Exemplo n.º 1
0
        public void UnLockAnyway(string key)
        {
            if (_microServiceHost.MasterGatewayAddress == null)
            {
                throw new MissMasterGatewayException("未连接上主网关");
            }

            var transactionId = "";

            RemovingKeyDict.TryAdd(key, transactionId);
            DateTime startime = DateTime.Now;

            while (true)
            {
                try
                {
                    //如果连接网关失败
                    using (var netclient = GatewayConnector.CreateClient(_microServiceHost.MasterGatewayAddress))
                    {
                        netclient.WriteServiceData(new GatewayCommand
                        {
                            Type    = CommandType.LockKey,
                            Content = new LockKeyInfo
                            {
                                Key            = key,
                                MicroServiceId = "$$$",//表示强制释放
                                IsUnlock       = true
                            }.ToJsonString()
                        });

                        var ret = netclient.ReadServiceObject <InvokeResult <string> >();
                        if (!ret.Success && ret.Data != null)
                        {
                            throw new Exception(ret.Data);
                        }
                    }
                    LockedKeyDict.TryRemove(key, out transactionId);
                    RemovingKeyDict.TryRemove(key, out transactionId);
                    break;
                }
                catch (Exception)
                {
                    //如果发生错误,可以不断重试,直到超时为止
                    if ((DateTime.Now - startime).TotalMilliseconds > _gatewayKeyTimeout)
                    {
                        //如果已经连不上网关,网关会在10秒内释放这个key
                        LockedKeyDict.TryRemove(key, out transactionId);
                        RemovingKeyDict.TryRemove(key, out transactionId);
                        throw;
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
            }
        }
Exemplo n.º 2
0
        public bool TryLock(string transactionId, string key)
        {
            if (_microServiceHost.MasterGatewayAddress == null)
            {
                throw new MissMasterGatewayException("未连接上主网关");
            }
            if (string.IsNullOrEmpty(transactionId))
            {
                throw new Exception("tranid is empty");
            }

            LockedKeyDict.TryGetValue(key, out string lockerTranId);
            if (lockerTranId == transactionId)
            {
                return(true);
            }

            if (LockedKeyDict.TryAdd(key, transactionId))
            {
                try
                {
                    using (var netclient = GatewayConnector.CreateClient(_microServiceHost.MasterGatewayAddress))
                    {
                        netclient.WriteServiceData(new GatewayCommand
                        {
                            Type    = CommandType.LockKey,
                            Content = new LockKeyInfo
                            {
                                Key            = key,
                                MicroServiceId = _microServiceHost.Id,
                            }.ToJsonString()
                        });

                        var ret = netclient.ReadServiceObject <InvokeResult <string> >();
                        if (ret.Success == false)
                        {
                            LockedKeyDict.TryRemove(key, out transactionId);
                        }

                        //记录网关的超时时间
                        if (ret.Success)
                        {
                            _gatewayKeyTimeout = Convert.ToInt32(ret.Data);
                        }

                        return(ret.Success);
                    }
                }
                catch (Exception)
                {
                    LockedKeyDict.TryRemove(key, out transactionId);
                    throw;
                }
            }

            return(false);
        }
Exemplo n.º 3
0
        public bool TryUnLock(string transactionId, string key)
        {
            if (_microServiceHost.MasterGatewayAddress == null)
            {
                throw new MissMasterGatewayException("未连接上主网关");
            }
            if (string.IsNullOrEmpty(transactionId))
            {
                throw new Exception("tranid is empty");
            }

            if (LockedKeyDict.TryGetValue(key, out string locker))
            {
                if (locker == transactionId)
                {
                    RemovingKeyDict.TryAdd(key, transactionId);
                    DateTime startime = DateTime.Now;
                    while (true)
                    {
                        try
                        {
                            //如果连接网关失败
                            using (var netclient = GatewayConnector.CreateClient(_microServiceHost.MasterGatewayAddress))
                            {
                                netclient.WriteServiceData(new GatewayCommand
                                {
                                    Type    = CommandType.LockKey,
                                    Content = new LockKeyInfo
                                    {
                                        Key            = key,
                                        MicroServiceId = _microServiceHost.Id,
                                        IsUnlock       = true
                                    }.ToJsonString()
                                });

                                var ret = netclient.ReadServiceObject <InvokeResult <string> >();
                                LockedKeyDict.TryRemove(key, out transactionId);
                                RemovingKeyDict.TryRemove(key, out transactionId);

                                if (!ret.Success && ret.Data != null)
                                {
                                    throw new Exception(ret.Data);
                                }
                                return(ret.Success);
                            }

                            break;
                        }
                        catch (Exception)
                        {
                            //如果发生错误,可以不断重试,直到超时为止
                            if ((DateTime.Now - startime).TotalMilliseconds > _gatewayKeyTimeout)
                            {
                                //如果已经连不上网关,网关会在10秒内释放这个key
                                LockedKeyDict.TryRemove(key, out transactionId);
                                RemovingKeyDict.TryRemove(key, out transactionId);
                                throw;
                            }
                            else
                            {
                                Thread.Sleep(1000);
                            }
                        }
                    }
                }
            }
            return(false);
        }