Beispiel #1
0
        /// <summary>
        /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
        /// to the server and streaming keep alive responses from the server to the client.
        /// </summary>
        /// <param name="requests"></param>
        /// <param name="methods"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param>
        /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param>
        public async Task LeaseKeepAlive(LeaseKeepAliveRequest[] requests, Action <LeaseKeepAliveResponse>[] methods,
                                         CancellationToken cancellationToken, Grpc.Core.Metadata headers = null, DateTime?deadline = null)
        {
            await CallEtcdAsync(async (connection) =>
            {
                using (AsyncDuplexStreamingCall <LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser =
                           connection.leaseClient
                           .LeaseKeepAlive(headers, deadline, cancellationToken))
                {
                    Task leaserTask = Task.Run(async() =>
                    {
                        while (await leaser.ResponseStream.MoveNext(cancellationToken))
                        {
                            LeaseKeepAliveResponse update = leaser.ResponseStream.Current;
                            foreach (Action <LeaseKeepAliveResponse> method in methods)
                            {
                                method(update);
                            }
                        }
                    }, cancellationToken);

                    foreach (LeaseKeepAliveRequest request in requests)
                    {
                        await leaser.RequestStream.WriteAsync(request);
                    }

                    await leaser.RequestStream.CompleteAsync();
                    await leaserTask;
                }
            });
        }
Beispiel #2
0
        /// <summary>
        /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
        /// to the server and streaming keep alive responses from the server to the client.
        /// </summary>
        /// <param name="requests"></param>
        /// <param name="methods"></param>
        /// <param name="token"></param>
        public async void LeaseKeepAlive(LeaseKeepAliveRequest[] requests, Action <LeaseKeepAliveResponse>[] methods, CancellationToken token, Metadata headers = null)
        {
            using (AsyncDuplexStreamingCall <LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser = _balancer.GetConnection().leaseClient.LeaseKeepAlive(headers))
            {
                Task leaserTask = Task.Run(async() =>
                {
                    while (await leaser.ResponseStream.MoveNext(token))
                    {
                        LeaseKeepAliveResponse update = leaser.ResponseStream.Current;
                        foreach (Action <LeaseKeepAliveResponse> method in methods)
                        {
                            method(update);
                        }
                    }
                });

                foreach (LeaseKeepAliveRequest request in requests)
                {
                    await leaser.RequestStream.WriteAsync(request);
                }

                await leaser.RequestStream.CompleteAsync();

                await leaserTask;
            }
        }
Beispiel #3
0
        /// <summary>
        /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
        /// to the server and streaming keep alive responses from the server to the client.
        /// </summary>
        /// <param name="leaseId"></param>
        /// <param name="cancellationToken"></param>
        public async Task LeaseKeepAlive(long leaseId, CancellationToken cancellationToken)
        {
            await CallEtcdAsync(async (connection) =>
            {
                using (AsyncDuplexStreamingCall <LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser =
                           connection.leaseClient.LeaseKeepAlive(cancellationToken: cancellationToken))
                {
                    LeaseKeepAliveRequest request = new LeaseKeepAliveRequest
                    {
                        ID = leaseId
                    };

                    while (true)
                    {
                        cancellationToken.ThrowIfCancellationRequested();

                        await leaser.RequestStream.WriteAsync(request);
                        if (!await leaser.ResponseStream.MoveNext(cancellationToken))
                        {
                            await leaser.RequestStream.CompleteAsync();
                            throw new EndOfStreamException();
                        }

                        LeaseKeepAliveResponse update = leaser.ResponseStream.Current;
                        if (update.ID != leaseId || update.TTL == 0) // expired
                        {
                            await leaser.RequestStream.CompleteAsync();
                            return;
                        }

                        await Task.Delay(TimeSpan.FromMilliseconds(update.TTL * 1000 / 3), cancellationToken);
                    }
                }
            });
        }
Beispiel #4
0
 /// <summary>
 /// 返回
 /// </summary>
 /// <param name="response"></param>
 private void Watch(LeaseKeepAliveResponse response)
 {
     // response.Header.
     //刷新返回了
     Console.WriteLine("刷新:" + response.ID);
     udateID[response.ID.ToString() + "_" + response.Header.ClusterId.ToString()] = DateTime.Now.Ticks;
 }
