There are two Internet providers. which "fall off" with unpredictable periodicity. Accordingly, the server has two IP (from two Internet providers), between which you have to switch to maintain the site (data is being uploaded from the server).

The task is to check which connection is active. It is desirable to quickly ... What I tried: I created a file on the server and check what query (through what IP) it is available. But...

is_readable, file_exists - false with active connection

fopen, file_get_contents - correspond to the situation, but they are “spinning” for quite a long time.

get_headers - sometimes does not work (does not give headers, although the file is opened or given)

Questions: What is the best way to check connection activity? Is the logical way I chose? Maybe the idea to read the file is not the best, but it is worth trying to connect to the database?

    1 answer 1

    "fopen, file_get_contents" - correspond to the situation, but they "spin" for quite some time.

    And, nevertheless, the most reliable way to check if a server is available at a certain IP address is to try to connect. A network connection is long in any case (if not with a guaranteed server in one data center), so it makes sense to do this check somehow asynchronously and regularly. For example, on the crown, run a script that will check the connection and write the IP address to a simple file. So when it becomes necessary to use a connection, it is already known what address to contact - to read it from a file.

    In general, it looks like you need to look in the direction of some load-balancer (LB). For example HAProxy. The principle is that this LB listens to the local port, which will use your site. And LB already automatically sends a request to the desired external resource, automatically monitoring which one of them is available.

    Here are a couple descriptions of the HAProxy settings for this configuration (in English; if English is not strong, you can look for similar in Russian): https://www.haproxy.com/blog/failover-and-worst-case-management-with- haproxy / https://serverfault.com/questions/473445/haproxy-switch-only-in-case-of-server-down

    • Thanks for the detailed answer. Website hosting, server separately. Installing a proxy or a listener at the hoster is quite difficult. I experiment with attempts to connect to the database. Maybe it will turn out faster - DNS