From ca733f3e0789e5161288b0dcf2d9660890e8ba44 Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Fri, 19 Oct 2018 10:02:43 -0700 Subject: 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 --- gear/__init__.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/gear/__init__.py b/gear/__init__.py index 4ed674e..b1d1cdd 100644 --- a/gear/__init__.py +++ b/gear/__init__.py @@ -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) -- cgit v1.2.1