예제 #1
0
        public IWatcher Watch(ByteSequence key, WatchOption watchOption)
        {
            if (IsClosed())
            {
                throw new ClosedWatchClientException();
            }
            WatcherImpl watcher = new WatcherImpl(key, watchOption, this);

            // this.pendingWatchers.Enqueue(watcher);

            Etcdserverpb.WatchRequest       request       = new Etcdserverpb.WatchRequest();
            Etcdserverpb.WatchCreateRequest createRequest = new Etcdserverpb.WatchCreateRequest();
            createRequest.Key            = key.GetByteString();
            createRequest.PrevKv         = watchOption.isPrevKV();
            createRequest.ProgressNotify = watchOption.isProgressNotify();
            createRequest.RangeEnd       = watchOption.getEndKey().GetByteString();
            createRequest.StartRevision  = watchOption.getRevision();
            request.CreateRequest        = createRequest;
            Grpc.Core.CallOptions callOptions = new Grpc.Core.CallOptions();
            watchClient.Watch(callOptions);
            // watchClient.Watch()
            // watchClient.Watch()

            //  if (this.pendingWatchers.Count == 1) {
            // head of the queue send watchCreate request.
            //  WatchRequest request = this.toWatchCreateRequest(watcher);
            // this.getGrpcWatchStreamObserver().onNext(request);
            // }

            return(watcher);
        }
예제 #2
0
        public async Task <string> GetAsync(CancellationToken cancellationToken)
        {
            // fixed https error
            var httpclientHandler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true
            };

            var httpClient = new HttpClient(httpclientHandler)
            {
                // The port number(5001) must match the port of the gRPC server.
                BaseAddress = new Uri("https://localhost:5001")
            };

            try
            {
                var client = GrpcClient.Create <UserInfoRpcService.UserInfoRpcServiceClient>(httpClient);

                var callOptions = new Grpc.Core.CallOptions()
                                  // StatusCode=Cancelled
                                  .WithCancellationToken(cancellationToken)
                                  // StatusCode=DeadlineExceeded
                                  .WithDeadline(DateTime.UtcNow.AddMilliseconds(200));

                var reply = await client.GetByIdAsync(new GetUserByIdRequest { Id = 1 }, callOptions);

                return(reply.Name);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "some exception occure");

                return("error");
            }
        }
예제 #3
0
        public LeaseKeepAliveResponse KeepAliveOnce(long leaseId, LeaseOption option = null)
        {
            Grpc.Core.CallOptions callOptions = new Grpc.Core.CallOptions();

            var rsp = leaseClient.LeaseKeepAlive(callOptions);

            return(null);
        }
예제 #4
0
        public ICloseableClient KeepAlive(long leaseId)
        {
            Grpc.Core.CallOptions callOptions = new Grpc.Core.CallOptions();
            //callOptions.
            var rsp = leaseClient.LeaseKeepAlive(callOptions);

            return(null);
        }
예제 #5
0
        static void GrpcDemoConcurrentTest()
        {
            var tasks = new Thread[10];

            for (var i = 0; i < tasks.Length; i++)
            {
                tasks[i] = new Thread(() =>
                {
                    var key = new GrpcDemo.ServiceKey()
                    {
                        Key = "test"
                    };
                    for (var j = 0; j < 1000; j++)
                    {
                        try
                        {
                            using (var client = ConsulHelper.Instance.GetServiceClient("grpcdemo"))
                            {
                                var stub        = client.GetStub <GrpcDemo.DemoService.DemoServiceClient>();
                                var callOptions = new Grpc.Core.CallOptions().WithDeadline(DateTime.UtcNow.AddMilliseconds(5000));
                                var ret         = stub.GetKeyValue(key, callOptions);
                                if (string.IsNullOrEmpty(ret.Value))
                                {
                                    Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + ret.GetHashCode());
                                }
                                if (j % 100 == 0)
                                {
                                    Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + j);
                                }
                            }
                        }
                        catch (Exception err)
                        {
                            Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + err.Message);
                            if (err.InnerException != null)
                            {
                                Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + err.InnerException.Message);
                            }
                        }
                    }
                });
                tasks[i].Start();
            }
            int idleCount = 0, activeCount = 0;

            do
            {
                Thread.Sleep(5000);
                var clientCount = ConsulHelper.Instance.GetServiceClientCount("grpcdemo", "grpc");
                idleCount   = clientCount.Item1;
                activeCount = clientCount.Item2;
                Console.WriteLine(string.Format("ClientCount<{0},{1}>", idleCount, activeCount));
            }while (idleCount != activeCount);
            Console.ReadLine();
        }