Beispiel #5
0
        /// <summary>
        /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
        /// to the server and streaming keep alive responses from the server to the client.
        /// </summary>
        /// <param name="requests"></param>
        /// <param name="method"></param>
        /// <param name="cancellationToken"></param>
        /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param>
        public async Task LeaseKeepAlive(LeaseKeepAliveRequest[] requests, Action <LeaseKeepAliveResponse> method,
                                         CancellationToken cancellationToken, Grpc.Core.Metadata headers = null) => await CallEtcdAsync(async (connection) =>
        {
            using (AsyncDuplexStreamingCall <LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser =
                       connection._leaseClient
                       .LeaseKeepAlive(headers, cancellationToken: cancellationToken))
            {
                Task leaserTask = Task.Run(async() =>
                {
                    while (await leaser.ResponseStream.MoveNext(cancellationToken).ConfigureAwait(false))
                    {
                        LeaseKeepAliveResponse update = leaser.ResponseStream.Current;
                        method(update);
                    }
                }, cancellationToken);

                foreach (LeaseKeepAliveRequest request in requests)
                {
                    await leaser.RequestStream.WriteAsync(request).ConfigureAwait(false);
                }

                await leaser.RequestStream.CompleteAsync().ConfigureAwait(false);
                await leaserTask.ConfigureAwait(false);
            }
        }).ConfigureAwait(false);
Beispiel #6
0
        /// <summary>
        /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
        /// to the server and streaming keep alive responses from the server to the client.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="method"></param>
        /// <param name="token"></param>
        public async void LeaseKeepAlive(LeaseKeepAliveRequest request, Action <LeaseKeepAliveResponse> method, CancellationToken token)
        {
            try
            {
                using (AsyncDuplexStreamingCall <LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser = _leaseClient.LeaseKeepAlive(_headers))
                {
                    Task leaserTask = Task.Run(async() =>
                    {
                        while (await leaser.ResponseStream.MoveNext(token))
                        {
                            LeaseKeepAliveResponse update = leaser.ResponseStream.Current;
                            method(update);
                        }
                    });

                    await leaser.RequestStream.WriteAsync(request);

                    await leaser.RequestStream.CompleteAsync();

                    await leaserTask;
                }
            }
            catch (RpcException ex) when(ex.Status.Equals(StatusCode.Unavailable))
            {
                // If connection issue, then re-initate the LeaseKeepAlive request
                ResetConnection(ex);
                LeaseKeepAlive(request, method, token);
            }
            catch
            {
                throw;
            }
        }
Beispiel #7
0
 private void HandleLeaseKeepAliveRequest(LeaseKeepAliveResponse response)
 {
     if (response.TTL == LeaseTtl)
     {
         return;
     }
     Console.WriteLine($"HandleLeaseKeepAliveRequest: failed to keep lease alive for leaderKey {_leaderKey}");
     _leader = false;
 }
