Exemplo n.º 1
0
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await bootstrapChannel.CloseAsync();

            var quietPeriod     = configuration.QuietPeriodTimeSpan;
            var shutdownTimeout = configuration.ShutdownTimeoutTimeSpan;
            await workerGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);

            await bossGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);
        }
Exemplo n.º 2
0
        private async Task ShutdownGroupAsync()
        {
            if (_bossGroup == null || _workerGroup == null)
            {
                throw new NotImplementedException("Need initialize netty server");
            }

            await Task.WhenAll(
                _bossGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1)),
                _workerGroup.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(1))
                );
        }
Exemplo n.º 3
0
        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await bootstrapChannel.CloseAsync();

            var quietPeriod     = configuration.QuietPeriodTimeSpan;
            var shutdownTimeout = configuration.ShutdownTimeoutTimeSpan;
            await workerGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);

            await bossGroup.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);

            foreach (var item in Provider.GetServices <IRpcClient>())
            {
                await item.ShutdownGracefullyAsync(quietPeriod, shutdownTimeout);
            }
        }
Exemplo n.º 4
0
        public override void TestShutdownGracefullyNoQuietPeriod()
        {
            IEventLoopGroup loop = new DispatcherEventLoopGroup();
            ServerBootstrap b    = new ServerBootstrap();

            b.Group(loop)
            .Channel <TcpServerChannel>()
            .ChildHandler(new ChannelHandlerAdapter());

            // Not close the Channel to ensure the EventLoop is still shutdown in time.
            var cf = b.BindAsync(0);

            cf.GetAwaiter().GetResult();

            var f = loop.ShutdownGracefullyAsync(TimeSpan.Zero, TimeSpan.FromMinutes(1));

            Assert.True(loop.TerminationCompletion.Wait(TimeSpan.FromMilliseconds(600)));
            Assert.True(f.IsSuccess());
            Assert.True(loop.IsShutdown);
            Assert.True(loop.IsTerminated);
        }
Exemplo n.º 5
0
        private static async Task RunClientAsync()
        {
            HttpClientHandler httpClientHandler = new HttpClientHandler();
            IEventLoopGroup   group             = new DispatcherEventLoopGroup();

            try
            {
                Bootstrap bootstrap = new Bootstrap()
                                      .Group(group)
                                      .Channel <TcpChannel>()
                                      .Option(ChannelOption.SoBacklog, 8192)
                                      .Handler(new ActionChannelInitializer <IChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    pipeline.AddLast("decoder", new HttpResponseDecoder(4096, 8192, 8192, false));
                    pipeline.AddLast("aggregator", new HttpObjectAggregator(1024));
                    pipeline.AddLast("encoder", new HttpRequestEncoder());
                    pipeline.AddLast("deflater", new HttpContentDecompressor());    //解压
                    pipeline.AddLast("handler", httpClientHandler);
                }));

                Stopwatch stopwatch = new Stopwatch();
                while (true)
                {
                    Console.WriteLine("请输入请求地址(http://127.0.0.1:5000/api/values)");
                    string url = Console.ReadLine();
                    if (string.IsNullOrWhiteSpace(url))
                    {
                        url = "http://127.0.0.1:5000/api/values";
                    }

                    try
                    {
                        httpClientHandler = new HttpClientHandler();
                        Uri uri = new Uri(url);
                        stopwatch.Reset();
                        stopwatch.Start();
                        IChannel chanel = bootstrap.ConnectAsync(IPAddress.Parse(uri.Host), uri.Port).Result;

                        DefaultFullHttpRequest request = new DefaultFullHttpRequest(DotNetty.Codecs.Http.HttpVersion.Http11, HttpMethod.Get, uri.ToString());
                        HttpHeaders            headers = request.Headers;
                        headers.Set(HttpHeaderNames.Host, uri.Authority);

                        chanel.WriteAndFlushAsync(request).Wait();
                        while (true)
                        {
                            if (httpClientHandler.Data != null)
                            {
                                Console.WriteLine("结果:{0}", httpClientHandler.Data);
                                break;
                            }
                        }
                        stopwatch.Stop();
                        Console.WriteLine("耗时:{0}ms", stopwatch.ElapsedMilliseconds);
                        //await chanel.CloseAsync();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            finally
            {
                group.ShutdownGracefullyAsync().Wait();
            }
        }