Example #1
0
        private Grpc.Core.AsyncDuplexStreamingCall <Nacos.Payload, Nacos.Payload> BindRequestStream(Nacos.BiRequestStream.BiRequestStreamClient client, GrpcConnection grpcConn)
        {
            var call = client.requestBiStream();

            System.Threading.Tasks.Task.Factory.StartNew(
                async() =>
            {
                var cts = new System.Threading.CancellationTokenSource();

                while (await call.ResponseStream.MoveNext(cts.Token))
                {
                    var current = call.ResponseStream.Current;

                    var parseBody = GrpcUtils.Parse(current);

                    var request = (CommonRequest)parseBody;
                    if (request != null)
                    {
                        try
                        {
                            var response       = HandleServerRequest(request);
                            response.RequestId = request.RequestId;
                            await call.RequestStream.WriteAsync(GrpcUtils.Convert(response));
                        }
                        catch (Exception)
                        {
                            throw;
                        }
                    }
                }

                if (IsRunning() && !grpcConn.IsAbandon())
                {
                    logger?.LogInformation(" Request Stream onCompleted ,switch server ");

                    if (System.Threading.Interlocked.CompareExchange(ref rpcClientStatus, RpcClientStatus.UNHEALTHY, RpcClientStatus.RUNNING) == RpcClientStatus.RUNNING)
                    {
                        SwitchServerAsync();
                    }
                }
                else
                {
                    logger?.LogInformation("client is not running status ,ignore complete  event ");
                }
            }, System.Threading.Tasks.TaskCreationOptions.LongRunning);

            return(call);
        }
        private CommonResponse ServerCheck(Grpc.Core.ChannelBase channel)
        {
            try
            {
                var payload = GrpcUtils.Convert <object>(new { }, new RequestMeta {
                    Type = RemoteRequestType.Req_ServerCheck
                });

                var client = new Nacos.Request.RequestClient(channel);
                var resp   = client.request(payload);

                var res = GrpcUtils.Parse(resp);
                return((CommonResponse)res);
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "[{0}]Fail to server check!", GetName());
                return(null);
            }
        }