Beispiel #8
0
        private async void KeepAliveResponseHandler(LeaseKeepAliveResponse leaseKeepAliveResponse)
        {
            //Console.WriteLine(leaseKeepAliveResponse);
            if (leaseKeepAliveResponse.TTL == LeaseTtl)
            {
                return;
            }
            await _timer.DisposeAsync();

            await ImHere();
        }
        /// <summary>
        /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
        /// to the server and streaming keep alive responses from the server to the client.
        /// </summary>
        /// <param name="leaseId"></param>
        /// <param name="cancellationToken"></param>
        public async Task LeaseKeepAlive(long leaseId, CancellationToken cancellationToken)
        {
            int retryCount = 0;

            while (true)
            {
                try
                {
                    using (AsyncDuplexStreamingCall <LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser =
                               _balancer.GetConnection().leaseClient.LeaseKeepAlive(cancellationToken: cancellationToken))
                    {
                        LeaseKeepAliveRequest request = new LeaseKeepAliveRequest
                        {
                            ID = leaseId
                        };

                        while (true)
                        {
                            cancellationToken.ThrowIfCancellationRequested();

                            await leaser.RequestStream.WriteAsync(request);

                            if (!await leaser.ResponseStream.MoveNext(cancellationToken))
                            {
                                await leaser.RequestStream.CompleteAsync();

                                throw new EndOfStreamException();
                            }

                            LeaseKeepAliveResponse update = leaser.ResponseStream.Current;
                            if (update.ID != leaseId || update.TTL == 0) // expired
                            {
                                await leaser.RequestStream.CompleteAsync();

                                return;
                            }

                            await Task.Delay(TimeSpan.FromMilliseconds(update.TTL * 1000 / 3), cancellationToken);
                        }
                    }
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
                {
                    retryCount++;
                    if (retryCount >= _balancer._numNodes)
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
        /// to the server and streaming keep alive responses from the server to the client.
        /// </summary>
        /// <param name="requests"></param>
        /// <param name="methods"></param>
        /// <param name="token"></param>
        /// <param name="headers">The initial metadata to send with the call. This parameter is optional.</param>
        /// <param name="deadline">An optional deadline for the call. The call will be cancelled if deadline is hit.</param>
        public async Task LeaseKeepAlive(LeaseKeepAliveRequest[] requests, Action <LeaseKeepAliveResponse>[] methods,
                                         CancellationToken cancellationToken, Grpc.Core.Metadata headers = null, DateTime?deadline = null)
        {
            bool success    = false;
            int  retryCount = 0;

            while (!success)
            {
                try
                {
                    using (AsyncDuplexStreamingCall <LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser =
                               _balancer.GetConnection().leaseClient
                               .LeaseKeepAlive(headers, deadline, cancellationToken))
                    {
                        Task leaserTask = Task.Run(async() =>
                        {
                            while (await leaser.ResponseStream.MoveNext(cancellationToken))
                            {
                                LeaseKeepAliveResponse update = leaser.ResponseStream.Current;
                                foreach (Action <LeaseKeepAliveResponse> method in methods)
                                {
                                    method(update);
                                }
                            }
                        }, cancellationToken);

                        foreach (LeaseKeepAliveRequest request in requests)
                        {
                            await leaser.RequestStream.WriteAsync(request);
                        }

                        await leaser.RequestStream.CompleteAsync();

                        await leaserTask;
                    }

                    success = true;
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
                {
                    retryCount++;
                    if (retryCount >= _balancer._numNodes)
                    {
                        throw;
                    }
                }
            }
        }
Beispiel #11
0
        /// <summary>
        /// LeaseKeepAlive keeps the lease alive by streaming keep alive requests from the client
        /// to the server and streaming keep alive responses from the server to the client.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="method"></param>
        /// <param name="token"></param>
        public async void LeaseKeepAlive(LeaseKeepAliveRequest request, Action <LeaseKeepAliveResponse> method, CancellationToken token, Metadata headers = null)
        {
            bool success    = false;
            int  retryCount = 0;

            while (!success)
            {
                try
                {
                    using (AsyncDuplexStreamingCall <LeaseKeepAliveRequest, LeaseKeepAliveResponse> leaser = _balancer.GetConnection().leaseClient.LeaseKeepAlive(headers))
                    {
                        Task leaserTask = Task.Run(async() =>
                        {
                            while (await leaser.ResponseStream.MoveNext(token))
                            {
                                LeaseKeepAliveResponse update = leaser.ResponseStream.Current;
                                method(update);
                            }
                        });

                        await leaser.RequestStream.WriteAsync(request);

                        await leaser.RequestStream.CompleteAsync();

                        await leaserTask;
                    }
                    success = true;
                }
                catch (RpcException ex) when(ex.StatusCode == StatusCode.Unavailable)
                {
                    retryCount++;
                    if (retryCount >= _balancer._numNodes)
                    {
                        throw ex;
                    }
                }
            }
        }
Beispiel #12
0
 private static void Print(LeaseKeepAliveResponse response)
 {
     //Console.WriteLine(response);
 }
 /// <summary>
 /// 返回
 /// </summary>
 /// <param name="response"></param>
 private static void Watch(LeaseKeepAliveResponse response)
 {
     //刷新返回了
     Console.WriteLine("注册:" + response.ID);
 }