HttpClientPool icon indicating copy to clipboard operation
HttpClientPool copied to clipboard

测试同步发送请求高并发场景下 client.getRequest()会有概率性的为null

Open ghost opened this issue 4 years ago • 1 comments

测试代码如下:

 public static void main(String args[]) throws Throwable {
        final ClientPool pool = new NettyClientPool(1, "127.0.0.1");
        pool.start();
        for (int i = 0; i < 5; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 1000; i++) {
                        String name = Thread.currentThread().getName();
                        Request request = new Request("/", Request.RequestMethod.POST);
                        request.getBody().writeBytes((name+" "+i + "aa").getBytes(Charset.forName("utf-8")));
                        Response response = null;
                        try {
                            response = pool.requestWithTimeOut(request, 30000).sync();
                            //pool.stop();
                            System.out.println(response.getBody().toString(Charset.forName("utf-8")));
                        } catch (Throwable throwable) {
                            throwable.printStackTrace();
                        }

                    }
                }
            }).start();
        }
    }

在ChannelRead方法中会概率性抛出:client.getRequest()空指针异常,一直没搞明白为什么会抛出这个异常,猜测是client.ready()使用了setRequest(null),但是我看这个方法是同步的 按道理不应该

                if (msg instanceof LastHttpContent) {
                    Response nr = new Response(this.response, this.body);
                    this.body = null;

                    if ( client.getRequest()
                        .getFuture()
                        .getStatus()
                        .equals(Future.FutureStatus.Running)) {
                        client.getRequest().getFuture().success(nr);
                    } else {
                        Logger.getLogger("main").warning("请求执行付完毕,但是请求状态不对:"+nr.getBody().toString(Charset.defaultCharset()));
                    }
                    client.ready();
                }

ghost avatar May 06 '20 09:05 ghost

我也,,请问有解决方案嘛 大佬

Vikincer avatar Aug 04 '23 08:08 Vikincer