Skip to content

Commit e3f3bf2

Browse files
committed
Skip IPv6 for 'localhost' PYTHON-356
This works around Winsock's misfeature of re-trying connections 3 times before failing, making localhost connections slow on Windows. If you must use IPv6 on localhost pass ::1 as host.
1 parent f4ec4be commit e3f3bf2

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

pymongo/pool.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,15 @@ def create_connection(self, pair):
178178
This is a modified version of create_connection from
179179
CPython >=2.6.
180180
"""
181-
# Don't try IPv6 if we don't support it.
181+
host, port = pair or self.pair
182+
183+
# Don't try IPv6 if we don't support it. Also skip it if host
184+
# is 'localhost' (::1 is fine). Avoids slow connect issues
185+
# like PYTHON-356.
182186
family = socket.AF_INET
183-
if socket.has_ipv6:
187+
if socket.has_ipv6 and host != 'localhost':
184188
family = socket.AF_UNSPEC
185189

186-
host, port = pair or self.pair
187190
err = None
188191
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
189192
af, socktype, proto, dummy, sa = res

0 commit comments

Comments
 (0)