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:
- 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].
- 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.