Commit e76bcac6 authored by Denis Bilenko's avatar Denis Bilenko

socket: resolve ipv6 and ipv4 concurrently in getaddrinfo() thus reducing the...

socket: resolve ipv6 and ipv4 concurrently in getaddrinfo() thus reducing the delay in AF_UNSPEC case
parent 06077e87
...@@ -136,6 +136,8 @@ if 'inet_ntop' not in globals(): ...@@ -136,6 +136,8 @@ if 'inet_ntop' not in globals():
from gevent.hub import getcurrent, get_hub from gevent.hub import getcurrent, get_hub
from gevent import core from gevent import core
from gevent import spawn
from gevent.util import wrap_errors
_ip4_re = re.compile('^[\d\.]+$') _ip4_re = re.compile('^[\d\.]+$')
...@@ -745,22 +747,29 @@ else: ...@@ -745,22 +747,29 @@ else:
result.append((family, socktype, proto, '', sockaddr)) result.append((family, socktype, proto, '', sockaddr))
else: else:
failure = None failure = None
job = spawn(wrap_errors(gaierror, resolve_ipv6), host, evdns_flags)
try: try:
for res in resolve_ipv4(host, evdns_flags)[1]: try:
sockaddr = (inet_ntop(AF_INET, res), port) ipv4_res = resolve_ipv4(host, evdns_flags)[1]
for socktype, proto in socktype_proto: except gaierror, failure:
result.append((AF_INET, socktype, proto, '', sockaddr)) ipv4_res = None
except gaierror, failure: ipv6_res = job.get()
pass if isinstance(ipv6_res, gaierror):
try: ipv6_res = None
for res in resolve_ipv6(host, evdns_flags)[1]: if failure is not None:
sockaddr = (inet_ntop(AF_INET6, res), port, 0, 0) raise
for socktype, proto in socktype_proto: if ipv4_res is not None:
result.append((AF_INET6, socktype, proto, '', sockaddr)) for res in ipv4_res:
except gaierror: sockaddr = (inet_ntop(AF_INET, res), port)
if failure is not None: for socktype, proto in socktype_proto:
raise result.append((AF_INET, socktype, proto, '', sockaddr))
if ipv6_res is not None:
for res in ipv6_res[1]:
sockaddr = (inet_ntop(AF_INET6, res), port, 0, 0)
for socktype, proto in socktype_proto:
result.append((AF_INET6, socktype, proto, '', sockaddr))
finally:
job.kill()
return result return result
# TODO libevent2 has getaddrinfo that is probably better than the hack above; should wrap that. # TODO libevent2 has getaddrinfo that is probably better than the hack above; should wrap that.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment