예제 #1
0
 public void Delete(string ServiceName)
 {
     try
     {
         _client.DeleteRangeAsync($"/EtcdDiscovery/{ServiceName}").Wait();
         lock (watchers)
         {
             watchers.RemoveWhere(c => c.key == Google.Protobuf.ByteString.CopyFromUtf8($"/EtcdDiscovery/{ServiceName}"));
         }
     }
     catch (Exception ex)
     {
         if (ex.HResult != -2146233086)
         {
             throw ex;
         }
     }
 }
예제 #2
0
        public UnLockResult UnLock(string lockname, long Id)
        {
            ZkLockerWatcher watcher  = null;
            bool            localhas = false;
            bool            srvhas   = false;

            lock (_lockerList)
            {
                var locallst = _lockerList.Where(c => c.Name == lockname).ToList();
                if (locallst.Count > 0)
                {
                    if (locallst.FirstOrDefault().Id != Id)
                    {
                        return(UnLockResult.NotSameLockId);
                    }
                    else
                    {
                        watcher  = locallst.FirstOrDefault();
                        localhas = true;
                    }
                }
            }

            if (!localhas)
            {
                Etcdserverpb.RangeResponse oldlockers = null;
                try
                {
                    oldlockers = _client.GetAsync($"/locks/{lockname}").Result;
                }
                catch (Exception ex)
                {
                    lock (_client)
                    {
                        _client = bestClient(_clients);
                    }
                    return(UnLockResult.Fail);
                }
                if (oldlockers?.Kvs.Count > 0)
                {
                    if (long.Parse(oldlockers.Kvs[0].Value.ToStringUtf8()) != Id)
                    {
                        return(UnLockResult.NotSameLockId);
                    }
                    else
                    {
                        srvhas = true;
                    }
                }
            }

            if (localhas || srvhas)
            {
                long deled = 0;
                try
                {
                    deled = _client.DeleteRangeAsync($"/locks/{lockname}").Result.Deleted;
                    if (localhas)
                    {
                        lock (_lockerList)
                        {
                            _lockerList.RemoveWhere(c => c.Id == Id && c.Name == lockname);
                        }
                    }
                }
                catch (Exception ex)
                {
                    lock (_client)
                    {
                        _client = bestClient(_clients);
                    }
                    return(UnLockResult.Fail);
                }

                if (deled > 0)
                {
                    return(UnLockResult.Success);
                }
                else
                {
                    return(UnLockResult.NotExists);
                }
            }
            else
            {
                return(UnLockResult.NotExists);
            }
        }