예제 #6
0
        private void SendKeepAliveExecutor()
        {
            Grpc.Core.CallOptions callOptions = new Grpc.Core.CallOptions();
            //callOptions.
            var rsp = leaseClient.LeaseKeepAlive(callOptions);

            try
            {
                timer.Elapsed -= Timer_Elapsed;
            }
            catch
            {
            }

            timer.Elapsed += Timer_Elapsed;
        }
예제 #7
0
        public ICloseableClient KeepAlive(long leaseId)
        {
            KeepAlive <Etcdserverpb.LeaseKeepAliveRequest, Etcdserverpb.LeaseKeepAliveResponse> item = null;

            if (!keepAlives.TryGetValue(leaseId, out item))
            {
                item = new KeepAlive <Etcdserverpb.LeaseKeepAliveRequest, Etcdserverpb.LeaseKeepAliveResponse>(leaseId);
            }

            Grpc.Core.CallOptions callOptions = new Grpc.Core.CallOptions();
            //callOptions.

            var rsp = leaseClient.LeaseKeepAlive(callOptions);

            item.AddObserver(rsp);

            return(item);
        }
예제 #8
0
            public void Resume()
            {
                if (closed)
                {
                    return;
                }

                if (stream == null)
                {
                    // id is not really useful today but it may be in etcd 3.4
                    watchID = -1;
                    Etcdserverpb.WatchCreateRequest createRequest = new Etcdserverpb.WatchCreateRequest
                    {
                        Key            = key.GetByteString(),
                        PrevKv         = watchOption.IsPrevKV,
                        ProgressNotify = watchOption.IsProgressNotify,
                        RangeEnd       = watchOption.EndKey.GetByteString(),
                        StartRevision  = watchOption.Revision
                    };
                    if (watchOption.EndKey.IsPresent)
                    {
                        createRequest.RangeEnd = watchOption.EndKey.GetByteString();
                    }
                    if (watchOption.IsNoDelete)
                    {
                        createRequest.Filters.Add(Etcdserverpb.WatchCreateRequest.Types.FilterType.Nodelete);
                    }
                    if (watchOption.IsNoPut)
                    {
                        createRequest.Filters.Add(Etcdserverpb.WatchCreateRequest.Types.FilterType.Noput);
                    }
                    Grpc.Core.CallOptions     callOptions = new Grpc.Core.CallOptions();
                    Etcdserverpb.WatchRequest request     = new Etcdserverpb.WatchRequest
                    {
                        CreateRequest = createRequest
                    };
                    var rsp = watchClient.Watch(callOptions);
                    rsp.RequestStream.WriteAsync(request);
                    stream       = rsp;
                    this.watchID = rsp.ResponseStream.Current.WatchId;
                }
            }
예제 #9
0
        static void ESGrpcConcurrentTest()
        {
            var tasks = new Thread[10];

            for (var i = 0; i < tasks.Length; i++)
            {
                tasks[i] = new Thread(() =>
                {
                    var param           = new TaocheES.SearchCondition();
                    param.RequestSource = 9;
                    param.PageIndex     = 1;
                    param.PageSize      = 200;
                    param.ReturnFieldArray.Add("ucarid");
                    param.ReturnFieldArray.Add("userid");
                    param.ReturnFieldArray.Add("color");
                    param.ReturnFieldArray.Add("displayprice");
                    param.ReturnFieldArray.Add("cartitle");
                    param.CommonFlag = 0;
                    for (var j = 0; j < 1000; j++)
                    {
                        try
                        {
                            using (var client = ConsulHelper.Instance.GetServiceClient("esgrpc"))
                            {
                                var stub        = client.GetStub <TaocheES.TaocheESService.TaocheESServiceClient>();
                                var callOptions = new Grpc.Core.CallOptions().WithDeadline(DateTime.UtcNow.AddMilliseconds(5000));
                                var ret         = stub.SearchTaocheCar(param, callOptions);
                                if (ret.Count <= 0)
                                {
                                    Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + ret.Count);
                                }
                                if (j % 100 == 0)
                                {
                                    Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + j);
                                }
                            }
                        }
                        catch (Exception err)
                        {
                            Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + err.Message);
                            if (err.InnerException != null)
                            {
                                Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + err.InnerException.Message);
                            }
                        }
                    }
                });
                tasks[i].Start();
            }
            int idleCount = 0, activeCount = 0;

            do
            {
                Thread.Sleep(5000);
                var clientCount = ConsulHelper.Instance.GetServiceClientCount("esgrpc", "grpc");
                idleCount   = clientCount.Item1;
                activeCount = clientCount.Item2;
                Console.WriteLine(string.Format("ClientCount<{0},{1}>", idleCount, activeCount));
            }while (idleCount != activeCount);
            Console.ReadLine();
        }