Play Framework 无法处理超过 12 个并发连接
Play Framework can't handle more than 12 concurrent connections
我有一个无法处理超过 12 个并发连接的 vanilla Play 2.6 应用程序。它还会影响 Play 2.5。
这是一个示例控制器:
public class TestController extends Controller {
public Result index() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ok("");
}
}
测试 12 个并发连接:
ab -n 12 -c 12 http://localhost:9000/
输出:
...
Concurrency Level: 12
Time taken for tests: 1.005 seconds
Complete requests: 12
...
所以所有 12 个并发请求都在 1 秒内响应,这符合预期。
测试 13 个并发连接:
ab -n 13 -c 13 http://localhost:9000/
输出:
...
Concurrency Level: 13
Time taken for tests: 2.004 seconds
Complete requests: 13
...
现在 13 个并发连接需要 2 秒。这两种情况都经过多次测试并产生了一致的结果。
为什么会这样? Play 应该能够处理超过 12 个并发连接吗?
Play 在其基础上使用非阻塞 IO,并且不会为每个请求分配一个线程。所以当你使用像Thread.sleep
这样的方法时,你就阻止了Play使用线程来处理其他请求。
做阻塞IO时,文档推荐使用专用线程池。您可以在官方文档中阅读更多信息以及如何处理这种情况:https://www.playframework.com/documentation/2.6.x/ThreadPools#Understanding-Play-thread-pools
我有一个无法处理超过 12 个并发连接的 vanilla Play 2.6 应用程序。它还会影响 Play 2.5。
这是一个示例控制器:
public class TestController extends Controller {
public Result index() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ok("");
}
}
测试 12 个并发连接:
ab -n 12 -c 12 http://localhost:9000/
输出:
...
Concurrency Level: 12
Time taken for tests: 1.005 seconds
Complete requests: 12
...
所以所有 12 个并发请求都在 1 秒内响应,这符合预期。
测试 13 个并发连接:
ab -n 13 -c 13 http://localhost:9000/
输出:
...
Concurrency Level: 13
Time taken for tests: 2.004 seconds
Complete requests: 13
...
现在 13 个并发连接需要 2 秒。这两种情况都经过多次测试并产生了一致的结果。
为什么会这样? Play 应该能够处理超过 12 个并发连接吗?
Play 在其基础上使用非阻塞 IO,并且不会为每个请求分配一个线程。所以当你使用像Thread.sleep
这样的方法时,你就阻止了Play使用线程来处理其他请求。
做阻塞IO时,文档推荐使用专用线程池。您可以在官方文档中阅读更多信息以及如何处理这种情况:https://www.playframework.com/documentation/2.6.x/ThreadPools#Understanding-Play-thread-pools