0

With the current implementation of getaddrinfo(), I'm not given any information about a timeout of the IP address(es) returned. The library implementing that function has the information, but I haven't yet seen a function to retrieve the timeout.

What I'm wondering is how do people current implement the concept of connecting to a service when they are given multiple addresses and the connection either never happens or fails after a while.

I can think of multiple algorithms and I'm wondering which one is currently used:

  1. User gather host name (say from a .conf file)
  2. User creates a "connection object"
  3. User says "connect" on that object
  4. Object transform the host name in a list of IP addresses
  5. Object tries to connect to first IP
    1. Connection in (5) fails, try with next IP
    2. Connection in (5.1) fails, repeat until all IPs were tested
    3. All IPs were exhausted, sleep and try again from (4)
  6. Connection in 5 succeeded, run with that connection until we lose it
  7. Connection is lost, try again from (4) or from (5.1)?

My main problem is what happens in step (7). Should we try again from (4), which means we are not unlikely to retry the same IP address, or should we try from (5.1), in which case we may be testing with an out of date¹ IP address... (it could be days between step (6) and step (7)). I think that if I had the timeout for the IP address, I could do a smart decision since I would know whether I should go back to step (4). Without that timeout, I'm kind of stuck...

What is the current practice?


(1) The IP itself will still be owned by that company, but the DNS assigns a duration to its response (the TTL). More or less, it means that the IP should not be used after that duration has elapsed.

4
  • 1
    Addresses don't have timeouts. What do you mean, it should return a timeout? There is no such concept. It's like asking for the colour of the word "apple" Jul 30, 2021 at 9:52
  • @user253751 A DNS always assigns a timeout to the IPs that it returns. That timeout is actually very small for large companies because a minute later they may want you to get other IPs. Actually, if you try to retrieve IPs several times from a large website, even before things timeout, you are not unlikely to get different sets. Jul 30, 2021 at 15:46
  • Oh, are you thinking of TTL? The DNS library or server should manage that. Just call getaddrinfo every time you need the current addresses. Jul 30, 2021 at 15:51
  • @user253751 Yes. I updated my question accordingly. Jul 30, 2021 at 15:52

1 Answer 1

3

The best practice is to let DNS pick the IP address and use whatever it tells you.

If the service has provided multiple DNS entries with different IP addresses, that is generally not meant to provide failover but to provide geographic proximity or simply to distribute the load.

If a failover/redundancy solution is required, the typical approach is to hide the multiple IP addresses behind a load balancer, then put the load balancer's IP address in DNS. The load balancer will then forward traffic to the best IP. There are various techniques to keep track of which IP addresses are healthy and available for use, and it's not always simple. It would be very unusual to expect the client to deal with it.

1
  • The getaddrinfo() function is based on what the DNS tells my software, so I think that part is covered. From the answer you link, it looks like my point (7) should continue back at (4) in my algorithm. i.e. re-query the DNS each time, no matter what, except if a connection fails outright (never happens). Jul 28, 2021 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.