Here are the examples of the csharp api System.Collections.Generic.HashSet.Add(ZkLockerWatcher) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
1 Examples
19
Source : Client.cs
with MIT License
from chubbyerror
with MIT License
from chubbyerror
public LockModel Lock(string lockname)
{
LockModel @lock = new LockModel();
Etcdserverpb.RangeResponse oldlockers = null;
//use local cache speed up.
lock (_lockerList)
{
var locallst = _lockerList.Where(c => c.Name == lockname).ToList();
if (locallst.Count > 0)
{
@lock.Result = LockResult.LockExists;
@lock.Id = locallst.FirstOrDefault().Id;
return @lock;
}
}
//confirm lock not exists.
try
{
oldlockers = _client.GetAsync($"/locks/{lockname}").Result;
}
catch (Exception ex)
{
@lock.Id = 0;
@lock.Result = LockResult.Fail;
lock (_client)
{
_client = bestClient(_clients);
}
return @lock;
}
//return lockid to locker .
if (oldlockers?.Kvs.Count > 0)
{
@lock.Id = long.Parse(oldlockers.Kvs.FirstOrDefault().Value.ToStringUtf8());
@lock.Result = LockResult.LockExists;
}
else
{
//creat lock id
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
@lock.Id = (long)ts.TotalMilliseconds;
@lock.Result = LockResult.Success;
try
{
Etcdserverpb.LeaseGrantRequest request = new Etcdserverpb.LeaseGrantRequest();
request.TTL = _locklease;
var lease= _client.LeaseGrant(request);
Etcdserverpb.PutRequest put = new Etcdserverpb.PutRequest();
put.Key =Google.Protobuf.ByteString.CopyFromUtf8(($"/locks/{lockname}"));
put.Value = Google.Protobuf.ByteString.CopyFromUtf8(@lock.Id.ToString());
put.Lease = lease.ID;
_client.Put(put);
//add to cache and srv .
_client.Put($"/locks/{lockname}", @lock.Id.ToString());
lock (_lockerList)
{
_lockerList.Add(new ZkLockerWatcher() { Event = null, Id = @lock.Id, Name = lockname });
}
}
catch (Exception ex)
{
lock (_client)
{
_client = bestClient(_clients);
}
@lock.Result = LockResult.Fail;
}
}
return @lock;
}