Reputation: 9008
I am trying to create a set of websocket clients with the following code:
$server = stream_socket_server("tcp://127.0.0.1:8080");
for ($i = 1; $i <= 50; $i++) {
var_dump($i);
stream_socket_client("tcp://127.0.0.1:8080");
}
The first 35, or so, connections are created very fast. Then everything slows down and every step takes 1 second to execute.
Could you explain to me why this behaviour happens? Is it caused by a configuration parameter? Is it a common websocket behaviour?
Upvotes: 1
Views: 784
Reputation: 121
Working hypothesis: pending connections slows down the port
<?php
$server = stream_socket_server("tcp://127.0.0.1:8080");
for ($i = 1; $i <= 50; $i++) {
var_dump($i);
stream_socket_client("tcp://127.0.0.1:8080");
stream_socket_accept($server);
}
Supplement:
In case listening party is server.c (modified not to accepting connections). Connections slow down after 27% of specified backlog. http://www.linuxhowtos.org/data/6/server.c
<?php
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($socket, "127.0.0.1", 8081);
$backlog = 500; // less than /proc/sys/net/core/somaxconn
socket_listen($socket, $backlog);
for ($i = 1; $i <= 500; $i++) {
var_dump($i);
stream_socket_client("tcp://127.0.0.1:8081");
}
Upvotes: 1