PHP Can someone explain the pfsockopen function to me? (persistent socket)

From PHP.net:

http://www.php.net/manual/en/function.pfsockopen.php

I understand the essence of what this function performs, but it is still unclear to me whether it will fulfill what I would like. Here is my scenario:

I have a large PHP application that is used by many users at the same time. Inside the application, I open a TCP socket on a remote server to register messages, etc. I was hoping I could use pfsockopen so that it would be necessary to open much fewer connections. For example, a custom in-socket character is opened. User2 signs, the socket does not open, because it can "copy" to the socket opened by user1.

Is it possible?

+6
source share
1 answer

pfsockopen really keep the socket open when the script ends, allowing it to reuse the request from another, effectively opening fewer connections, as you would expect. However, this is not compatible with all SAPIs.

Persistence is found on the basis of each process . Thus, pfsockopen runs in the CLI. SAPI closes and reopens the socket at each execution, because the CLI script is executed in one running process, opens the socket and ends (closing the socket with the process).

In CGI mode with one process per script, this is also true.

When using Apache SAPI, it depends on what type of multiprocessor module (MPM) is used. mpm-prefork generates a new process with every request, so it most likely does not support it. mpm-worker , however, spawns threads, so it will probably work there. mpm-winnt is a version of Windows with multi-threaded MPM, so it should work too.

The worst fsockopen that the call will be executed as a normal fsockopen call.

+9
source

Source: https://habr.com/ru/post/901318/


All Articles