swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.41k stars 3.16k forks source link

Swoole\Http\Server I can't take more then 1000 connections at the same time #1330

Closed morozovsk closed 7 years ago

morozovsk commented 7 years ago

Please answer these questions before submitting your issue. Thanks! 在提交Issue前请回答以下问题:

  1. What did you do? If possible, provide a recipe for reproducing the error. 请详细描述问题的产生过程,贴出相关的代码,最好能提供可稳定重现的最小实例。
$server = new Swoole\Http\Server('0.0.0.0', 80);
$server->set([
    'worker_num' => 1,
    'max_connection' => 50000,
    'max_request' => 1000000
]);
$max = 0;
$server->on('Request', function($request, $response) use ($pdo, $server, &$max) {
    if (count($server->connections) > $max) {
        $max = count($server->connections);
        echo "max:$max\n";
    }

    //my slow code
}
$server->start();
  1. What did you expect to see? 期望的结果是什么?

I woud like swoole works with more then 1000 connections.

  1. What did you see instead? 实际运行的结果是什么? max:984 max:991 max:998 max:1000 [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 1475 byte failed, because connection[fd=2184] is closed. max:1001 [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 623 byte failed, because connection[fd=2185] is closed. max:1011 [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 258 byte failed, because connection[fd=2187] is closed. max:1020 [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 372 byte failed, because connection[fd=2188] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 379 byte failed, because connection[fd=2189] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 357 byte failed, because connection[fd=2190] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 387 byte failed, because connection[fd=2191] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 365 byte failed, because connection[fd=2192] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 381 byte failed, because connection[fd=2193] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 351 byte failed, because connection[fd=2194] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 375 byte failed, because connection[fd=2195] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 371 byte failed, because connection[fd=2196] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 246 byte failed, because connection[fd=2197] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 246 byte failed, because connection[fd=2198] is closed. [2017-08-17 21:07:10 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 369 byte failed, because connection[fd=2200] is closed. [2017-08-17 21:07:11 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 182 byte failed, because connection[fd=2201] is closed. max:1030 [2017-08-17 21:07:11 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 338 byte failed, because connection[fd=2202] is closed. [2017-08-17 21:07:11 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 370 byte failed, because connection[fd=2203] is closed. [2017-08-17 21:07:11 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 339 byte failed, because connection[fd=2204] is closed. [2017-08-17 21:07:11 11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 368 byte failed, because connection[fd=2205] is closed. [2017-08-17 21:07:11 *11.0] ERROR swFactoryProcess_finish (ERROR 1004): send 183 byte failed, because connection[fd=2206] is closed. max:1042

  2. What version of Swoole are you using (php --ri swoole)? 你所使用的Swoole版本号是什么(使用php --ri swoole可以获取)?

Version => 1.9.18 Author => tianfeng.han[email: mikan.tenny@gmail.com] epoll => enabled eventfd => enabled timerfd => enabled signalfd => enabled cpu affinity => enabled spinlock => enabled rwlock => enabled async http/websocket client => enabled Linux Native AIO => enabled pcre => enabled zlib => enabled mutex_timedlock => enabled pthread_barrier => enabled futex => enabled

Directive => Local Value => Master Value swoole.aio_thread_num => 2 => 2 swoole.display_errors => On => On swoole.use_namespace => Off => Off swoole.fast_serialize => Off => Off swoole.unixsock_buffer_size => 8388608 => 8388608

  1. What is your machine environment used (including version of kernel & php & gcc) ? 你使用的机器系统环境是什么(包括内核、PHP、gcc编译器版本信息)?

docker, ubuntu 17.04, PHP 7.0.22 Linux 198fa5bd3634 4.8.0-52-generic #55-Ubuntu SMP Fri Apr 28 13:28:50 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux gcc (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406

  1. If you are using ssl, what is your openssl version? 如果你使用了SSL,你的openssl版本是什么?

no ssl

PS: I take part in the competition https://highloadcup.ru/rating/ and I wrote rest api on swoole with slite in memory. It is very fast on test with 100rpc but it breaks when 200 rpc.

matyhtf commented 7 years ago

Check ulimit -n system settings.

morozovsk commented 7 years ago

Thank you very match for you help. ulimit in docker: 1048576 ulimit in host system: 65535 for user and 1024 for root (even after reboot)

In host system I have /etc/security/limits.conf:

If I start my php script under user without docker it have more 1024 connections, but if I start under root (also without docker) I have problem right after start my script (before I start test): [2017-08-18 11:52:43 @8043.0] WARNING swServer_start_check: serv->max_connection is exceed the maximum value[1024].

I think problem in it. I don't know how fix this problem for root for default using, only for custom commands: sudo sh -c "ulimit -n 65535 && exec su $LOGNAME"

But I found how to fix it for docker: it needs to append --ulimit nofile=65535:65535 for run command.

Thank you again for you help and for nice library.

morozovsk commented 7 years ago

I fixed my code it. Now it is only 5 times more slow the winner code on C. Your library is very fast. I will optimize my code and may be I will win :)