对HTTP Nio线程池中阻止的同一URL的请求

时间:2018-08-14 07:36:45

标签: spring-boot tomcat nio spring-restcontroller

我只是在springboot(1.5.4.RELEASE)应用程序中创建了一个RestController并测试了当有多个请求进入时它如何工作。令我感到困惑的是:

  • 相同的网址:第二个请求被阻止,直到执行第一个请求
  • 不同的url:无阻塞,几乎同时执行2个请求

我的问题是谁阻止了第二个请求,为什么?

测试代码:

@GetMapping(value = "/sleep")
public String sleep(HttpServletRequest request, @RequestParam boolean status) 
{
    String requestId = request.toString();
    logger.info("request  [{}] in  and status = {}.", requestId, status);
    String result;
    if (status)
    {
        Thread.currentThread().sleep(10 * 1000);
        result = "slept";
    }
    else
    {
        result = "stay up";
    }
    logger.info("response [{}] out and result = [{}].", requestId, result);
    return result;
}

测试结果:

  1. 不同的网址:请勿阻塞,几乎同时开始执行。

http://localhost:20002/sleep?status=falsehttp://localhost:20002/sleep?status=true

2018-08-14 15:04:14.139  [http-nio-20002-exec-5]: connection [RequestFacade@46515328] in  and status = true.
2018-08-14 15:04:16.452  [http-nio-20002-exec-6]: connection [RequestFacade@1140f857] in  and status = false.
2018-08-14 15:04:16.452  [http-nio-20002-exec-6]: connection [RequestFacade@1140f857] out and result = [stay up].
2018-08-14 15:04:24.139  [http-nio-20002-exec-5]: connection [RequestFacade@46515328] out and result = [slept].
  1. 相同的url:块,直到完成第一个请求后,第二个请求才执行。

http://localhost:20002/sleep?status=truehttp://localhost:20002/sleep?status=true

2018-08-14 15:10:29.943  [http-nio-20002-exec-9]: connection [RequestFacade@46515328] in  and status = true.
2018-08-14 15:10:39.944  [http-nio-20002-exec-9]: connection [RequestFacade@46515328] out and result = [slept].
2018-08-14 15:10:39.960  [http-nio-20002-exec-1]: connection [RequestFacade@1140f857] in  and status = true.
2018-08-14 15:10:49.960  [http-nio-20002-exec-1]: connection [RequestFacade@1140f857] out and result = [slept].

我调试了tomcat-embed-core的代码,发现当带有不同url的请求进入时,Poller线程可以立即捕获并处理它。当具有相同网址的请求进入时,轮询器在返回第一个连接之前无法获取它。

1 个答案:

答案 0 :(得分:0)

由于@ M.Deinum,阻止与NIO无关。是Chrome阻止了第二个请求,出于某种原因,我在下面找到了一些评论:

this behavior is due to Chrome locking the cache and waiting to see the result of one 
request before requesting the same resource again. 

https://stackoverflow.com/a/27514611/10222882

并且由chrome Network证明->计时,第二个请求处于[已停止]状态,直到返回第一个响应。

connection_stalled