diff options
author | Clark Boylan <clark.boylan@gmail.com> | 2018-10-19 10:02:43 -0700 |
---|---|---|
committer | Clark Boylan <clark.boylan@gmail.com> | 2018-10-19 14:33:37 -0700 |
commit | ca733f3e0789e5161288b0dcf2d9660890e8ba44 (patch) | |
tree | c6a28535f50126d0ad2201af212b3eeb01473bfa | |
parent | c00ca944db0d6dc6ef90859b0b9b7f3a58196fb0 (diff) | |
download | gear-ca733f3e0789e5161288b0dcf2d9660890e8ba44.tar.gz |
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
-rw-r--r-- | gear/__init__.py | 11 |
1 files 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) |