A
A
antobra2021-11-15 00:57:04
PHP
antobra, 2021-11-15 00:57:04

Php socket create connection error with many requests, how to win?

Greetings. Tell me, what could be the reason for the error that the client does not connect to the socket when there are many requests through socket_create? That is, for example, with a cycle of 5k requests, it takes a few seconds, and after “bang” and a connection error.
Technically, I solved the issue by adding usleep(3000) after socket_close, but this is not a normal story. This increases the script execution time, and it looks like a crutch obviously. Are there options to avoid the error?
Ps: I checked that each connection ends with socket_close and the connection does not hang. That is, as if the server does not have time to close the previous connection somewhere inside, as a new one knocks on it. It is for this reason that usleep helps the socket "cool down". I don't know, I've been scratching my head for a few days. I take the server-client code from php.net. The SO_REUSEADDR setting is present (connection to 127.0.0.1).
Advise what you can see, what to look for?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitsliputsli, 2021-11-15
@antobra

See connection error logs.
Check the number of simultaneous connections, if around 1000, then you most likely stumbled upon the limit of file descriptors.
The fact is that php creates a file for each connection, the limit of the number of file descriptors per process by default, something like 1024. As far as I remember, this is the OS limit and the php limit. Therefore, you need to increase the limit in Linux, and you will have to rebuild php with a large number.
Or use any other language, without such troubles. Anyway, in python and go, there are no such problems, they do not use file descriptors for each connection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question