1

I simply created a RestController in springboot(1.5.4.RELEASE) application and test how it works when multiple requests come in. What confused me is:

  • same url : the 2nd request blocked until the 1st request executed
  • different url: non-block, 2 requests executed almost at the same time

My question is who is blocking the 2nd requst and why?

Test Code:

@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;
}

Test Result:

  1. Different url:do not block, almost start executing at the same time.

http://localhost:20002/sleep?status=false AND http://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. Same url: block, the 2nd requst doesn't execute until the 1st requst is done.

http://localhost:20002/sleep?status=true AND http://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].

I debug the code of tomcat-embed-core and find that when request with different url comes in, the Poller thread could catch it immediately and process it; while request with the same url come in, the Poller could not get it until the 1st connection is returned.

  • What happens if you use Jetty or a newer version of Spring Boot (1.5.14 instead of 1.5.4)? – M. Deinum Aug 14 '18 at 09:21
  • Test on Jetty and SpringBoot 1.5.14.RELEASE, truns out to be no difference. – George Wang Aug 23 '18 at 07:42
  • Tried to reproduce it, but works as expected on my side. Firing 8 concurrent requests for the same URL and they arrive concurrently. How are you testing this? From a browser, a test or something like cUrl or Httpie on the command line? I suspect a browser which probably prevents calling the same URL (and waits for the first request to finish before calling it again). – M. Deinum Aug 23 '18 at 08:01
  • Yes,thanks.Using diffenent cliets and the requests arrive concurrently.It's the chrome who stalled the request to the same resource. – George Wang Aug 24 '18 at 01:37

1 Answers1

0

Thanks to @M.Deinum, the blocking has nothing to do with NIO. It's the chrome who blocked the 2nd request, and for reason ,I find some comment below:

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

And It's proved by chrome Network -> Timing, the 2nd request is in [stalled] status until the 1st response is returned.

connection_stalled