Prefer ipv6 for listen addrs if available

If the listen address allows for ipv4 or ipv6 values we want to prefer
ipv6 if the host is configured with working ivp6. We add the ai_flag
AF_ADDRCONFIG to filter out ipv6 (and ipv4) if the host isn't configure
for this AF_INET version. Then we sort based on the family to prefer
ipv6 over ipv4.

The reason for this is clients will prefer ipv6 before falling back to
ipv4 when attempting to connect to a hostname. If the server isn't
listening on ipv6 this makes new connections happen slowly.

Change-Id: I9f7a235b04068856c6cceeb2c230f3b56945572e
This commit is contained in:
Clark Boylan 2018-10-19 10:02:43 -07:00
parent c00ca944db
commit ca733f3e07
1 changed files with 8 additions and 3 deletions

View File

@ -2735,9 +2735,14 @@ class Server(BaseClientServer):
if all([self.ssl_key, self.ssl_cert, self.ssl_ca]):
self.use_ssl = True
for res in socket.getaddrinfo(host, self.port, socket.AF_UNSPEC,
socket.SOCK_STREAM, 0,
socket.AI_PASSIVE):
# Get all valid passive listen addresses, then sort by family to prefer
# ipv6 if available.
addrs = socket.getaddrinfo(host, self.port, socket.AF_UNSPEC,
socket.SOCK_STREAM, 0,
socket.AI_PASSIVE |
socket.AI_ADDRCONFIG)
addrs.sort(key=lambda addr: addr[0], reverse=True)
for res in addrs:
af, socktype, proto, canonname, sa = res
try:
self.socket = socket.socket(af, socktype, proto)