【问题标题】:Play Framework can't handle more than 12 concurrent connectionsPlay Framework 无法处理超过 12 个并发连接
【发布时间】:2018-10-18 09:32:09
【问题描述】:

我有一个无法处理超过 12 个并发连接的普通 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 个并发连接吗?

【问题讨论】:

    标签: java playframework playframework-2.0 akka akka-http


    【解决方案1】:

    Play 在其基础上使用非阻塞 IO,它不会为每个请求分配线程。所以当你使用像Thread.sleep这样的方法时,你会阻止Play使用线程来处理其他请求。

    在做阻塞 IO 时,文档建议使用专用的线程池。您可以在官方文档中阅读更多信息以及如何处理此案例:https://www.playframework.com/documentation/2.6.x/ThreadPools#Understanding-Play-thread-pools

    【讨论】:

    • 谢谢,我一定有过短暂的精神错乱。我知道神奇数字 12 的来源——它是我的 CPU 线程数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多