public async Task ReleaseAsync() { if (leaseClient != null) { await leaseClient.ReleaseAsync(); leaseClient = null; } }
LeaseImpl(ClientConnectionManager connectionManager) { this.connectionManager = connectionManager; managedChannel = connectionManager.NewChannel(); leaseClient = new LeaseClient(managedChannel.channel); // this.stub = connectionManager.NewStub<LeaseFutureStub>(null); // this.leaseStub = connectionManager.NewStub<LeaseStub>(null); // this.keepAlives = new ConcurrentDictionary<string, impl.KeepAlive>(); //this.scheduledExecutorService = MoreExecutors.listeningDecorator(Executors.newScheduledThreadPool(2)); }
public EtcdClient(params Uri[] etcdUrls) { this.channel = new Channel(etcdUrls[0].Host, etcdUrls[0].Port, ChannelCredentials.Insecure); this.kvClient = new KV.KVClient(channel); this.watchClient = new WatchClient(this.channel); this.leaseClient = new LeaseClient(this.channel); var asyncDuplexStreamingCall = leaseClient.LeaseKeepAlive(); this.leaseTTLRefresher = new EtcdLeaseTTLRefresher(asyncDuplexStreamingCall); }
public async Task <bool> AcquireAsync() { var acquisitionStarted = DateTimeOffset.UtcNow; do { attempts++; var blobClient = containerClient.GetBlobClient(lockIdentifier); leaseClient = blobClient.GetLeaseClient(); try { await leaseClient.AcquireAsync(lockDuration); return(true); } catch (StorageRequestFailedException ex) when(ex.ErrorCode == "BlobNotFound") { var contentBytes = Encoding.UTF8.GetBytes(lockIdentifier); var contentStream = new MemoryStream(contentBytes); await blobClient.UploadAsync(contentStream); } catch (StorageRequestFailedException ex) when(ex.ErrorCode == "LeaseAlreadyPresent") { // Simple backoff logic. var tryImmediatelyOrNot = random.Next(0, 3); var maxBackOffDuration = attempts * (int)backoffInterval.TotalMilliseconds; var randomBackOff = random.Next(0, maxBackOffDuration); int delay = tryImmediatelyOrNot * randomBackOff; await Task.Delay(delay); } catch (Exception ex) { throw ex; } } while (attempts < maxRetries || DateTimeOffset.UtcNow > acquisitionStarted + acquisitionTimeout); return(false); }